When I get out of program(sys.exit(app.exec_())),Main Form closes, but there are two problems:
1 - Destructor of MainForm class not run
2 - Thread is still running too
I want when i close app, destructor of MainForm is runed and all threads are killed too
class MainForm(QMainWindow,Ui_MainWindow):
def __init__(self,parent=None):
super(MainForm,self).__init__(parent)
self.setupUi(self)
#...
def init_main_form(self):
#...
self.show_time()
def show_time(self):
self.label_9.setText(u"{}:{}:{}".format(str(datetime.datetime.now().hour),str(datetime.datetime.now().minute),str(datetime.datetime.now().second)))
self.label_9.resize(self.label_9.width()+len(self.label_9.text())*3,self.label_9.height())
b = threading.Timer(1,self.show_time)
#b.setName('localtime')
#self.thread_list.append(b)
b.start()
def __del__(self):
print("app is closed")
for tr in threading.enumerate():
if tr.isAlive():
tr._Thread__stop()
# or tr.finished
# or tr.terminate()
def main():
app = QApplication(sys.argv)
main_form = MainForm()
main_form.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
While you're at it please remember that per documentation __del__
is not guaranteed to run when the interpreter exits.
__del__
is even trickier in other implementations of Python (like Jython). Your application shouldn't rely on it executing to run correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With