PyQt : accessing elements from outside the class -
first of all, i'd state question similar this: accessing gui elements outside gui class . since newby in pyqt, more help.
i have design.py file, exported qt designer
# -*- coding: utf-8 -*- 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(150, 94) self.centralwidget = qtgui.qwidget(mainwindow) self.centralwidget.setobjectname(_fromutf8("centralwidget")) self.lineedit = qtgui.qlineedit(self.centralwidget) self.lineedit.setgeometry(qtcore.qrect(20, 10, 113, 27)) self.lineedit.setobjectname(_fromutf8("lineedit")) self.pushbutton = qtgui.qpushbutton(self.centralwidget) self.pushbutton.setgeometry(qtcore.qrect(30, 40, 85, 27)) self.pushbutton.setobjectname(_fromutf8("pushbutton")) 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.pushbutton.settext(_translate("mainwindow", "pushbutton", none))
which imported main class:
# -*- coding: utf-8 -*- pyqt4 import qtgui, qtcore import sys import design class exampleapp(qtgui.qmainwindow, design.ui_mainwindow): def __init__(self, parent=none): super(exampleapp, self).__init__(parent) self.setupui(self) self.pushbutton.clicked.connect(self.button_function) def button_function(self): self.lineedit.settext('results') def main(): app = qtgui.qapplication(sys.argv) form = exampleapp() form.show() app.exec_() if __name__ == '__main__': main()
so far, good. have created window in press pushbutton , message appears in editline.
this want achieve: want pushbutton trigger function outside class (easy), , function deliver message ediltline (hard). this:
def button_function(self): calculations() def calculations(): # somehow ... self.lineedit.settext('results') def main(): app = qtgui.qapplication(sys.argv) form = exampleapp() form.show() app.exec_() if __name__ == '__main__': main()
any thought appreciated.
this need
def button_function(self): calculations(self) def calculations(obj): obj.lineedit.settext('results')
Comments
Post a Comment