RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1059927
Accepted
5478512
5478512
Asked:2020-12-17 23:57:25 +0000 UTC2020-12-17 23:57:25 +0000 UTC 2020-12-17 23:57:25 +0000 UTC

如果程序在 PyQt5 中有多个窗口,如何使用一个窗口?

  • 772

我有一个程序,其中有三个窗口。我不明白如何只使用一个窗口(显示文本,使用按钮)。

这是代码:main.py

from PyQt5 import QtCore, QtGui, QtWidgets

from ui_inst import Ui_Inst
from ui_uch import Ui_Uch                           

class Ui_Start(object):
    def setupUi(self, Ui_Start):
        Ui_Start.setObjectName("Ui_Start")
        Ui_Start.resize(443, 293)

        self.lineEdit1 = QtWidgets.QLineEdit(Ui_Start)
        self.lineEdit1.setGeometry(QtCore.QRect(50, 80, 321, 51))
        self.lineEdit1.setStyleSheet("")
        self.lineEdit1.setText("")
        self.lineEdit1.setObjectName("lineEdit")
        self.textEdit = QtWidgets.QTextEdit(Ui_Start)
        self.textEdit.setGeometry(QtCore.QRect(10, 30, 421, 41))
        self.textEdit.setObjectName("textEdit")
        self.pushButton_2 = QtWidgets.QPushButton(Ui_Start)
        self.pushButton_2.setGeometry(QtCore.QRect(150, 210, 141, 58))
        self.pushButton_2.setStyleSheet("font: 16pt \"Molot\";")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton = QtWidgets.QPushButton(Ui_Start)
        self.pushButton.setGeometry(QtCore.QRect(50, 140, 321, 58))
        self.pushButton.setStyleSheet("font: 20pt \"Molot\";")
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Ui_Start)
        QtCore.QMetaObject.connectSlotsByName(Ui_Start)

    def retranslateUi(self, Ui_Start):
        _translate = QtCore.QCoreApplication.translate
        Ui_Start.setWindowTitle(_translate("Ui_Start", "Ui_Start"))
        self.textEdit.setHtml(_translate("Ui_Start", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
        "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
        "p, li { white-space: pre-wrap; }\n"
        "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
        "<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:16pt;\">Введите номер и букву класса:</span></p></body></html>"))
        self.pushButton_2.setText(_translate("Ui_Start", "инструкция"))
        self.pushButton.setText(_translate("Ui_Start", "Найти класс"))


class MyInst(QtWidgets.QWidget, Ui_Inst):
    def __init__(self, parent=None):
        super(MyInst, self).__init__(parent)
        self.setupUi(self)
class MyUch(QtWidgets.QWidget, Ui_Uch):
    def __init__(self, parent=None):
        super(MyUch, self).__init__(parent)
        self.setupUi(self)       


class Main(QtWidgets.QWidget, Ui_Start):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

        self.pushButton_2.clicked.connect(self.onClicked)
        self.pushButton.clicked.connect(self.uch)

    def onClicked(self):
        self.inst = MyInst()
        self.inst.show()
    def uch(self):
        self.uch = MyUch()
        self.uch.show()
        self.lineEdit2.setText("1")

    



StyleSheet = '''
QPushButton {
    font: bold italic 16pt 'Comic Sans MS';
    background-color: silver;
    width: 75px ;
    height: 50px;
    border: none;
    text-align: center;
}
QPushButton:hover {
    background: #C9C0BB; 
}            
QPushButton:pressed {
    background-color: blue;
}

'''


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)

    app.setStyleSheet(StyleSheet)                          

    file = QtCore.QFile("dark.qss")                                
    file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text)
    stream = QtCore.QTextStream(file)
    app.setStyleSheet(stream.readAll())

    w = Main()
    w.show()

    sys.exit(app.exec_())

