文本从 LineEdit1 (Form1) 到 LineEdit2 (Form2)_Python, PyQt

Posted

技术标签:

【中文标题】文本从 LineEdit1 (Form1) 到 LineEdit2 (Form2)_Python, PyQt【英文标题】:Text from LineEdit1 (Form1) to LineEdit2 (Form2)_ Python, PyQt 【发布时间】:2016-09-30 12:49:34 【问题描述】:

我在主窗口中使用 LineEdit11 和 button1 创建了 MainWindow。我还使用 LineEdit 和按钮 2 创建了 V1 Form 2。

当我单击 Button1 时,Form 2 将显示。我在 lineEdit 中写了一个文本,我想通过单击 Button2 将文本传输到 form1 中的 LineEdit11。如何连接它们?

Python 3.5、PyQT 4

提前谢谢你

好的,所以我在 V1 课上写

def wyslij(self):
        self.lineEdit.setText(self.Ui_Form.lineEdit_11.text())

我得到错误:

'Ui_V1' 对象没有属性 'Ui_Form' 当然,我将 V1 类添加到 Ui_Form 中。 V1是第二个窗口,Ui_Form是主窗口。

这是一个完整的代码:

from PyQt4 import QtCore, QtGui
from V1 import Ui_V1
from V2 import Ui_V2
from V3 import Ui_V3

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)

# V1 window
class Ui_V1(object):
    def setupUi(self, V1):
        V1.setObjectName(_fromUtf8("V1"))
        V1.resize(400, 300)
        self.label = QtGui.QLabel(V1)
        ...
        self.pushButton.setText(_translate("V1", "Wyslij", None))
        self.pushButton_2.setText(_translate("V1", "Wyslij", None))
        self.pushButton.clicked.connect(lambda: self.wyslij())

    def wyslij(self):
        self.lineEdit.setText(self.Ui_Form.lineEdit_11.text())



# V2 okno
class Ui_V2(object):
    def setupUi(self, V2):
        V2.setObjectName(_fromUtf8("V2"))
        V2.resize(400, 300)
        self.label_4 = QtGui.QLabel(V2)
        ...
        self.pushButton.setText(_translate("V2", "Wyslij", None))
        self.label.setText(_translate("V2", "V2", None))
        self.label_3.setText(_translate("V2", "A", None))
        self.pushButton_2.setText(_translate("V2", "Wyslij", None))

# V3 okno
class Ui_V3(object):
    def setupUi(self, V3):
        V3.setObjectName(_fromUtf8("V3"))
        V3.resize(400, 300)
        self.label_4 = QtGui.QLabel(V3)
       ...
        self.pushButton.setText(_translate("V3", "Wyslij", None))
        self.label.setText(_translate("V3", "V3", None))
        self.label_3.setText(_translate("V3", "A", None))
        self.pushButton_2.setText(_translate("V3", "Wyslij", None))



# Main window!
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(728, 601)
        Form.setMinimumSize(QtCore.QSize(728, 601))
        self.label_6 = QtGui.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(169, 3, 401, 41))
        self.label_6.setObjectName(_fromUtf8("label_6"))...


    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label_6.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">TYGODNIOWA KONTROLA JAKOSCI</span></p></body></html>", None))
        self.label_8.setText(_translate("Form", "(dd-mm-rrrr)", None))...

        self.pushButton_2.clicked.connect(lambda: self.zapis_tyg()) # Do zapisu do tyg formularza
        self.pushButton_5.clicked.connect(lambda: self.openV1())  # Do pojawienia si eokna V1
        self.pushButton_6.clicked.connect(lambda: self.openV2())  # Do pojawienia si eokna V2
        self.pushButton_7.clicked.connect(lambda: self.openV3())  # Do pojawienia si eokna V3

    #zapis do pliku formularza tyg
    def zapis_tyg(self): # ZAPIS DO FORMULARZ TYGODNIOWEGO
         s = ""
         seq = (self.comboBox_6.currentText(), ".txt"); # This is sequence of strings. laczenie aby nazywał pliki wzaleznosci od aparatu
         a= s.join( seq )
         text_file = open( a , "a")
         text_file.write(self.lineEdit.text()+ "\t" ) #data
         text_file.write(self.lineEdit_2.text()+ "\t" ) #osoba
         text_file.write(self.comboBox_6.currentText() + "\t" ) #aparat
         text_file.write(self.comboBox.currentText() + "\t" ) #AKCESORIUM
         text_file.write(self.comboBox_2.currentText() + "\t" ) #ZAB ANTYK
         text_file.write(self.comboBox_3.currentText() + "\t" ) #iZO
         text_file.write(self.comboBox_4.currentText() + "\t" )# CENTRQTOR
         text_file.write(self.comboBox_5.currentText() + "\t" )# TELEMETR
         text_file.write(self.textEdit.toPlainText()+ "\n" ) # KOM MECH
         text_file.close()

    #function to show a new form
    def openV1(self):
        self.V1Window=QtGui.QMainWindow()
        self.ui= Ui_V1()
        self.ui.setupUi(self.V1Window)
        self.V1Window.show()

    def openV2(self):
        self.V2Window=QtGui.QMainWindow()
        self.ui= Ui_V2()
        self.ui.setupUi(self.V2Window)
        self.V2Window.show()

    def openV3(self):
        self.V3Window=QtGui.QMainWindow()
        self.ui= Ui_V3()
        self.ui.setupUi(self.V3Window)
        self.V3Window.show()

还有这条线:

def wyslij(self):
        self.lineEdit.setText(self.Ui_Form.lineEdit_11.text())

不能正常工作。

【问题讨论】:

如果你想要一个好的答案,你应该展示一些来 【参考方案1】:

没有任何代码,最简单的方法(但不是最好的)就是这样的:

def on_button2_click(self):
    self.form2.lineEdit2.setText(self.lineEdit1.text())

【讨论】:

以上是关于文本从 LineEdit1 (Form1) 到 LineEdit2 (Form2)_Python, PyQt的主要内容,如果未能解决你的问题,请参考以下文章

如何从另一个类访问 Winform 文本框控件?

C#从form2更改按钮文本

如何从Python中的文本中提取以字母A到L开头的单词?

将 Console.WriteLine() 重定向到文本框

将值从文本框传递到 SQL 查询中的“TOP n”子句

从 Windows Form1 访问 printQueue 类