Unable to detect undefined names - pyqt4

New to QtDesigner and PyQt and trying to get my first simple application going. (The user enters anywhere from one to three values in LineEdits and the sum is put in a fourth LineEdit). I created the GUI (simpleAdder1.py) with Qt Designer and then wrote the following code (callsimpleAdder1.pyw). Unfortunately it's not running and code analysis suggests a problem at the 'from simpleAdder1 import *' line. The problem is ...."from simpleAdder1 import * ,used, unable to detect undefined names'
I originally thought it was a path problem. But simpleAdder1.py is in the same directory as callsimpleadder1.pyw. I also copied simpleAdder1 into one of the paths that Python checks and that didn't help.
Where am I going wrong ? What names are undefined ? How do I fix this ?
import sys
from simpleAdder1 import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui=Ui_Dialog()
self.ui.setupUi(self)
def on_v1Input_textChanged(self):
self.ui.calcResult()
def on_v2Input_textChanged(self):
self.ui.calcResult()
def on_v3Input_textChanged(self):
self.ui.calcResult()
def calcResult(self):
if len(self.ui.v1Input.txt())!=0:
a=float(self.ui.v1Input.txt())
else:
a=0
if len(self.ui.v2Input.txt())!=0:
b=float(self.ui.v2Input.txt())
else:
b=0
if len(self.ui.v3Input.txt())!=0:
c=float(self.ui.v1Input.txt())
else:
c=0
sum=a+b+c
self.ui.calc_result.setText(+str(sum))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp=MyForm()
myapp.show
app.exec_()
The code for the GUI (simpleAdder1) is as follows:
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 Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(673, 565)
self.v1Input = QtGui.QLineEdit(Dialog)
self.v1Input.setGeometry(QtCore.QRect(50, 70, 71, 20))
self.v1Input.setObjectName(_fromUtf8("v1Input"))
self.v2Input = QtGui.QLineEdit(Dialog)
self.v2Input.setGeometry(QtCore.QRect(150, 70, 71, 20))
self.v2Input.setObjectName(_fromUtf8("v2Input"))
self.v3Input = QtGui.QLineEdit(Dialog)
self.v3Input.setGeometry(QtCore.QRect(250, 70, 71, 20))
self.v3Input.setObjectName(_fromUtf8("v3Input"))
self.calc_result = QtGui.QLineEdit(Dialog)
self.calc_result.setGeometry(QtCore.QRect(420, 70, 113, 20))
self.calc_result.setObjectName(_fromUtf8("calc_result"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(60, 50, 46, 13))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(160, 50, 46, 13))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(260, 50, 46, 13))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(450, 50, 46, 13))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(200, 230, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v1Input.clear)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v2Input.clear)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v3Input.clear)
QtCore.QMetaObject.connectSlotsByName(Dialog)
#QtCore.QObject.connect(self.v1Input,QtCore.SIGNAL("textChanged"),self.calcResult)
#QtCore.QObject.connect(self.v2Input,QtCore.SIGNAL("textChanged"),self.calcResult)
#QtCore.QObject.connect(self.v3Input,QtCore.SIGNAL("textChanged"),self.calcResult)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "Val 1", None))
self.label_2.setText(_translate("Dialog", "Val 2", None))
self.label_3.setText(_translate("Dialog", "Val 3", None))
self.label_4.setText(_translate("Dialog", "Result", None))
self.pushButton.setText(_translate("Dialog", "Clear Inputs", None))