ui_inst.py:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Inst(object):
    def setupUi(self, Inst):
        Inst.setObjectName("Ui_Inst")
        Inst.resize(1111, 883)
        self.textEdit = QtWidgets.QTextEdit(Inst)                        
        self.textEdit.setGeometry(QtCore.QRect(0, 10, 1111, 41))
        self.textEdit.setObjectName("textEdit")
        self.text_inctru = QtWidgets.QTextEdit(Inst)
        self.text_inctru.setGeometry(QtCore.QRect(20, 60, 1071, 811))
        self.text_inctru.setObjectName("text_inctru")

        self.retranslateUi(Inst)
        QtCore.QMetaObject.connectSlotsByName(Inst)                      

    def retranslateUi(self, Inst):
        _translate = QtCore.QCoreApplication.translate
        Inst.setWindowTitle(_translate("Inst", "Inst"))
        self.textEdit.setHtml(_translate("Inst", "<!DOCTYPE HTML PUBLIC 
        \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC- 
        html40/strict.dtd\">\n"
        "<html><head><meta name=\"qrichtext\" content=\"1\" /><style 
        type=\"text/css\">\n"
        "p, li { white-space: pre-wrap; }\n"
        "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font- 
        size:8.25pt; font-weight:400; font-style:normal;\">\n"
        "<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; 
        margin-left:0px; margin-right:0px; -qt-block-indent:0; text- 
        indent:0px;\"><span style=\" font-size:22pt;\">Инструкция</span></p> 
        </body></html>"))

ui_uch.py​​ ​​​:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Uch(object):
    def setupUi(self, Uch):
        Uch.setObjectName("Uch")
        Uch.resize(718, 336)
        self.lineEdit = QtWidgets.QLineEdit(Uch)
        self.lineEdit.setGeometry(QtCore.QRect(60, 40, 611, 81))
        self.lineEdit.setStyleSheet("font: 20pt \"Molot\";")
        self.lineEdit.setObjectName("lineEdit1")
        self.pushButton = QtWidgets.QPushButton(Uch)
        self.pushButton.setGeometry(QtCore.QRect(190, 140, 341, 51))
        self.pushButton.setStyleSheet("font: 27pt \"Molot\";")
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Uch)
        self.pushButton_2.setGeometry(QtCore.QRect(60, 222, 601, 61))
        self.pushButton_2.setStyleSheet("font: 45pt \"Molot\";")
        self.pushButton_2.setObjectName("pushButton_2")
        self.label = QtWidgets.QLabel(Uch)
        self.label.setGeometry(QtCore.QRect(70, 0, 601, 41))
        self.label.setStyleSheet("font: 25pt \"Molot\";")
        self.label.setObjectName("label")

        self.retranslateUi(Uch)
        QtCore.QMetaObject.connectSlotsByName(Uch)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Uch", "Uch"))
        self.pushButton.setText(_translate("Uch", "Выбрать ученика"))
        self.pushButton_2.setText(_translate("Uch", "Задать вопрос"))
        self.label.setText(_translate("Uch", "Отвечает ученик:"))
python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2020-12-18T01:13:37Z2020-12-18T01:13:37Z

    您的主窗口是由一个类Main和您从Ui_Start.

    您可以像这样访问此主窗口中的对象:

    self.lineEdit1 ...    например, self.lineEdit1.text()
    self.textEdit ...     например, self.textEdit.setHtml("<b>Привет Мир</b>")
    self.pushButton_2 ... например, self.pushButton_2.clicked.connect(self.onClicked)
    self.pushButton ...   например, self.pushButton.clicked.connect(self.uch)
    

    当您单击该按钮时,self.pushButton_2您会创建另一个窗口,这将是self.inst. 您可以像这样访问此 (self.inst) 对象的属性:

    self.inst.textEdit ...
    self.inst.text_inctru ...
    

    当您单击该按钮时,self.pushButton您会创建另一个窗口,这将是self.uch. 您可以像这样访问此 (self.uch) 对象的属性:

    self.uch.lineEdit ...
    self.uch.pushButton ...
    self.uch.pushButton_2 ...
    self.uch.label ...
    

    这些其他窗口将是ОКНАМИ因为没有父窗口的小部件将是一个窗口。要显示这些 OTHER WINDOWS,您必须分别执行 self.inst.show()或self.uch.show()。


    from PyQt5 import QtCore, QtGui, QtWidgets
    from ui_inst import Ui_Inst
    from ui_uch import Ui_Uch                           
    
    class Ui_Start(object):
        def setupUi(self, Ui_Start):
            Ui_Start.setObjectName("Ui_Start")
            Ui_Start.resize(443, 293)
    
            self.lineEdit1 = QtWidgets.QLineEdit(Ui_Start)
            self.lineEdit1.setGeometry(QtCore.QRect(50, 80, 321, 51))
            self.lineEdit1.setStyleSheet("")
            self.lineEdit1.setText("")
            self.lineEdit1.setObjectName("lineEdit")
            self.textEdit = QtWidgets.QTextEdit(Ui_Start)
            self.textEdit.setGeometry(QtCore.QRect(10, 30, 421, 41))
            self.textEdit.setObjectName("textEdit")
            self.pushButton_2 = QtWidgets.QPushButton(Ui_Start)
            self.pushButton_2.setGeometry(QtCore.QRect(150, 210, 141, 58))
            self.pushButton_2.setStyleSheet("font: 16pt \"Molot\";")
            self.pushButton_2.setObjectName("pushButton_2")
            self.pushButton = QtWidgets.QPushButton(Ui_Start)
            self.pushButton.setGeometry(QtCore.QRect(50, 140, 321, 58))
            self.pushButton.setStyleSheet("font: 20pt \"Molot\";")
            self.pushButton.setObjectName("pushButton")
    
            self.retranslateUi(Ui_Start)
            QtCore.QMetaObject.connectSlotsByName(Ui_Start)
    
        def retranslateUi(self, Ui_Start):
            _translate = QtCore.QCoreApplication.translate
            Ui_Start.setWindowTitle(_translate("Ui_Start", "Ui_Start"))
            self.textEdit.setHtml(_translate("Ui_Start", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
            "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
            "p, li { white-space: pre-wrap; }\n"
            "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
            "<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:16pt;\">Введите номер и букву класса:</span></p></body></html>"))
            self.pushButton_2.setText(_translate("Ui_Start", "инструкция"))
            self.pushButton.setText(_translate("Ui_Start", "Найти класс"))
    
    
    class MyInst(QtWidgets.QWidget, Ui_Inst):
        def __init__(self, parent=None):
            super(MyInst, self).__init__(parent)
            self.setupUi(self)
    
    
    class MyUch(QtWidgets.QWidget, Ui_Uch):
        def __init__(self, parent=None):
            super(MyUch, self).__init__(parent)
            self.setupUi(self)       
    
    
    class Main(QtWidgets.QWidget, Ui_Start):
        def __init__(self, parent=None):
            super(Main, self).__init__(parent)
            self.setupUi(self)
    
            self.pushButton_2.clicked.connect(self.onClicked)
            self.pushButton.clicked.connect(self.uch)
    
        def onClicked(self):
            self.inst = MyInst()
            self.inst.show()
    
        def uch(self):
            self.uch = MyUch()
            self.uch.show()
    #        self.lineEdit2.setText("1")    # у вас нет объета `self.lineEdit2` !
            self.uch.lineEdit.setText(self.lineEdit1.text())                       # <<<-----<
    
    
    StyleSheet = '''
    QPushButton {
        font: bold italic 16pt 'Comic Sans MS';
        background-color: silver;
        width: 75px ;
        height: 50px;
        border: none;
        text-align: center;
    }
    QPushButton:hover {
        background: #C9C0BB; 
    }            
    QPushButton:pressed {
        background-color: blue;
    }
    
    '''
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
    
        app.setStyleSheet(StyleSheet)                          
    
        file = QtCore.QFile("dark.qss")                                
        file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text)
        stream = QtCore.QTextStream(file)
        app.setStyleSheet(stream.readAll())
    
        w = Main()
        w.show()
    
        sys.exit(app.exec_())
    

    在此处输入图像描述

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5