PyQt有一个状态栏和菜单栏QWidg

2024-09-27 20:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文


我正在尝试创建一个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函数。

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


Tags: selfisexampledefsysexitthisquit
3条回答

您还可以使用QMainWindow和QWidget的组合。

我发现这在某些情况下很有用。您可以将statusbar和menubar添加到MainWindow部分,将widgets添加到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()

下面是一个使用您的代码的工作解决方案。我把一个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()  

您可以将按钮/标签等移动到一个QWidget,然后将这个小部件添加到主窗口。下面是它的外观(我更改了导入,使其可读性更强)。

您的内容小部件类:

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

将其添加到主窗口:

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。希望这有帮助!

相关问题 更多 >

    热门问题