python3+PyQt5实现窗口切分条

本文是对《Python Qt GUI快速编程》的第9章的窗口切分条例子News Reader 用Python3+PyQt5进行改写。
注:import qrc_resources,资源文件resources.qrc如下:

===========================

<RCC version="1.0">
<qresource>
<file alias="icon.png">images/icon.pngfile>
<file alias="filenew.png">images/filenew.pngfile>
<file alias="fileopen.png">images/fileopen.pngfile>
<file alias="filesave.png">images/filesave.pngfile>
<file alias="filesaveas.png">images/filesaveas.pngfile>
<file alias="fileclose.png">images/filequit.pngfile>
<file alias="filequit.png">images/filequit.pngfile>
<file alias="editcut.png">images/editcut.pngfile>
<file alias="editcopy.png">images/editcopy.pngfile>
<file alias="editpaste.png">images/editpaste.pngfile>
<file alias="editadd.png">images/editadd.pngfile>
<file alias="editedit.png">images/editedit.pngfile>
<file alias="editdelete.png">images/editdelete.pngfile>
qresource>
RCC>

======
sudo pyrcc5 -o qrc_resources.py resources.qrc 生成qrc_resources.py

import sys
from PyQt5.QtCore import (QSettings, Qt,QByteArray)
from PyQt5.QtWidgets import (QAction, QApplication, QListWidget,
        QMainWindow, QSplitter, QTextBrowser)
from PyQt5.QtGui import QIcon
import qrc_resources


