具有状态栏和菜单栏的 PyQt QWidget

PyQt having a status bar & menu bar QWidget


我正在尝试创建一个 PyQt 应用程序,它具有状态栏和菜单栏以及 window 中的其他小部件。下面是我用 class 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 功能

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

这是一个使用您的代码的有效解决方案。我在 QMainWindow 中添加了一个 centralWidget 和一个 centralLayout,它现在包含你的 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()  

你可以移动你的buttons/labels/etc。到 QWidget,并将此小部件添加到主 window。这是它的样子(我更改了导入以使其更具可读性)。

您的内容小部件 class :

class ExampleContent(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)
        self.initUI()

    def initUI(self):        
        qbtn = 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)

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

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

添加到主window:

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

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

        exitAction = QAction(QIcon('exit-icon-2.png'), '&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit/Terminate application')
        exitAction.triggered.connect(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 the widget here
        content = ExampleContent(self)
        self.setCentralWidget(content)

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

除了中间有一个 QWidget 而不是 QMainWindow 之外,一切都像以前一样工作。希望对您有所帮助!

您也可以结合使用 QMainWindow 和 QWidget。

我发现这在某些情况下很有用。您可以将状态栏和菜单栏添加到 MainWindow 部分,将小部件添加到 QWidget 区域。

import sys
from PyQt4 import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):

def __init__(self, parent=None):

    super(MainWindow, self).__init__(parent)

    self.win_widget = WinWidget(self)
    widget = QtGui.QWidget()
    layout = QtGui.QVBoxLayout(widget)
    layout.addWidget(self.win_widget)
    self.setCentralWidget(widget)

    self.statusBar().showMessage('Ready')

    self.setGeometry(300, 300, 450, 250)
    self.setWindowTitle('Test')  
    self.setWindowIcon (QtGui.QIcon('logo.png'))
    self.show()

    self.win_widget = WinWidget (self)



class WinWidget (QtGui.QWidget) : 

def __init__(self, parent): 
    super (WinWidget , self).__init__(parent)
    self.__controls()
    #self.__layout()

def __controls(self):

    self.qbtn = QtGui.QPushButton('Quit', self)
    self.qbtn. clicked.connect(QtCore.QCoreApplication.instance().quit)
    self.qbtn.setFixedSize (100,25)
    self.qbtn.move(50, 50)  


def main():

app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()