I'm having trouble creating a button, that when pressed, will completely close/exit/stop a Python Tkinter GUI and relaunch it as a fresh instance. The code that creates the GUI and the code that has all the GUI functions are in two separate classes.
Here is what it looks like:
from tkinter import *
from tkinter import messagebox
import threading
has_gui_started = False
class ExcelScript:
def __init__(self, gui):
self.b2 = Button(text='More', command=self.more_less)
self.b2.grid(column=1, row=3, padx=10, pady=2, sticky="w")
def more_less(self):
if self.b2['text'] == "More":
self.b2['text'] = "Less"
self.b9 = Button(text='Restart Program', command=lambda:[threading.Thread(target=self.restart).start()])
self.b9.grid(column=1, row=10, padx=10, pady=2, sticky="w")
else:
self.b2['text'] = "More"
self.b9.destroy()
def restart(self):
#gui.master.destroy()
GUI.restart_gui(self)
print("Nothing is happening.")
pass
class GUI:
def __init__(self):
self.start_gui()
def start_gui(self):
self.gui = Tk()
ExcelScript(self.gui)
self.gui.title('Excel Script')
self.gui.geometry("")
self.gui.mainloop()
def restart_gui(self):
self.gui.quit()
self.gui.destroy()
self.start_gui()
if has_gui_started == False:
GUI()
has_gui_started = True
Does anyone know how to make the function restart_gui
work properly?