__version__ = "1.0.0"


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.groupsList = QListWidget()
        self.messagesList = QListWidget()
        self.messageView = QTextBrowser()

        self.messageSplitter = QSplitter(Qt.Vertical)
        self.messageSplitter.addWidget(self.messagesList)
        self.messageSplitter.addWidget(self.messageView)
        self.mainSplitter = QSplitter(Qt.Horizontal)
        self.mainSplitter.addWidget(self.groupsList)
        self.mainSplitter.addWidget(self.messageSplitter)
        self.setCentralWidget(self.mainSplitter)

        self.mainSplitter.setStretchFactor(0, 1)
        self.mainSplitter.setStretchFactor(1, 3)
        self.messageSplitter.setStretchFactor(0, 1)
        self.messageSplitter.setStretchFactor(1, 2)

        self.createMenusAndToolbars()

        settings = QSettings()
        if settings.value("MainWindow/Geometry") or \
                      settings.value("MainWindow/State") or \
                      settings.value("MainSplitter"):
            self.restoreGeometry(
                    QByteArray(settings.value("MainWindow/Geometry")))
            self.restoreState(
                    QByteArray(settings.value("MainWindow/State")))
            self.messageSplitter.restoreState(
                    QByteArray(settings.value("MessageSplitter")))
            self.mainSplitter.restoreState(
                    QByteArray(settings.value("MainSplitter")))

        status = self.statusBar()
        status.setSizeGripEnabled(False)
        status.showMessage("Ready", 5000)
        self.setWindowTitle("News Reader")
        self.generateFakeData()


    def createMenusAndToolbars(self):
        fileMenu = self.menuBar().addMenu("&File")
        fileToolbar = self.addToolBar("File")
        fileToolbar.setObjectName("FileToolbar")
        for icon, text in (("new", "&New..."), ("open", "&Open..."),
                ("save", "&Save"), ("save", "Save &As..."),
                (None, None), ("quit", "&Quit")):
            if icon is None:
                fileMenu.addSeparator()
            else:
                action = QAction(QIcon(":/file{0}.png".format(icon)),
                                 text, self)
                if icon == "quit":
                    action.triggered.connect(self.close)
                elif text != "Save &As...":
                    fileToolbar.addAction(action)
                fileMenu.addAction(action)

        editMenu = self.menuBar().addMenu("&Edit")
        editToolbar = self.addToolBar("Edit")
        editToolbar.setObjectName("EditToolbar")
        for icon, text in (("add", "&Add..."), ("edit", "&Edit..."),
                           ("delete", "&Remove")):
            action = QAction(QIcon(":/edit{0}.png".format(icon)),
                             text, self)
            editToolbar.addAction(action)
            editMenu.addAction(action)


    def closeEvent(self, event):
        if self.okToContinue():
            settings = QSettings()
            settings.setValue("MainWindow/Geometry",
                              self.saveGeometry())
            settings.setValue("MainWindow/State",
                              self.saveState())
            settings.setValue("MessageSplitter",
                    self.messageSplitter.saveState())
            settings.setValue("MainSplitter",
                    self.mainSplitter.saveState())
        else:
            event.ignore()


    def okToContinue(self):
        return True


    def generateFakeData(self):
        for group in ("ada", "apl", "asm.*", "asm370", "awk", "basic.*",
                "beta", "c.*", "c++.*", "clarion", "clipper.*", "clos",
                "clu", "cobol", "dylan", "eiffel", "forth.*",
                "fortran.*", "functional", "haskell", "hermes", "icon",
                "idl", "idl-pvwave", "java.*", "javascript", "labview",
                "limbo", "lisp.*", "logo", "misc", "ml.*", "modula2",
                "modula3", "mumps", "oberon", "objective-c", "pascal.*",
                "perl.*", "php.*", "pl1", "pop", "postscript",
                "prograph", "prolog", "python.*", "rexx.*", "ruby",
                "sathe", "scheme.*", "sigplan", "smalltalk.*", "tcl.*",
                "verilog", "vhdl", "visual.*", "vrml"):
            self.groupsList.addItem("comp.lang.{0}".format(group))
        for topic, author in (
                ("ANN: Einf\u00FChrung in die Programmierung mit Python",
                 "Ian Ozsvald",),
                ("SQLObject 0.7.3", "Oleg Broytmann",),
                ("ANN: Pyrex 0.9.5.1", "greg",),
                ("ANN: gozerbot IRC and JABBER bot", "bthate",),
                ("Extended deadline: CfP IEEE Software Special Issue on "
                 "Rapid Application Development with Dynamically Typed "
                 "Languages", "Laurence Tratt",),
                ("ANN: New python software community website in Chinese, "
                 "PythonNet.com", "Wenshan Du",),
                ("ANN: Plex 1.1.5 (Repost)", "greg",),
                ("ANN: Pyrex 0.9.5", "greg",),
                ("ftputil 2.2.1", "Stefan Schwarzer",),
                ("FlightFeather Social Networking Platform 0.3.1",
                 "George Belotsky",),
                ("OSCON 2007 Call for Participation Ends Soon",
                 "Kevin Altis",),
                ("ANN: tl.googlepagerank", "Thomas Lotze",),
                ("Dejavu 1.5.0RC1", "Robert Brewer",),
                ("PyCon: one week left for hotel registration",
                 "A.M. Kuchling",),
                ("FlightFeather Social Networking Platform 0.3.0",
                "George Belotsky",),
                ("SQLObject 0.8.0b2", "Oleg Broytmann",),
                ("SQLObject 0.7.3b1", "Oleg Broytmann",),
                ("ANN: Updated TkTreectrl wrapper module", "klappnase",),
                ("PyPy Trillke Sprints Feb/March 2007", "holger krekel",),
                ("wxPython 2.8.1.1", "Robin Dunn",),
                ("Movable Python 2.0.0 Final Available", "Fuzzyman",),
                ("ANN: Phebe 0.1.1", "Thomas Lotze",),
                ("Exception #03. Python seminar in Kiev city (Ukraine).",
                 "Mkdir",),
                ("FlightFeather Social Networking Platform 0.2.8",
                "George Belotsky",),
                ("ANN: Python Installation", "Ian Ozsvald",),
                ("ANN: pyGame Basics", "Ian Ozsvald",),
                ("PythonTidy 1.10", "Chuck Rhode",),
                ("Shed Skin Optimizing Python-to-C++ Compiler 0.0.10",
                 "Mark Dufour",),
                ("ANN : Karrigell 2.3.3", "Pierre Quentel",),
                ("ANN: amplee 0.4.0", "Sylvain Hellegouarch")):
            self.messagesList.addItem("{0} from {1}".format(topic, author))
        self.messageView.setHtml("""
Groups:comp.lang.python.announce
From:"Fuzzyman" <[email protected]>
Subject:[ANN] Movable Python 2.0.0 Final Available

Movable Python 2.0.0 Final

http://www.voidspace.org.uk/python/movpy/ is now available.

The new binaries are available for download from the groups page :

Movable Python Groups Page http://voidspace.tradebit.com/groups.php

Binaries are uploaded for Python 2.2.3, 2.3.5, 2.4.4, 2.5 and the MegaPack http://www.voidspace.org.uk/python/movpy/megapack.html.

There is also a fresh version of the free demo, based on Python 2.3.5:

Movable Python Trial Version http://voidspace.tradebit.com/files.php/7007

What is Movable Python

Movable Python is a distribution of Python, for Windows, that can run without being installed. It means you can carry a full development environment round on a USB stick.

It is also useful for testing programs with a 'clean Python install', and testing programs with multiple versions of Python.

The GUI program launcher makes it a useful programmers tool, including features like :

  • Log the output of all Python scripts run
  • Enable Psyco for all scripts
  • Quick Launch buttons for commonly used programs
  • Configure multiple interpreters for use from a single interface

It comes with the Pythonwin IDE and works fine with other IDEs like SPE http://developer.berlios.de/projects/python/.

Plus many other features and bundled libraries.""") if __name__ == "__main__": app = QApplication(sys.argv) app.setOrganizationName("Qtrac Ltd.") app.setOrganizationDomain("qtrac.eu") app.setApplicationName("News Reader") form = MainWindow() form.show() app.exec_()

运行结果:
python3+PyQt5实现窗口切分条_第1张图片

你可能感兴趣的:(PyQt5)