PDA

View Full Version : Complete newbie hello world question!



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 :)

wysota
27th January 2013, 02:56
Your "clicked" function doesn't know anything about a "designed" object created in another function. This is a strictly python issue and nothing to do with Qt.

Added after 7 minutes:

One possibility to work around it is to define a function in Ui_Form and call that as a slot. Note the code below is sort of a hack that uses some features unique to Python. Usually you'd subclass QWidget, in the constructor instantiate Ui_Form and define the slot function in the QWidget subclass and not the Ui_Form class itself.


def fun_myClickHandler(self):
self.label.setText("Hello World!")

ui_test.Ui_Form.myClickHandler = fun_myClickHandler

designed = ui_test.Ui_Form()
widget = QtGui.QWidget()
designed.setupUi(widget)
designed.button.clicked.connect(designed.myClickHa ndler)
# and so on...

adamsp303
27th January 2013, 20:47
Thank you, got this working now with the following code:



import sys
from PyQt4 import QtCore, QtGui

import ui_test

def fun_myClickHandler(self):
self.label.setText("Hello World!")

def main():
ui_test.Ui_Form.myClickHandler = fun_myClickHandler
app = QtGui.QApplication(sys.argv)
designed = ui_test.Ui_Form()
widget = QtGui.QWidget()
designed.setupUi(widget)
designed.button.clicked.connect(designed.myClickHa ndler)
widget.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()
sys.exit(app.exec_())


Although I don't really understand it, I think I need to learn Python :-).. I have been aproaching this from a perspective of wishing to learn to code GUI software and to find an equivalent of visual basic under Linux as its all I have really done before..

wysota
27th January 2013, 21:31
Hard to do that without learning a programming language first.