Calling a function from another GUI class Python -
i have created gui in python consists of stacked widget containing 3 seperate widgets, loginbackend, editorbackend, mapbackend. seperate classes named loginbackend, editorbackend, mapbackend. each 1 called class , main window widget called mainbackend. idea user login (using loginbackend widget) , able access rest of widgets. problem each of other widgets need login information in order connect database. reason cannot information return when calling editorbackend class. trying this
this editorbackend
from loginbackend import loginbackend class editorbackend(qwidget, editordesign.ui_form): def __init__(self): super(self.__class__, self).__init__() self.setupui(self) self.initui() def initui(self): self.submitpushbutton.clicked.connect(self.insert) def insert(self): login = loginbackend() url = login.creds() print('url: ' + url)
this loginbackend
class loginbackend(qwidget, logindesign.ui_login): def __init__(self): super(self.__class__, self).__init__() self.setupui(self) def creds(self): username = self.userlineedit.text() password = self.passlineedit.text() host = self.selhostlabel.text() port = self.selportlabel.text() sid = self.selsidlabel.text() cred = username + '/' + password + '@' + host + ':' + port + '/' + sid print('creds: '+ cred) return cred
then the main window calls both of these , puts them stacked widget.
from pyqt5.qtwidgets import qapplication,qmainwindow import sys loginbackend import loginbackend mapbackend import mapbackend editorbackend import editorbackend edittnsnamesbackend import edittnsnamesbackend import mainwindow class mainbackend(qmainwindow, mainwindow.ui_mainwindow): def __init__(self): super(self.__class__, self).__init__() self.setupui(self) self.initui() def initui(self): self.url = none self.poplist() self.login = loginbackend() self.mapping = mapbackend() self.edit = editorbackend() self.edittns = edittnsnamesbackend() self.popwidgets() self.listwidget.itemclicked.connect(self.change) self.actiontns_names.triggered.connect(self.edittnsnames) def edittnsnames(self): self.edittns.show() def change(self): index = self.listwidget.currentrow() self.stackedwidget.setcurrentindex(index) self.url = self.login.creds() print('url change. ' + self.url) def poplist(self): widgets = ['login', 'edit database', 'edit mapping'] self.listwidget.additems(widgets) def popwidgets(self): self.stackedwidget.insertwidget(0,self.login) self.stackedwidget.setcurrentwidget(self.login) self.stackedwidget.insertwidget(1,self.edit) self.stackedwidget.setcurrentwidget(self.edit) self.stackedwidget.insertwidget(2,self.mapping) self.stackedwidget.setcurrentwidget(self.mapping) self.stackedwidget.setcurrentindex(0) self.listwidget.setcurrentrow(0) def main(): app = 0 app = qapplication(sys.argv) main_window = mainbackend() main_window.show() app.exec_() if __name__ == "__main__": main()
the print statement in loginbackend , in main window prints need cannot print in editorbackend. have suggestions on how fix or maybe work around?
the designer files (abbreviated):
logindesign classes:
class ui_login(object): def setupui(self, login): login.setobjectname("login") ... ... ... def retranslateui(self, login): _translate = qtcore.qcoreapplication.translate
editordesign classes:
class ui_form(object): def setupui(self, form): form.setobjectname("form") ... ... ... def retranslateui(self, form): _translate = qtcore.qcoreapplication.translate
mainwindow classes:
class ui_mainwindow(object): def setupui(self, mainwindow): mainwindow.setobjectname("mainwindow") ... ... ... def retranslateui(self, mainwindow): _translate = qtcore.qcoreapplication.translate
so solved problem creating function in class need information. pass argument main window (where knew values). shown below.
create function in editorbackend
from loginbackend import loginbackend class editorbackend(qwidget, editordesign.ui_form): def __init__(self): super(self.__class__, self).__init__() self.setupui(self) self.initui() self.url = none def initui(self): self.submitpushbutton.clicked.connect(self.insert) #### new function #### def geturl(self, url): self.url = url print('geturl '+url) ###################### def insert(self): print('url: ' + self.url)
send login info main window editorbackend
from pyqt5.qtwidgets import qapplication,qmainwindow import sys loginbackend import loginbackend mapbackend import mapbackend editorbackend import editorbackend edittnsnamesbackend import edittnsnamesbackend import mainwindow class mainbackend(qmainwindow, mainwindow.ui_mainwindow): def __init__(self): super(self.__class__, self).__init__() self.setupui(self) self.initui() def initui(self): self.url = none self.poplist() self.login = loginbackend() self.mapping = mapbackend() self.edit = editorbackend() self.edittns = edittnsnamesbackend() self.popwidgets() self.listwidget.itemclicked.connect(self.change) self.actiontns_names.triggered.connect(self.edittnsnames) def edittnsnames(self): self.edittns.show() def change(self): index = self.listwidget.currentrow() self.stackedwidget.setcurrentindex(index) self.url = self.login.creds() print('url change. ' + self.url) ### pass information editorbackend ### self.edit.geturl(self.url) ############################################# def poplist(self): widgets = ['login', 'edit database', 'edit mapping'] self.listwidget.additems(widgets) def popwidgets(self): self.stackedwidget.insertwidget(0,self.login) self.stackedwidget.setcurrentwidget(self.login) self.stackedwidget.insertwidget(1,self.edit) self.stackedwidget.setcurrentwidget(self.edit) self.stackedwidget.insertwidget(2,self.mapping) self.stackedwidget.setcurrentwidget(self.mapping) self.stackedwidget.setcurrentindex(0) self.listwidget.setcurrentrow(0) def main(): app = 0 app = qapplication(sys.argv) main_window = mainbackend() main_window.show() app.exec_() if __name__ == "__main__": main()
Comments
Post a Comment