How to switch between two windows in python using PyQt -
i working on app. wanted create toolbar after having been clicked open settings window. don't know how exactly. way have toolbar icon connected showsettings function. function creates new instance of settings each time click on icon ( know shouldn't create new instances each time 1 in beginning , work on it. don't know how though) , show settings window associated instance. in way settings window flashes second , disappears. don't know what's wrong hope can me out :)
import sys import time pyqt4 import qtcore, qtgui ui import ui_mainwindow ui import ui_settingswindow #aplikacja obslugujaca bramki lf #zamiana pliku .ui na kod pythona #pyuic4 untitled.ui -o output.py class timerthread(qtcore.qthread): timeelapsed = qtcore.pyqtsignal(float) def __init__(self, parent=none): super(timerthread, self).__init__(parent) self.timestart = none def start(self, timestart): self.timestart = timestart return super(timerthread, self).start() def run(self): while self.parent().isrunning(): self.timeelapsed.emit(time.time() - self.timestart) time.sleep(0.1) class mythread(qtcore.qthread): timeelapsed = qtcore.pyqtsignal(float) def __init__(self, parent=none): super(mythread, self).__init__(parent) self.timerthread = timerthread(self) self.timerthread.timeelapsed.connect(self.timeelapsed.emit) def run(self): self.timerthread.start(time.time()) iterations = 10000000000 while iterations: print "running {0}".format(self.__class__.__name__) iterations -= 1 time.sleep(10) class settingswindow(qtgui.qmainwindow): #constructor: def __init__(self, parent=none): qtgui.qwidget.__init__(self, parent) self.ui_settigns = ui_settingswindow() self.ui_settigns.setupui(self) class mainwindow(qtgui.qmainwindow): #constructor: def __init__(self, parent=none): qtgui.qwidget.__init__(self, parent) self.ui = ui_mainwindow() self.ui.setupui(self) self.mythread = mythread(self) self.mythread.timeelapsed.connect(self.on_mythread_timeelapsed) self.mythread.finished.connect(self.on_mythread_finished) #nie wiem jak przerzucic pliku ui bez errorow #toolbar settingsaction = qtgui.qaction(qtgui.qicon('tools.jpg'), 'ustawienia', self) settingsaction.setshortcut('ctrl+u') settingsaction.triggered.connect(self.showsettings) self.toolbar = self.addtoolbar('settings') self.toolbar.addaction(settingsaction) def updatelist(self): contest_name = self.ui.combobox.currenttext() #jakis try-catch tutaj sie przyda gdyby nie plik nie istnial #zczytuje z pliku liste robotow open('database\%s.txt' % contest_name) f: robots_list = f.read().splitlines() self.ui.listwidget.clear() self.ui.listwidget.additems(robots_list) def search(self): text = self.ui.lineedit.text() self.ui.lineedit.clear() item = self.ui.listwidget.finditems(text, qtcore.qt.matchexactly) #jakiegos try-catcha trzeba tutaj walnac self.ui.listwidget.setitemselected(item[0], true) def showsettings(self): settings = settingswindow() #creates settings instance settings.show() @qtcore.pyqtslot() def on_button_clicked(self): self.mythread.start() @qtcore.pyqtslot(int) def on_mythread_timeelapsed(self, seconds): self.ui.lcdnumber.display(seconds) @qtcore.pyqtslot() def on_mythread_finished(self): self.mythread.terminate() print "done" if __name__ == "__main__": app = qtgui.qapplication(sys.argv) myapp = mainwindow() settings = settingswindow() myapp.show() settings.show() sys.exit(app.exec_()) and here ui file
# -*- coding: utf-8 -*- # form implementation generated reading ui file 'untitled.ui' # # created: fri apr 24 08:04:20 2015 # by: pyqt4 ui code generator 4.11.3 # # warning! changes made in file lost! 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_settingswindow(object): def setupui(self, settingswindow): settingswindow.setobjectname(_fromutf8("ustawienia")) settingswindow.resize(686, 556) class ui_mainwindow(object): def setupui(self, mainwindow): mainwindow.setobjectname(_fromutf8("mainwindow")) mainwindow.resize(686, 556) self.centralwidget = qtgui.qwidget(mainwindow) self.centralwidget.setobjectname(_fromutf8("centralwidget")) self.groupbox = qtgui.qgroupbox(self.centralwidget) self.groupbox.setgeometry(qtcore.qrect(10, 10, 341, 491)) self.groupbox.setobjectname(_fromutf8("groupbox")) self.pushbutton = qtgui.qpushbutton(self.groupbox) self.pushbutton.setgeometry(qtcore.qrect(10, 460, 231, 23)) self.pushbutton.setobjectname(_fromutf8("pushbutton")) self.label = qtgui.qlabel(self.groupbox) self.label.setgeometry(qtcore.qrect(10, 80, 90, 16)) self.label.setobjectname(_fromutf8("label")) self.lineedit = qtgui.qlineedit(self.groupbox) self.lineedit.setgeometry(qtcore.qrect(130, 77, 191, 20)) self.lineedit.setobjectname(_fromutf8("lineedit")) self.listwidget = qtgui.qlistwidget(self.groupbox) self.listwidget.setgeometry(qtcore.qrect(10, 110, 321, 341)) self.listwidget.setobjectname(_fromutf8("listwidget")) item = qtgui.qlistwidgetitem() self.listwidget.additem(item) item = qtgui.qlistwidgetitem() self.listwidget.additem(item) item = qtgui.qlistwidgetitem() self.listwidget.additem(item) self.label_2 = qtgui.qlabel(self.groupbox) self.label_2.setgeometry(qtcore.qrect(10, 50, 111, 16)) self.label_2.setobjectname(_fromutf8("label_2")) self.combobox = qtgui.qcombobox(self.groupbox) self.combobox.setgeometry(qtcore.qrect(130, 50, 191, 22)) self.combobox.setobjectname(_fromutf8("combobox")) self.combobox.additem(_fromutf8("")) self.combobox.additem(_fromutf8("")) self.combobox.additem(_fromutf8("")) self.combobox.additem(_fromutf8("")) self.groupbox_2 = qtgui.qgroupbox(self.centralwidget) self.groupbox_2.setgeometry(qtcore.qrect(360, 10, 311, 491)) self.groupbox_2.setobjectname(_fromutf8("groupbox_2")) self.lcdnumber = qtgui.qlcdnumber(self.groupbox_2) self.lcdnumber.setgeometry(qtcore.qrect(10, 20, 281, 151)) self.lcdnumber.setsmalldecimalpoint(true) self.lcdnumber.setobjectname(_fromutf8("lcdnumber")) self.pushbutton_2 = qtgui.qpushbutton(self.groupbox_2) self.pushbutton_2.setgeometry(qtcore.qrect(20, 340, 75, 23)) self.pushbutton_2.setobjectname(_fromutf8("pushbutton_2")) self.pushbutton_3 = qtgui.qpushbutton(self.groupbox_2) self.pushbutton_3.setgeometry(qtcore.qrect(200, 340, 75, 23)) self.pushbutton_3.setobjectname(_fromutf8("pushbutton_3")) mainwindow.setcentralwidget(self.centralwidget) self.retranslateui(mainwindow) qtcore.qobject.connect(self.pushbutton, qtcore.signal(_fromutf8("clicked()")), self.lineedit.clear) qtcore.qobject.connect(self.lineedit, qtcore.signal("returnpressed()"), mainwindow.search) #connected search method in class mainwindow qtcore.qobject.connect(self.combobox, qtcore.signal(_fromutf8("currentindexchanged(qstring)")), mainwindow.updatelist) qtcore.qobject.connect(self.pushbutton_2, qtcore.signal(_fromutf8("clicked()")), mainwindow.on_button_clicked) qtcore.qobject.connect(self.pushbutton_3, qtcore.signal(_fromutf8("clicked()")), mainwindow.on_mythread_finished) qtcore.qmetaobject.connectslotsbyname(mainwindow) def retranslateui(self, mainwindow): mainwindow.setwindowtitle(_translate("mainwindow", "lf timer", none)) self.groupbox.settitle(_translate("mainwindow", "wybierz robota", none)) self.pushbutton.settext(_translate("mainwindow", "uruchom aplikacjÄ™ pomiaru czasu", none)) self.label.settext(_translate("mainwindow", "wyszukaj robota:", none)) self.lineedit.settext(_translate("mainwindow", "cos tam", none)) __sortingenabled = self.listwidget.issortingenabled() self.listwidget.setsortingenabled(false) item = self.listwidget.item(0) item.settext(_translate("mainwindow", "robot 1", none)) item = self.listwidget.item(1) item.settext(_translate("mainwindow", "robot 2", none)) item = self.listwidget.item(2) item.settext(_translate("mainwindow", "robot 3", none)) self.listwidget.setsortingenabled(__sortingenabled) self.label_2.settext(_translate("mainwindow", "wybierz konkurencje:", none)) self.combobox.setitemtext(0, _translate("mainwindow", "", none)) self.combobox.setitemtext(1, _translate("mainwindow", "lf", none)) self.combobox.setitemtext(2, _translate("mainwindow", "turbo lf", none)) self.combobox.setitemtext(3, _translate("mainwindow", "lego lf", none)) self.groupbox_2.settitle(_translate("mainwindow", "pomiar czasu", none)) self.pushbutton_2.settext(_translate("mainwindow", "start", none)) self.pushbutton_3.settext(_translate("mainwindow", "stop", none))
the instance of settings window local mainwindow.showsettings method. such, garbage collected because nothing keeps reference window.
you can fix in 2 ways. first specify parent of settings window when instantiated. second store instance attribute of mainwindow (eg self.settings = ...).
Comments
Post a Comment