• 2252阅读
  • 1回复

请教!PYQT两个窗口间的控件如何相互访问 [复制链接]

上一主题 下一主题
离线lason123
 

只看楼主 倒序阅读 楼主  发表于: 2018-07-26
关键词: QT4界面窗口

坛里的各位高手,请教PyQt两个窗口间控件相互访问的问题,如上图,
form1里有两个按钮,Btn1和Btn2,当点击form1中Btn2的时候,会跳出form2面板,form2中有个按钮Btn3
我的想法是:当form2中Btn3被点击的时候,form1中的Btn1按钮设置使能设为False
困难点:当弹出form2时,点击Btn3,如何把这个消息告知Form1类,换句话说,Btn3如何访问Form1类,子类如何访问父类

怎么写,谢谢大家了

form1的界面函数如下:

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

# Form implementation generated from reading ui file 'UI1.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from testui2 import ui2   #导入界面2的类

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class ui1(QtGui.QWidget):
    def __init__(self, ):
        super(ui1, self).__init__()

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(794, 591)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(100, 180, 181, 111))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.setStyleSheet(_fromUtf8("background-color: rgb(85, 255, 0);"))
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(360, 180, 181, 111))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.setStyleSheet(_fromUtf8("background-color: rgb(85, 255, 0);"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.showUI2)

    def showUI2(self):
        self.showui2 = ui2()
        self.showui2.show()

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form1", None))
        self.pushButton.setText(_translate("Form", "Btn1", None))
        self.pushButton_2.setText(_translate("Form", "Btn2", None))

def main():
    import sys
    app = QtGui.QApplication(sys.argv)
    widget = QtGui.QWidget()
    ui = ui1()
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())
    pass
if __name__ == '__main__':
    main()

form2的界面类函数(QT 设计师生成的代码):
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'UI2.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)


class ui2(QtGui.QWidget):
    def __init__(self):
        super(ui2, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)
        # self.new = Main_GUI()
        # self.setupUi(self)
        self.flag=False

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(794, 591)
        self.form = Form
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(100, 180, 181, 111))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.setStyleSheet(_fromUtf8("background-color: rgb(85, 255, 0);"))

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form2", None))
        self.pushButton.setText(_translate("Form", "Btn3", None))
离线linker

只看该作者 1楼 发表于: 2018-07-27
信号槽。
话说有必要发两个贴吗?
做土豆长大的孩纸
快速回复
限100 字节
 
上一个 下一个