I am new to Qt want to set an entry/lineEdit field value to a Label available in another frame which is defined in another class:
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_())
What I need to do is to set the lineEdit
(in class Dialog2
) value entered by the user to label_4
(in class Dialog3
):
def button_click(self):
# shost is a QString object
text = self.lineEdit.text()
print (text)
Question: How can I update Dialog3.label_4
after the user inputs the email?
Change the "on_timeout1" function
The problem with your code is that you need to somehow "communicate" dialog 2 with dialog 3. For this simple case I would recommend you to do the following:
Dialog2
to take as input a reference to an instance ofDialog3
.Dialog2.button_clicked
method update the instance ofDialog3
and hide the currentDialog2
instance."__main__"
part: showw1
andw2
but passw3
tow2
in the constructor (so w3 must be created first), eliminate the second timer.Below is a full implementation of what I described above. But let me finish with pointing out that this "communication" between UI elements is a more subtle and complex topic. So if you intend to do serious UI development I recommend you to start reading about the Model-View-Controller pattern, for example here.