OK, I think I (maybe) understand your problem.
Unfortunately it's not running and code analysis suggests a problem at the 'from simpleAdder1 import *' line. The problem is ...."from simpleAdder1 import * ,used, unable to detect undefined names'
I see your code the problem widget can't show isn't this error I think error this your path file.
But, I use PyDev 2.8.2 on Eclipse Kepler Service Release 1, It have (little) warning unused import like this,
Unused in wild import: QtCore
Found at: simpleAdder1
Then, I suggest to use "used import" only, like this;
from simpleAdder1 import Ui_Dialog, QtGui
And what is going wrong your GUI ? It's because this line in main;
myapp.show
To solve problem, please "Call function" (In old isn't call function anything) like this;
myapp.show()
Regards,

Related

How to get imported class to change attribute of main.py label stylesheet [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using PyQt5 to create a gui, defining a buttons class in a separate .py file and importing it, then trying to use a method from my buttons class to change the color of a label in my Ui_MainWindow which is my gui class in main.py
I have isolated this code from a larger project which outlines what I am attempting to accomplish.
Here is my buttons class which is saved and imported as buttons:
class buttons(object):
def __init__(self, Ui_MainWindow):
print('creating buttons object')
def func(self):
Ui_MainWindow.label.setStyleSheet("background-color: green;")
Here is my main.py with my Ui_MainWindow class which constructs the gui:
from PyQt5 import QtCore, QtGui, QtWidgets
import buttons
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 228)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Push button created
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: (btn_obj.func()))
self.pushButton.setGeometry(QtCore.QRect(0, 70, 131, 81))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 50, 121, 101))
self.label.setStyleSheet("background-color: rgb(255, 0, 4);")
self.label.setText("")
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# CREATE A BUTTONS OBJECT SO WE CAN CALL ITS METHOD
btn_obj = buttons.buttons(self)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is throwing the error:
Traceback (most recent call last):
File "c:\Users\Gaming\Google Drive\Code\Python\help\main.py", line 13, in <lambda>
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: (btn_obj.func()))
File "c:\Users\Gaming\Google Drive\Code\Python\help\buttons.py", line 6, in func
Ui_MainWindow.label.setStyleSheet("background-color: green;")
NameError: name 'Ui_MainWindow' is not defined
If I understand correctly, since the label in question is an attribute of a Ui_MainWindow object, and not the class itself it is unable to be changed with my current code. I am hoping someone could show/tell me the proper way to implement this sort of thing. Thank you in advance.
Change:
class buttons(object):
def __init__(self, Ui_MainWindow):
print('creating buttons object')
def func(self):
Ui_MainWindow.label.setStyleSheet("background-color: green;")
To:
class buttons(object):
def __init__(self, Ui_MainWindow):
print('creating buttons object')
self.Ui_MainWindow = Ui_MainWindow
def func(self):
self.Ui_MainWindow.label.setStyleSheet("background-color: green;")

When running python script to import code to invoke the UI design and display i have no errors but i dont get a display

