我正在尝试创建一个 PyQt 应用程序,它同时具有状态栏和菜单栏以及窗口中的其他小部件。下面是我设法让它用类 QtGui.QMainWindow 方法运行的代码。但是当我打算添加更多功能时,我意识到我必须使用 QtGui.QWidget 来代替。

这是代码:

import sys
from PyQt4 import QtGui, QtCore

### How can I use QtGui.QWidget here??? ###

class Example(QtGui.QMainWindow):
     def __init__(self):
          super(Example, self).__init__()
          self.initUI()

     def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        self.setToolTip('This is a <b>QWidget</b> Window widget')

        exitAction = QtGui.QAction(QtGui.QIcon('exit-icon-2.png'), '&Exit', self)

        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit/Terminate application')

        exitAction.triggered.connect(QtGui.qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        menubar.setToolTip('This is a <b>QWidget</b> for MenuBar')

        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAction)

        qbtn = QtGui.QPushButton('Quit', self)

        qbtn.setToolTip('This is a <b>QPushButton</b> widget')
        qbtn.clicked.connect(self.launchAAA)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(170, 190)



        self.setGeometry(500, 180, 400, 400)
        self.setWindowTitle('Quit button with Message')
        self.show()

    def launchAAA(self, event):

        reply = QtGui.QMessageBox.question(self, 'Message',
        "Are you sure to quit?", QtGui.QMessageBox.Yes |
        QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
           QtGui.QApplication.quit()
        else:
           pass


def main():

   app = QtGui.QApplication(sys.argv)
   ex=Example()

   sys.exit(app.exec_())


if __name__ == '__main__':
   main()

我的印象是菜单栏和标题栏可以使用 QWidget 方法创建,但在这里它不起作用。我打算使用 QtGui.QLCDNumber 向应用程序添加 LCD 功能。

有关如何解决上述问题的任何建议。谢谢

最佳答案

这是使用您的代码的有效解决方案。我在 centralWidget 中添加了一个 centralLayout 和一个 QMainWindow ,它现在包含您的 qbtn :

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
     def __init__(self):
          super(Example, self).__init__()
          self.initUI()

     def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        self.setToolTip('This is a <b>QWidget</b> Window widget')

        exitAction = QtGui.QAction(QtGui.QIcon('exit-icon-2.png'), '&Exit', self)

        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit/Terminate application')

        exitAction.triggered.connect(QtGui.qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        menubar.setToolTip('This is a <b>QWidget</b> for MenuBar')

        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAction)

        # Create a central Widgets
        centralWidget = QtGui.QWidget()

        # Create a Layout for the central Widget
        centralLayout = QtGui.QHBoxLayout()



        qbtn = QtGui.QPushButton('Quit', self)

        qbtn.setToolTip('This is a <b>QPushButton</b> widget')
        qbtn.clicked.connect(self.launchAAA)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(170, 190)

        # Add the Button to the Layout
        centralLayout.addWidget(qbtn)

        # Set the Layout
        centralWidget.setLayout(centralLayout)

        # Set the Widget
        self.setCentralWidget(centralWidget)

        self.setGeometry(500, 180, 400, 400)
        self.setWindowTitle('Quit button with Message')
        self.show()

     def launchAAA(self, event):

        reply = QtGui.QMessageBox.question(self, 'Message',
        "Are you sure to quit?", QtGui.QMessageBox.Yes |
        QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
           QtGui.QApplication.quit()
        else:
           pass


def main():

   app = QtGui.QApplication(sys.argv)
   ex=Example()

   sys.exit(app.exec_())


if __name__ == '__main__':
   main()

关于python - PyQt 有一个状态栏和菜单栏 QWidget,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38322349/

10-14 18:11
查看更多