【问题标题】:Connecting several QT windows with a QPushButton使用 QPushButton 连接多个 QT 窗口
【发布时间】:2021-08-26 13:52:59
【问题描述】:

我正在做一个项目,我想为此开发 GUI。 我使用 Qt Designer 创建了 4 个不同的 UI 文件,并使用 PySide (pyside-uic) 将它们转换为 python 文件。 现在,我想创建一个主 python 文件来连接这些窗口。 我的软件结构如下:

1 - 欢迎窗口:按下按钮加载主窗口并关闭欢迎窗口 2 - 主窗口:它有 2 个按钮来打开我的第三个和第四个窗口。

我已阅读 this postthis one 的帖子,但我无法完成(我是新手)。

这是我的欢迎窗口代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'welcome_window_gui.ui'
#
# Created: Tue Mar 29 16:49:21 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.4

#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_welcome_window(object):
    def setupUi(self, welcome_window):
        welcome_window.setObjectName("welcome_window")
        welcome_window.resize(474, 300)
        self.verticalLayout_2 = QtGui.QVBoxLayout(welcome_window)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.welcomeLBL = QtGui.QLabel(welcome_window)
        self.welcomeLBL.setAlignment(QtCore.Qt.AlignCenter)
        self.welcomeLBL.setObjectName("welcomeLBL")
        self.verticalLayout_2.addWidget(self.welcomeLBL)
        self.welcomeBTN = QtGui.QPushButton(welcome_window)
        self.welcomeBTN.setObjectName("welcomeBTN")
        self.verticalLayout_2.addWidget(self.welcomeBTN)

        self.retranslateUi(welcome_window)
        QtCore.QObject.connect(self.welcomeBTN, QtCore.SIGNAL("clicked()"), welcome_window.close)
        QtCore.QMetaObject.connectSlotsByName(welcome_window)

    def retranslateUi(self, welcome_window):
        welcome_window.setWindowTitle(QtGui.QApplication.translate("welcome_window", "Welcome", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeLBL.setText(QtGui.QApplication.translate("welcome_window", "Welcome TO the Software", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeBTN.setText(QtGui.QApplication.translate("welcome_window", "Enter to Software", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    welcome_window = QtGui.QWidget()
    ui = Ui_welcome_window()
    ui.setupUi(welcome_window)
    welcome_window.show()
    sys.exit(app.exec_())

这是我的主窗口:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main_window_gui.ui'
#
# Created: Tue Mar 29 15:56:57 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_main_window(object):
    def setupUi(self, main_window):
        main_window.setObjectName("main_window")
        main_window.resize(643, 439)
        self.gridLayout_2 = QtGui.QGridLayout(main_window)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setContentsMargins(0, -1, -1, -1)
        self.gridLayout.setObjectName("gridLayout")
        self.TabWidget = QtGui.QTabWidget(main_window)
        self.TabWidget.setTabPosition(QtGui.QTabWidget.North)
        self.TabWidget.setIconSize(QtCore.QSize(16, 16))
        self.TabWidget.setObjectName("TabWidget")
        self.tab = QtGui.QWidget()
        self.tab.setObjectName("tab")
        self.gridLayoutWidget = QtGui.QWidget(self.tab)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 531, 331))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout_3 = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.TabWidget.addTab(self.tab, "")
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.TabWidget.addTab(self.tab_2, "")
        self.gridLayout.addWidget(self.TabWidget, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.LEDbtn = QtGui.QPushButton(main_window)
        self.LEDbtn.setObjectName("LEDbtn")
        self.gridLayout_2.addWidget(self.LEDbtn, 2, 3, 1, 1)
        self.calenderBTN = QtGui.QPushButton(main_window)
        self.calenderBTN.setObjectName("calenderBTN")
        self.gridLayout_2.addWidget(self.calenderBTN, 1, 3, 1, 1)

        self.retranslateUi(main_window)
        self.TabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(main_window)

    def retranslateUi(self, main_window):
        main_window.setWindowTitle(QtGui.QApplication.translate("main_window", "Software", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.tab), QtGui.QApplication.translate("main_window", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.tab_2), QtGui.QApplication.translate("main_window", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))
        self.LEDbtn.setText(QtGui.QApplication.translate("main_window", "LED", None, QtGui.QApplication.UnicodeUTF8))
        self.calenderBTN.setText(QtGui.QApplication.translate("main_window", "Calendar", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    main_window = QtGui.QWidget()
    ui = Ui_main_window()
    ui.setupUi(main_window)
    main_window.show()
    sys.exit(app.exec_())

有没有办法在 Main Python 代码中调用这些类?

谢谢。

【问题讨论】:

    标签: python qt pyside


    【解决方案1】:

    您可以简单地将它们导入您的主文件:

    from first_file import Ui_welcome_window
    from second_file import Ui_main_window
    

    然后你就可以像往常一样使用它们了 (注意,2个文件中的if __name__ == "__main__"部分没有被执行)

    制作一个按钮:

    #create the 2 windows
    main_window = QtGui.QWidget()
    ui = Ui_main_window()
    ui.setupUi(main_window)
    
    welcome_window = QtGui.QWidget()
    ui2 = Ui_welcome_window()
    ui2.setupUi(welcome_window)
    
    ui2.welcomeBTN.clicked.connect(main_window.show)
    

    当您单击welcome_window 上的按钮时,main_window 应为show()。对第 3 和第 4 个窗口执行相同操作!

    你好,MrP01

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2016-09-03
      • 2014-05-15
      • 1970-01-01
      相关资源
      最近更新 更多