adamsp303
27th January 2013, 00:29
I am using PyQT and am trying to set up a really simple program. All I want is a simple button that when clicked changes a label text to hello world.. Sorry if this is the wrong place to ask this question.. I have designed the UI in QT 4 Designer and am editing the code in Eric.
The UI code is:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sat Jan 26 21:56:01 2013
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(247, 177)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 40, 131, 20))
self.label.setObjectName(_fromUtf8("label"))
self.button = QtGui.QPushButton(Form)
self.button.setGeometry(QtCore.QRect(70, 100, 96, 27))
self.button.setObjectName(_fromUtf8("button"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "Hmmm...", None, QtGui.QApplication.UnicodeUTF8))
self.button.setText(QtGui.QApplication.translate("Form", "Press Me!", None, QtGui.QApplication.UnicodeUTF8))
and the main app code is:
import sys
from PyQt4 import QtCore, QtGui
import ui_test
def clicked():
designed.label.setText("Hello World!")
def main():
app = QtGui.QApplication(sys.argv)
designed = ui_test.Ui_Form()
widget = QtGui.QWidget()
designed.setupUi(widget)
designed.button.clicked.connect(clicked)
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
But when I run the program and click the button I get the error message:
The debugged program raised the exception unhandled NameError
"global name 'designed' is not defined"
File: /home/peter/Documents/qt/test/app.py, Line: 7
Break here?
So I guess its this line that is causing the problem..
designed.label.setText("Hello World!")
Can anyone help please :)
The UI code is:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sat Jan 26 21:56:01 2013
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(247, 177)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 40, 131, 20))
self.label.setObjectName(_fromUtf8("label"))
self.button = QtGui.QPushButton(Form)
self.button.setGeometry(QtCore.QRect(70, 100, 96, 27))
self.button.setObjectName(_fromUtf8("button"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "Hmmm...", None, QtGui.QApplication.UnicodeUTF8))
self.button.setText(QtGui.QApplication.translate("Form", "Press Me!", None, QtGui.QApplication.UnicodeUTF8))
and the main app code is:
import sys
from PyQt4 import QtCore, QtGui
import ui_test
def clicked():
designed.label.setText("Hello World!")
def main():
app = QtGui.QApplication(sys.argv)
designed = ui_test.Ui_Form()
widget = QtGui.QWidget()
designed.setupUi(widget)
designed.button.clicked.connect(clicked)
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
But when I run the program and click the button I get the error message:
The debugged program raised the exception unhandled NameError
"global name 'designed' is not defined"
File: /home/peter/Documents/qt/test/app.py, Line: 7
Break here?
So I guess its this line that is causing the problem..
designed.label.setText("Hello World!")
Can anyone help please :)