Please! Need help JUST TO START implementing Python code into QT designed forms
Dear users,
I am reading the Summerfield's PyQT book. Was fighting many days to start simple Python code in QT designed forms. I am using QT designer v. 4.8.0, Python v. 3.2.3. The simple program I am trying to make: Window with a pushButton, lineEdit and a textLabel. So what I want to do: when I push the button, the textLabel adopts the text from the lineEdit, that's it!
What I have done:
1. In QT designer drew a Dialog window with the three above metioned elements.
2. Using pyuic.py program converted the ui file to py file
3. created the following Python script:
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import t_transfer # my py file obtained in step 2, shown below
class DoItNow
(QDialog, t_transfer.
t_transfer): #created my own class DoItNow, where I try to initialize the form. Not sure about this, though :) def __init__(self, parent=None):
super(DoItNow, self).__init__(parent)
self.setupUi(self)
@pyqtSignature("")
def on_pushButton_clicked(self):
a = lineEdit.text()
label.setText(a)
form = Dialog()
form.show()
app.exec_()
My t_transfer.py file
Code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 't_transfer.ui'
#
# Created: Wed Aug 1 00:05:17 2012
# 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.
fromUtf8except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.
pushButton.
setGeometry(QtCore.
QRect(230,
120,
75,
23)) self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.
lineEdit.
setGeometry(QtCore.
QRect(50,
50,
113,
18)) self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.
label = QtGui.
QLabel(Dialog
) self.
label.
setGeometry(QtCore.
QRect(50,
130,
111,
16)) font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
if __name__ == "__main__":
import sys
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I would greatly appreciate your help!
Re: Please! Need help JUST TO START implementing Python code into QT designed forms
Your main program adapted with markers showing where:
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from t_transfer import Ui_Dialog # <<<< Import the class the Designer built
def __init__(self, parent=None):
super(DoItNow, self).__init__(parent)
self.ui = Ui_Dialog() # <<<< Creates an instance of the class the Designer built
self.ui.setupUi(self) # <<<< Finishes initialising it
@pyqtSignature("")
def on_pushButton_clicked(self):
a = self.ui.lineEdit.text() # <<<< self.ui refers to the ui object just like in C++
self.ui.label.setText(a)
if __name__ == "__main__":
import sys
form = DoItNow() #<<<< your class is called DoItNow not Dialog
form.show()
sys.exit(app.exec_())
There are other ways to do the UI incorporation: http://www.riverbankcomputing.co.uk/.../designer.html
Re: Please! Need help JUST TO START implementing Python code into QT designed forms
It works! Thanks a lot ChrisW67. Now I can continue :)