我是Qt的新手,想将entry / lineEdit字段值设置为在另一个类中定义的另一个框架中可用的Label:
from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtCore import SIGNAL
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 Ui_Dialog1(object):
def setupUi(self, Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog2(object):
def setupUi(self, Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
self.pb = QtGui.QPushButton(self.frame_2)
self.pb.setObjectName(_fromUtf8("login"))
self.pb.setText("Login")
self.connect(self.pb, SIGNAL("clicked()"), self.button_click)
def button_click(self):
# shost is a QString object
text = self.lineEdit.text()
print (text)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog3(object):
def setupUi(self, Dialog):
self.frame_3 = QtGui.QFrame(Dialog)
self.frame_3.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
self.frame_3.setObjectName(_fromUtf8("frame_3"))
self.label_3 = QtGui.QLabel(self.frame_3)
self.label_3.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(self.frame_3)
self.label_4.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_3.setText(_translate("Dialog", "Email", None))
self.label_4.setText(_translate("Dialog", text, None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Dialog1(QtGui.QDialog, Ui_Dialog1):
def __init__(self, parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
class Dialog2(QtGui.QDialog, Ui_Dialog2):
def __init__(self, parent=None):
super(Dialog2, self).__init__(parent)
self.setupUi(self)
class Dialog3(QtGui.QDialog, Ui_Dialog3):
def __init__(self, parent=None):
super(Dialog3, self).__init__(parent)
self.setupUi(self)
if __name__=="__main__":
import sys
a = QtGui.QApplication(sys.argv)
w1 = Dialog1()
w2 = Dialog2()
w3 = Dialog3()
def on_timeout():
w1.hide()
w2.show()
def on_timeout1():
w2.hide()
w3.show()
w1.show()
QtCore.QTimer.singleShot(3000, on_timeout)
QtCore.QTimer.singleShot(6000, on_timeout1)
sys.exit(a.exec_())
我需要做的是将用户输入的lineEdit
(在Dialog2
类中)的值设置为label_4
(在Dialog3
类中):
def button_click(self):
# shost is a QString object
text = self.lineEdit.text()
print (text)
问题:用户输入电子邮件后,如何更新Dialog3.label_4
?
答案 0 :(得分:2)
更改“ on_timeout1”功能
def on_timeout1():
w2.hide()
w3.show()
w3.label_4.setText(w2.lineEdit.text())
答案 1 :(得分:1)
代码的问题是,您需要以某种方式“对话”对话框2与对话框3。对于这种简单的情况,我建议您执行以下操作:
Dialog2
的构造函数以将对Dialog3
实例的引用作为输入。Dialog2.button_clicked
方法中,更新Dialog3
的实例并隐藏当前的Dialog2
实例。"__main__"
部分的登录名:在构造函数中显示w1
和w2
,但将 pass w3
更改为w2
(因此必须先创建w3),省去第二个计时器。下面是我上面描述的完整实现。但是,我最后要指出,UI元素之间的这种“交流”是一个更为微妙和复杂的话题。因此,如果您打算进行认真的UI开发,我建议您开始阅读有关Model-View-Controller模式的信息,例如here。
from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtCore import SIGNAL
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 Ui_Dialog1(object):
def setupUi(self, Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog2(object):
def setupUi(self, Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
self.pb = QtGui.QPushButton(self.frame_2)
self.pb.setObjectName(_fromUtf8("login"))
self.pb.setText("Login")
self.connect(self.pb, SIGNAL("clicked()"), self.button_click)
def button_click(self):
text = self.lineEdit.text()
self.w3.label_4.setText(text)
self.w3.show()
self.hide()
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog3(object):
def setupUi(self, Dialog):
self.frame_3 = QtGui.QFrame(Dialog)
self.frame_3.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
self.frame_3.setObjectName(_fromUtf8("frame_3"))
self.label_3 = QtGui.QLabel(self.frame_3)
self.label_3.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(self.frame_3)
self.label_4.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_3.setText(_translate("Dialog", "Email", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Dialog1(QtGui.QDialog, Ui_Dialog1):
def __init__(self, parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
class Dialog2(QtGui.QDialog, Ui_Dialog2):
def __init__(self, w3, parent=None):
super(Dialog2, self).__init__(parent)
self.setupUi(self)
self.w3 = w3
class Dialog3(QtGui.QDialog, Ui_Dialog3):
def __init__(self, parent=None):
super(Dialog3, self).__init__(parent)
self.setupUi(self)
if __name__=="__main__":
import sys
a = QtGui.QApplication(sys.argv)
w1 = Dialog1()
w3 = Dialog3()
w2 = Dialog2(w3)
def on_timeout():
w1.hide()
w2.show()
w1.show()
QtCore.QTimer.singleShot(3000, on_timeout)
sys.exit(a.exec_())