在PyQt4应用程序中使用选项卡创建框架

问题描述 投票:0回答:1

所以基本上在某人的帮助下,我能够提出用于创建具有两个帧的GUI的代码。一个在左边,一个在右边。但是我在为真实的应用程序编写相同的代码时遇到了麻烦。

即这里的代码非常基本,因为它只包含一个main函数。我如何参考自我重写这段代码,就像我在这里尝试的那样。

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
        def __init__(self):
        super(Window,self).__init__()
        self.setGeometry(50,50,500,300)
        self.setWindowTitle("PyQt4 Tuts")
        self.home()


    def home(self):

        mainWindow = QtGui.QWidget()
        mainLayout = QtGui.QGridLayout(mainWindow)

        frameLeft = QtGui.QFrame(mainWindow)
        frameLeft.setFrameShape(QtGui.QFrame.StyledPanel)
        frameLeft.setFrameShadow(QtGui.QFrame.Raised)
        gridLayoutLeft = QtGui.QGridLayout(frameLeft)
        mainLayout.addWidget(frameLeft, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)

        frameRigth = QtGui.QFrame(mainWindow)
        frameRigth.setFrameShape(QtGui.QFrame.StyledPanel)
        frameRigth.setFrameShadow(QtGui.QFrame.Raised)
        gridLayoutRigth = QtGui.QGridLayout(frameRigth)
        mainLayout.addWidget(frameRigth, 0, 1, 1, 1, QtCore.Qt.AlignVCenter)

        tabs    = QtGui.QTabWidget()
        gridLayoutRigth.addWidget(tabs, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)

        button = QtGui.QPushButton('test')
        gridLayoutLeft.addWidget(button, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)

        # Create tabs
        tab1    = QtGui.QWidget()   
        tab2 = QtGui.QWidget()

        # Set layout of first tab
        vBoxlayout  = QtGui.QVBoxLayout()
        pushButton1 = QtGui.QPushButton("Start")
        pushButton2 = QtGui.QPushButton("Settings")
        pushButton3 = QtGui.QPushButton("Stop")
        vBoxlayout.addWidget(pushButton1)
        vBoxlayout.addWidget(pushButton2)
        vBoxlayout.addWidget(pushButton3)
        tab1.setLayout(vBoxlayout)   

        # Add tabs
        tabs.addTab(tab1, "Tab 1")
        tabs.addTab(tab2, "Tab 2")

        mainWindow.show()

        self.show()

    def close_application(self):
        print("Whooaa so custom!")
        sys.exit()

def run():
    app=QtGui.QApplication(sys.argv)
    GUI=Window()
    sys.exit(app.exec_())

run()

我的主要问题是,在这里,我有self.show(),但也有mainWindow.show()我不知道使用哪一个或如何消除一个,所以我只需要一个。

python tabs pyqt frame
1个回答
0
投票

让球滚动:

mainWindow.show() # not this, this is a "QWidget"
self.show() # use this, this is a "QMainWindow"

你想“展示”你刚刚实施的课程QMainWindow。此外,这个类需要你在mainwindow中正确收集的内容,所以使用setCentralWidget()

self.setCentralWidget(mainWindow)
self.show()

最后close_application()永远不会被调用,你可以将它连接到一个按钮:

pushButton3.clicked.connect(self.close_application)

或覆盖closingEvent:

def closeEvent ( self, QCloseEvent ) :
    print("Whooaa so custom, that is closing now!")
    #QCloseEvent.accept()
© www.soinside.com 2019 - 2024. All rights reserved.