Good day, Please can someone assist, i have to add 2 calendars to my U.I, a birthday calendar and a 2019 calendar.
When i run the python script i don't receive any errors, nothing happens. It doesn't display the U.I design.
The first code is the .ui converted to .py and the code thereafter is the code to invoke and display the U.I Design, but nothing happens, please can someone help me, i am new to Python and i am really struggling.
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 Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(704, 580)
self.calendarWidget = QtGui.QCalendarWidget(Dialog)
self.calendarWidget.setGeometry(QtCore.QRect(190, 10, 280, 155))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Script MT Bold"))
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.calendarWidget.setFont(font)
self.calendarWidget.setObjectName(_fromUtf8("calendarWidget"))
self.calendarWidget_2 = QtGui.QCalendarWidget(Dialog)
self.calendarWidget_2.setGeometry(QtCore.QRect(190, 220, 280, 155))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Script MT Bold"))
font.setBold(True)
font.setWeight(75)
self.calendarWidget_2.setFont(font)
self.calendarWidget_2.setSelectedDate(QtCore.QDate(2019, 3, 27))
self.calendarWidget_2.setObjectName(_fromUtf8("calendarWidget_2"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 220, 101, 20))
self.label.setObjectName(_fromUtf8("label"))
self.dateEdit = QtGui.QDateEdit(Dialog)
self.dateEdit.setGeometry(QtCore.QRect(280, 380, 91, 21))
self.dateEdit.setDateTime(QtCore.QDateTime(QtCore.QDate(2019, 3, 27), QtCore.QTime(0, 0, 0)))
self.dateEdit.setDate(QtCore.QDate(2019, 3, 27))
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.dateEdit_2 = QtGui.QDateEdit(Dialog)
self.dateEdit_2.setGeometry(QtCore.QRect(280, 170, 91, 21))
self.dateEdit_2.setDate(QtCore.QDate(2019, 2, 22))
self.dateEdit_2.setObjectName(_fromUtf8("dateEdit_2"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(10, 20, 71, 20))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(530, 510, 141, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(530, 530, 111, 16))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(280, 430, 91, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label_5 = QtGui.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(10, 380, 181, 16))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_6 = QtGui.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(10, 460, 81, 16))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(280, 460, 91, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_7 = QtGui.QLabel(Dialog)
self.label_7.setGeometry(QtCore.QRect(10, 170, 91, 16))
self.label_7.setObjectName(_fromUtf8("label_7"))
self.label_8 = QtGui.QLabel(Dialog)
self.label_8.setGeometry(QtCore.QRect(110, 170, 51, 16))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.label_9 = QtGui.QLabel(Dialog)
self.label_9.setGeometry(QtCore.QRect(190, 380, 46, 13))
self.label_9.setObjectName(_fromUtf8("label_9"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "Birthday Calendar", None))
self.dateEdit.setDisplayFormat(_translate("Dialog", "d/m/yyyy", None))
self.dateEdit_2.setDisplayFormat(_translate("Dialog", "d/m/yyyy", None))
self.label_2.setText(_translate("Dialog", "2019 Calendar", None))
self.label_3.setText(_translate("Dialog", "Marelize Jansen van Vuuren", None))
self.label_4.setText(_translate("Dialog", "Student no: 60858753", None))
self.pushButton.setText(_translate("Dialog", "Calculate Age", None))
self.label_5.setText(_translate("Dialog", "Select DOB and Calculate exact age", None))
self.label_6.setText(_translate("Dialog", "Current Age", None))
self.label_7.setText(_translate("Dialog", "Select today\'s date", None))
self.label_8.setText(_translate("Dialog", "D/MM/YYY", None))
self.label_9.setText(_translate("Dialog", "D/MM/YY", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
This is the code to invoke and display the user interface
import sys
from dispcalendar import*
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.calendarWidget, QtCore.SIGNAL('selectionChanged()'), self.dispdate)
QtCore.QObject.connect(self.ui.calendarWidget_2, QtCore.SIGNAL('selectionChanged()'), self.dispdate)
self.dispdate()
def dispdate(self):
self.ui.dateEdit_2.setDate(self.ui.calendarWidget.selectedDate())
self.ui.dateEdit.setDate(self.ui.calendarWidget_2.selectedDate())
self.ui.dateEdit_2.display(text)
self.ui.dateEdit.display(text)
self.ui.calendarWidget.display(text)
self.ui.calendarWidget_2.display(text)
if __name__=="__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())

How to Make a Button Perform Single Click Instead of Double Click

I'm experimenting on PyQt5. When I enter a value and click the push button to pass the value entered on my line edit, I noticed that it requires 2 mouse clicks for it to pass, and display the value on my plain text edit.
How will I change my codes to allow the entered value on the line edit to be passed by just 1 click?
from PyQt5 import QtCore, QtGui, QtWidgets
from functools import partial
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 480)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 80, 151, 31))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(190, 90, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(40, 170, 181, 16))
self.label_2.setObjectName("label_2")
self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit.setGeometry(QtCore.QRect(220, 170, 104, 71))
self.plainTextEdit.setReadOnly(True)
self.plainTextEdit.setObjectName("plainTextEdit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(310, 90, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.Pass)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "Pass the value entered here:"))
self.label_2.setText(_translate("Form", "Accept and display the passed value:"))
self.pushButton.setText(_translate("Form", "PASS"))
def Pass(self):
accept = self.lineEdit.text()
self.pushButton.clicked.connect(partial(self.Get, accept))
def Get(self, getA):
self.plainTextEdit.setPlainText(getA)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
ui.Get(ui.Pass())
sys.exit(app.exec_())
.clicked fires the first time you click the button. The problem is however that you register Pass to the button:
self.pushButton.clicked.connect(self.Pass)
Now Pass is defined as:
def Pass(self):
accept = self.lineEdit.text()
# connect a new event
self.pushButton.clicked.connect(partial(self.Get, accept))
So the first time you click the button, you read the accept, and only connect the self.Get method to the button such that the next time you hit the button, self.Get will indeed be called.
In order to process the request straight away, you can simply modify the Pass method to:
def Pass(self):
accept = self.lineEdit.text()
self.Get(accept)

pyQt: how to pass information between windows

I have two windows, both containing one button and one lineEdit. I want to create a "ping - pong" communication between both windows. At first, I write something in the lineEdit of the first window, press the button, and a second window appears.
I want the message written in the lineEdit of the first window to appear to the lineEdit of the second window. (and vice versa).
this is the code for the creation of the First window, derived from Qt Designer:
# -*- coding: utf-8 -*-
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 Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(331, 249)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.PushButtonFirst = QtGui.QPushButton(self.centralwidget)
self.PushButtonFirst.setGeometry(QtCore.QRect(140, 180, 131, 27))
self.PushButtonFirst.setObjectName(_fromUtf8("PushButtonFirst"))
self.lineEditFirst = QtGui.QLineEdit(self.centralwidget)
self.lineEditFirst.setGeometry(QtCore.QRect(130, 50, 113, 27))
self.lineEditFirst.setObjectName(_fromUtf8("lineEditFirst"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionNew = QtGui.QAction(MainWindow)
self.actionNew.setObjectName(_fromUtf8("actionNew"))
self.actionOpen = QtGui.QAction(MainWindow)
self.actionOpen.setObjectName(_fromUtf8("actionOpen"))
self.actionClose = QtGui.QAction(MainWindow)
self.actionClose.setObjectName(_fromUtf8("actionClose"))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.PushButtonFirst.setText(_translate("MainWindow", "PushButtonFirst", None))
self.actionNew.setText(_translate("MainWindow", "New", None))
self.actionOpen.setText(_translate("MainWindow", "Open", None))
self.actionClose.setText(_translate("MainWindow", "Close", None))
this is the code for the creation of the Second window, derived from Qt Designer:
# -*- coding: utf-8 -*-
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 Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(329, 260)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.PushButtonSecond = QtGui.QPushButton(self.centralwidget)
self.PushButtonSecond.setGeometry(QtCore.QRect(130, 190, 121, 27))
self.PushButtonSecond.setObjectName(_fromUtf8("PushButtonSecond"))
self.lineEditSecond = QtGui.QLineEdit(self.centralwidget)
self.lineEditSecond.setGeometry(QtCore.QRect(120, 80, 113, 27))
self.lineEditSecond.setObjectName(_fromUtf8("lineEditSecond"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.PushButtonSecond.setText(_translate("MainWindow", "PushButtonSecond", None))
and this is the main code:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.PushButtonFirst.clicked.connect(self.on_PushButtonFirst_clicked)
self.dialog = Second(self)
def on_PushButtonFirst_clicked(self):
self.my_text_First = self.lineEditFirst.text()
pass_text(self)
self.dialog.show()
def pass_text(obj):
obj.lineEditSecond.setText('OK')
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I receive this message:
'First' object has no attribute 'lineEditSecond'
which is quite logical, since pass_text() is a function of the First class. Anyway, I can't think any workaround.
Any thought would be appreciated.
You pretty much got it working. When I get an attribute error and can't figure it out, I use print type(object) and print dir(object) as a first-line of debugging to double check that the object is what I think it is, and to inspect all of its attributes.
The problem is you were not passing the second dialog whose text you wanted to set. I fixed this, and made a few other minor changes to your First class:
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.PushButtonFirst.clicked.connect(self.on_PushButtonFirst_clicked)
self.partnerDialog = Second(self)
def on_PushButtonFirst_clicked(self):
self.partnerDialog.lineEditSecond.setText(self.lineEditFirst.text())
self.partnerDialog.show()
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
self.PushButtonSecond.clicked.connect(self.on_PushButtonSecond_clicked)
self.partnerDialog = parent #otherwise, recursion
def on_PushButtonSecond_clicked(self):
self.partnerDialog.lineEditFirst.setText(self.lineEditSecond.text())
self.partnerDialog.show()
I have tightened it up to keep things more encapsulated and easier for debugging/thinking/posting here.

Where is the error coming from?

I'm getting this error but I don't have any idea what I'm doing wrong:
>>> runfile('C:/Users/218003107/callphreeqcInput.pyw', wdir=r'C:/Users/218003107')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\218003107\AppData\Local\Continuum\Anaconda\lib\site-
packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "C:/Users/218003107/callphreeqcInput.pyw", line 5, in <module>
class MyForm(QtGui.QMainWindow, phreqMDI.Ui_MainWindow):
NameError: name 'phreqMDI' is not defined
>>>"
I'm running this code:
import sys
from PyQt4 import QtCore, QtGui
from phreqMDI import *
class MyForm(QtGui.QMainWindow, phreqMDI.Ui_MainWindow):
def __init__(self, parent=None):
super(MyForm,self).__init__(parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.connect(wt1,SIGNAL('textChanged()'),wtResult)
self.connect(wt2,SIGNAL('textChanged()'),wtResult)
self.connect(wt3,SIGNAL('textChanged()'),wtResult)
self.connect(wt4,SIGNAL('textChanged()'),wtResult)
def wtResult(self):
if len(self.ui.wt1.text())!=0:
a=float(self.ui.wt1.text())
else:
a=0
if len(self.ui.wt2.text())!=0:
b=float(self.ui.wt2.text())
else:
b=0
if len(self.ui.wt3.text())!=0:
c=float(self.ui.wt3.text())
else:
c=0
if len(self.ui.wt4.text())!=0:
c=float(self.ui.wt4.text())
else:
d=0
sum=a+b+c+d
self.ui.wt_total.setText(str(sum))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
=MyForm()
myapp.show()
app.exec_()
Here are the first few lines (not showing all 500 lines) of the ui file that created using Qt Designer and converted to .py with pyuic4 (which went fine):
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 Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 677)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
What am I doing wrong here ?
Since you've imported all from phreqMDI, code should be:
from phreqMDI import *
class MyForm(QtGui.QMainWindow, Ui_MainWindow):
or
import phreqMDI
class MyForm(QtGui.QMainWindow, phreqMDI.Ui_MainWindow):

Resources