Monday, 15 March 2010

python - PyQt4 enable button on text entry, connect windows -



python - PyQt4 enable button on text entry, connect windows -

i attempting write gui programme in python have virtually no experience in gui programming. starting learning tkinter , picked of need know purposes of program, discovered pyqt , qt designer, , output looks lot nicer.

in program, want first open little window prompts user info (that utilize load file or create new file). because info crucial, don't want user able progress first little window without entering want 'ok' button disabled , enabled when user enters info field. have made effort @ (mostly created in qt designer , edited) shown below.

# -*- coding: utf-8 -*- # form implementation generated reading ui file 'open.ui' # # created: wed jun 25 17:51:25 2014 # by: pyqt4 ui code generator 4.10.3 # # warning! changes made in file lost! pyqt4 import qtcore, qtgui try: _fromutf8 = qtcore.qstring.fromutf8 except attributeerror: def _fromutf8(s): homecoming s try: _encoding = qtgui.qapplication.unicodeutf8 def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror: def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig) class ui_form(object): def setupui(self, form): form.setobjectname(_fromutf8("form")) form.setenabled(true) form.resize(308, 143) self.horizontallayout = qtgui.qhboxlayout(form) self.horizontallayout.setobjectname(_fromutf8("horizontallayout")) self.verticallayout = qtgui.qvboxlayout() self.verticallayout.setobjectname(_fromutf8("verticallayout")) self.horizontallayout_3 = qtgui.qhboxlayout() self.horizontallayout_3.setobjectname(_fromutf8("horizontallayout_3")) spaceritem = qtgui.qspaceritem(40, 20, qtgui.qsizepolicy.expanding, qtgui.qsizepolicy.minimum) self.horizontallayout_3.additem(spaceritem) self.label = qtgui.qlabel(form) self.label.setobjectname(_fromutf8("label")) self.horizontallayout_3.addwidget(self.label) spaceritem1 = qtgui.qspaceritem(40, 20, qtgui.qsizepolicy.expanding, qtgui.qsizepolicy.minimum) self.horizontallayout_3.additem(spaceritem1) self.verticallayout.addlayout(self.horizontallayout_3) self.horizontallayout_7 = qtgui.qhboxlayout() self.horizontallayout_7.setobjectname(_fromutf8("horizontallayout_7")) self.label_2 = qtgui.qlabel(form) self.label_2.setobjectname(_fromutf8("label_2")) self.horizontallayout_7.addwidget(self.label_2) self.lineedit = qtgui.qlineedit(form) self.lineedit.setobjectname(_fromutf8("lineedit")) self.horizontallayout_7.addwidget(self.lineedit) self.verticallayout.addlayout(self.horizontallayout_7) self.horizontallayout_8 = qtgui.qhboxlayout() self.horizontallayout_8.setobjectname(_fromutf8("horizontallayout_8")) self.label_3 = qtgui.qlabel(form) self.label_3.setobjectname(_fromutf8("label_3")) self.horizontallayout_8.addwidget(self.label_3) self.lineedit_2 = qtgui.qlineedit(form) self.lineedit_2.setobjectname(_fromutf8("lineedit_2")) self.horizontallayout_8.addwidget(self.lineedit_2) self.verticallayout.addlayout(self.horizontallayout_8) self.horizontallayout_9 = qtgui.qhboxlayout() self.horizontallayout_9.setobjectname(_fromutf8("horizontallayout_9")) self.pushbutton_2 = qtgui.qpushbutton(form) self.pushbutton_2.setenabled(false) self.pushbutton_2.setobjectname(_fromutf8("pushbutton_2")) self.horizontallayout_9.addwidget(self.pushbutton_2) self.pushbutton = qtgui.qpushbutton(form) self.pushbutton.setobjectname(_fromutf8("pushbutton")) self.horizontallayout_9.addwidget(self.pushbutton) self.verticallayout.addlayout(self.horizontallayout_9) self.horizontallayout.addlayout(self.verticallayout) self.retranslateui(form) qtcore.qobject.connect(self.lineedit_2, qtcore.signal(_fromutf8("textedited(qstring)")), self.pushbutton_2.setenabled) qtcore.qmetaobject.connectslotsbyname(form) def retranslateui(self, form): form.setwindowtitle(_translate("form", "form", none)) self.label.settext(_translate("form", "please come in name , pupil number:", none)) self.label_2.settext(_translate("form", "name: ", none)) self.label_3.settext(_translate("form", "student number: ", none)) self.pushbutton_2.settext(_translate("form", "ok", none)) self.pushbutton.settext(_translate("form", "cancel", none)) if __name__ == "__main__": import sys app = qtgui.qapplication(sys.argv) form = qtgui.qwidget() ui = ui_form() ui.setupui(form) form.show() sys.exit(app.exec_())

when run programme , type pupil number field gives next error: typeerror: qwidget.setenabled(bool): argument 1 has unexpected type 'str'. realise problem is taking string input rather boolean don't know how prepare it.

the sec part of problem want new bigger window open when user clicks 'ok' on little window, window have next alternative @ bottom progresses similar window have no thought how (link windows is).

how do things mentioned above? or should stick tkinter though seems me aesthetically inferior. thanks.

firstly should never write code generated ui file qt designer gets overwritten pyuic tool next time write it. instead import seperate file.

this line needs removed ui_file:

qtcore.qobject.connect(self.lineedit_2, qtcore.signal(_fromutf8("textedited(qstring)")), self.pushbutton_2.setenabled)

example:

#!/usr/bin/env python # -*- coding: utf-8 -*- import sys pyqt4.qtcore import pyqtslot pyqt4.qtgui import qwidget, qapplication #assumes file called ui_main_form.py pyuic tool ui_main_form import ui_form class mainform(qwidget): def __init__(self, parent=none): #initialise super(mainform, self).__init__(parent) #setup ui self.ui = ui_form() self.ui.setupui(self) #now can modifications cant done in qt designer #handle textchanged signal qlineedit self.ui.lineedit_2.textchanged.connect(self.line_edit_text_changed) @pyqtslot(str) def line_edit_text_changed(self, text): if text: # check see if text filled in self.ui.pushbutton_2.setenabled(true) else: self.ui.pushbutton_2.setenabled(false) if __name__ == '__main__': app = qapplication(sys.argv) my_form = mainform() my_form.show() sys.exit(app.exec_())

there useful info on signal , slots mechanism pyqt here

for how launch windows 1 see this: how create new pyqt4 windows existing window? accepted answer.

hope gets started.

python qt user-interface pyqt

No comments:

Post a Comment