PDA

View Full Version : PyQT copy text from form1 to form2



logilink
3rd October 2016, 09:27
Dear Friends. I wrote a program that have Form and Form2 called V1. V1 will show after cliked button in Form1.
In V1 I have a LineEdit and button. When i wrote something in LineEdit in V1 I want send this text to LineEdit in Form1.


from PyQt4 import QtCore, QtGui
from V1 import Ui_V1


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.tex t())




# 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()) # v1 window


#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()



I had an error:

'Ui_V1' object has no attribute 'Ui_Form'
Where is the problem?
Thank you

anda_skoa
6th October 2016, 16:17
First, this looks like you have edited the generated class instead of just using it.

Second, as Python tells you, your Ui_V1 class does not have any member called Ui_Form.

Cheers,
_

logilink
7th October 2016, 07:40
Thanks for reply

I am beginner in python and in GUI :) so that why i am writinig in code in this class instead of creating second file only with functions.

so how to conect Ui_V1 wiht Ui_Form ? Because i coonected button with Ui_V1 but i cant connect button with Labels...

anda_skoa
7th October 2016, 10:27
If you want to access "self.Ui_Form" then you need to initizalize this.

Something like


self.Ui_Form = Ui_Form()


Cheers,
_

logilink
7th October 2016, 14:35
Ok so I have done it :


def wyslij(self):
self.kopiaWindow=QtGui.QMainWindow()
self.Ui_Form=Ui_Form()
self.Ui_Form.lineEdit.text()=self.lineEdit.text()

and i have an error:

SyntaxError: can't assign to function call

thanks.

anda_skoa
7th October 2016, 18:12
Yes, last line of your new code.

You are trying to assign to the function text()
You probably want to assign to the property text instead.

Cheers,
_

logilink
10th October 2016, 08:04
So how i cantransfer text from lineEdit (V1 Form) to lineEdit ( Ui_form)?

Thanks

anda_skoa
10th October 2016, 08:37
Either by assiging to the target's text property or by calling the target's setText function.

Cheers,
_

logilink
10th October 2016, 10:57
could you show me an example how to do this?
Thanks

something like this?

self.Ui_Form.lineEdit.text(self.lineEdit.text())

Error:

AttributeError: 'Ui_Form' object has no attribute 'lineEdit'

This error is showing when I paste V1 class in MainFile.py but when I separate in two files (of course with import V1/Import MainFile)


self.Ui_Form=Ui_Form()
NameError: name 'Ui_Form' is not defined

thanks

anda_skoa
10th October 2016, 12:09
could you show me an example how to do this?

Maybe you should consider doing a Python tutorial first if you are unclear about assignments or method calls.



something like this?

No, QLineEdit does not have a method called "text" that also has arguments.
Why would you even try that?



Error:

Which is a different error all together, you are trying to access an object that doesn't exist.
Python is sometimes doing things that are automagically correct, but even Python can't magically create objects.

Cheers,
_