在另一个类的QtGui类中调用小部件

时间:2019-01-31 15:02:55

标签: python qtgui

要使代码更具可读性,我想在我的GuiSetUp类中设置我的小部件,然后使用我的小部件(例如按钮)在MainWindow类中初始化我的GUI,以便它们可以调用必要的函数。我在下面编写了脚本代码。如您所见,我不确定如何使用MainWindow显示GUI,然后调用按钮将其分配给小部件。

import sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

class MainWindow(QtGui.QMainWindow):
def __init__(self):
    QtGui.QMainWindow.__init__(self, parent=None)

    #here I wanted to call on initilse my Gui here instead of using the 
    #"self.show()" at the bottom of my GuiSetUp class
    self.central_widget = QtGui.QStackedWidget()
    self.setCentralWidget(self.central_widget)

    #this problem probably also relates to teh first I cannot call my widgets
    #and assign them to appropriate functions
    self.login_widget = GuiSetUp(self)
    self.login_widget.button1.clicked.connect(sayHello)

def sayHello():
    print('hello')


class GuiSetUp(QtGui.QWidget):

def __init__(self, parent=None):
    QtGui.QWidget.__init__(self)

    hbox = QtGui.QHBoxLayout(self)

    L_layout = QtGui.QGridLayout()
    R_layout = QtGui.QGridLayout()

    L_widgets = QtGui.QWidget()
    L_widgets.setLayout(L_layout)

    R_widgets = QtGui.QWidget()
    R_widgets.setLayout(R_layout)

    button1 = QtGui.QPushButton('button1')
    button2 = QtGui.QPushButton('button2')

    bottom = QtGui.QFrame()
    bottom.setFrameShape(QtGui.QFrame.StyledPanel)

    L_layout.addWidget(button1, 0, 0, 1, 2 )
    L_layout.addWidget(button2, 1, 0, 1, 2 )
    L_layout.setAlignment(QtCore.Qt.AlignTop|QtCore.Qt.AlignCenter)


    splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
    splitter1.addWidget(L_widgets)

    splitter2 = QtGui.QSplitter(QtCore.Qt.Horizontal)
    splitter2.addWidget(splitter1)
    splitter2.addWidget(bottom)

    hbox.addWidget(splitter2)


    self.setGeometry(300, 300, 300, 200)
    self.setWindowTitle('QSplitter demo')

    self.setLayout(hbox)
    #self.show()

def main():
app = QtGui.QApplication(sys.argv)
ex = GuiSetUp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

0 个答案:

没有答案