zoukankan      html  css  js  c++  java
  • pyqt 多窗口跳转

    今天在做pyqt5的多页面跳转时遇到问题,一点击button按钮,程序会崩溃。在网上查了下,应该是当窗口A调用窗口B的时候,两个窗口不能是同一类型。我写的时候把A、B同时写成了QWidget。把窗口B改成Dialog类型后,就可以成功调用了;如果想关联更多窗口,方法相似即可。

    大概的模板如下:

    class A(QWidget):         
        def __init__(self):             
            ...省略...             
            self.btn = QPushButton('跳转按钮')         
        def initUI(self):             
            pass     
     
    class B(QDialog):         
        def __init__(self):             
            pass
        def initUI(self):             
            pass     
        
    if __name__ == '__main__':         
        app = QApplication(sys.argv)         
        a = A()         
        b = B()         
        a.show()         
        a.btn.clicked.connect(b.show)         
    View Code

    具体实现:

    使用qt designer 设计一个主窗口(带有pushbutton按钮),一个子窗口(我这里是直接使用的Calendar部件)

    保存为相应的.ui文件,之后使用pyuic5 -x -o XXX.py XXX.ui生成相应的XXX.py 文件

    生成的主窗口文件:main_q.py(可直接运行):

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'C:UsersAdministratorDesktopmain_q.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.2
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(1131, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setGeometry(QtCore.QRect(-20, 10, 1121, 521))
            self.tabWidget.setObjectName("tabWidget")
            self.tab = QtWidgets.QWidget()
            self.tab.setObjectName("tab")
            self.label_2 = QtWidgets.QLabel(self.tab)
            self.label_2.setGeometry(QtCore.QRect(60, 50, 101, 16))
            self.label_2.setObjectName("label_2")
            self.label_3 = QtWidgets.QLabel(self.tab)
            self.label_3.setGeometry(QtCore.QRect(60, 100, 91, 16))
            self.label_3.setObjectName("label_3")
            self.label_4 = QtWidgets.QLabel(self.tab)
            self.label_4.setGeometry(QtCore.QRect(60, 150, 101, 16))
            self.label_4.setObjectName("label_4")
            self.lineEdit = QtWidgets.QLineEdit(self.tab)
            self.lineEdit.setGeometry(QtCore.QRect(202, 50, 171, 20))
            self.lineEdit.setObjectName("lineEdit")
            self.lineEdit_2 = QtWidgets.QLineEdit(self.tab)
            self.lineEdit_2.setGeometry(QtCore.QRect(202, 100, 171, 21))
            self.lineEdit_2.setObjectName("lineEdit_2")
            self.lineEdit_3 = QtWidgets.QLineEdit(self.tab)
            self.lineEdit_3.setGeometry(QtCore.QRect(202, 150, 171, 20))
            self.lineEdit_3.setObjectName("lineEdit_3")
            self.pushButton = QtWidgets.QPushButton(self.tab)
            self.pushButton.setGeometry(QtCore.QRect(120, 240, 75, 41))
            self.pushButton.setObjectName("pushButton")
            self.textEdit = QtWidgets.QTextEdit(self.tab)
            self.textEdit.setGeometry(QtCore.QRect(530, 10, 571, 381))
            self.textEdit.setObjectName("textEdit")
            self.tabWidget.addTab(self.tab, "")
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.groupBox = QtWidgets.QGroupBox(self.tab_2)
            self.groupBox.setGeometry(QtCore.QRect(10, 10, 161, 171))
            self.groupBox.setObjectName("groupBox")
            self.layoutWidget = QtWidgets.QWidget(self.groupBox)
            self.layoutWidget.setGeometry(QtCore.QRect(10, 40, 91, 62))
            self.layoutWidget.setObjectName("layoutWidget")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget)
            self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.radioButton = QtWidgets.QRadioButton(self.layoutWidget)
            self.radioButton.setObjectName("radioButton")
            self.verticalLayout_2.addWidget(self.radioButton)
            self.radioButton_2 = QtWidgets.QRadioButton(self.layoutWidget)
            self.radioButton_2.setObjectName("radioButton_2")
            self.verticalLayout_2.addWidget(self.radioButton_2)
            self.radioButton_3 = QtWidgets.QRadioButton(self.layoutWidget)
            self.radioButton_3.setObjectName("radioButton_3")
            self.verticalLayout_2.addWidget(self.radioButton_3)
            self.groupBox_2 = QtWidgets.QGroupBox(self.tab_2)
            self.groupBox_2.setGeometry(QtCore.QRect(199, 19, 351, 161))
            self.groupBox_2.setObjectName("groupBox_2")
            self.layoutWidget1 = QtWidgets.QWidget(self.groupBox_2)
            self.layoutWidget1.setGeometry(QtCore.QRect(70, 20, 261, 102))
            self.layoutWidget1.setObjectName("layoutWidget1")
            self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget1)
            self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.dial = QtWidgets.QDial(self.layoutWidget1)
            self.dial.setObjectName("dial")
            self.horizontalLayout.addWidget(self.dial)
            self.lcdNumber = QtWidgets.QLCDNumber(self.layoutWidget1)
            self.lcdNumber.setObjectName("lcdNumber")
            self.horizontalLayout.addWidget(self.lcdNumber)
            self.fontComboBox = QtWidgets.QFontComboBox(self.tab_2)
            self.fontComboBox.setGeometry(QtCore.QRect(90, 220, 165, 22))
            self.fontComboBox.setObjectName("fontComboBox")
            self.label = QtWidgets.QLabel(self.tab_2)
            self.label.setGeometry(QtCore.QRect(90, 300, 251, 91))
            self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.label.setAlignment(QtCore.Qt.AlignCenter)
            self.label.setObjectName("label")
            self.progressBar = QtWidgets.QProgressBar(self.tab_2)
            self.progressBar.setGeometry(QtCore.QRect(40, 440, 451, 23))
            self.progressBar.setProperty("value", 24)
            self.progressBar.setObjectName("progressBar")
            self.tabWidget.addTab(self.tab_2, "")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 1131, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            self.tabWidget.setCurrentIndex(0)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.label_2.setText(_translate("MainWindow", "请输入开始时间:"))
            self.label_3.setText(_translate("MainWindow", "请输入结束时间:"))
            self.label_4.setText(_translate("MainWindow", "请输入k值:"))
            self.pushButton.setText(_translate("MainWindow", "开始查询"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "选项卡1"))
            self.groupBox.setTitle(_translate("MainWindow", "功能选项"))
            self.radioButton.setText(_translate("MainWindow", "RadioButton"))
            self.radioButton_2.setText(_translate("MainWindow", "RadioButton"))
            self.radioButton_3.setText(_translate("MainWindow", "RadioButton"))
            self.groupBox_2.setTitle(_translate("MainWindow", "移动刻度盘"))
            self.label.setText(_translate("MainWindow", "TextLabel"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "选项卡2"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    main_q

    生成的子窗口文件calendars.py(可直接运行:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_calendar(object):
        def setupUi(self, calendar):
            calendar.setObjectName("calendar")
            calendar.resize(277, 247)
            self.calendarWidget = QtWidgets.QCalendarWidget(calendar)
            self.calendarWidget.setGeometry(QtCore.QRect(-3, -4, 281, 251))
            self.calendarWidget.setObjectName("calendarWidget")
    
            self.retranslateUi(calendar)
            QtCore.QMetaObject.connectSlotsByName(calendar)
    
        def retranslateUi(self, calendar):
            _translate = QtCore.QCoreApplication.translate
            calendar.setWindowTitle(_translate("calendar", "Dialog"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        calendar = QtWidgets.QDialog()
        ui = Ui_calendar()
        ui.setupUi(calendar)
        calendar.show()
        sys.exit(app.exec_())
    calendars

    通过生成以上两个文件之后,编写一个文件,将两个窗体关联

    from GUI_pyqt.main_q import Ui_MainWindow
    from GUI_pyqt.calendars import Ui_calendar
    
    from PyQt5.QtWidgets import QApplication,QMainWindow,QDialog
    import sys
    
    class ParentWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.main_ui = Ui_MainWindow()
            self.main_ui.setupUi(self)
    
    
    class ChildWindow(QDialog):
        def __init__(self):
            super(ChildWindow,self).__init__()
            self.calendars = Ui_calendar()
            self.calendars.setupUi(self)
    
    if __name__ =="__main__":
        app = QApplication(sys.argv)
        window = ParentWindow()
        child = ChildWindow()
    
        #通过按钮将两个窗体关联
        btn = window.main_ui.pushButton
        btn.clicked.connect(child.show)
    
        #显示
        window.show()
        sys.exit(app.exec_())
    pyqt_main

    完成,接下来可以让窗口返回数据,或者更多的操作。

    参考:https://blog.csdn.net/weixin_39449466/article/details/81008711

  • 相关阅读:
    cookie简介
    Javaweb
    apache-tomcat-7.0.92
    学javaweb 先学Servlet 应用理论很重要
    DataGuard切换(主库为Rac+备库为Rac)
    Oracle 迁移 序列
    oracle SQL 执行进度
    ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
    盘点 Oracle 11g 中新特性带来的10大性能影响
    查询近一个月的表空间使用情况
  • 原文地址:https://www.cnblogs.com/mxhmxh/p/9586074.html
Copyright © 2011-2022 走看看