0

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?

Benjamin
  • 1
  • 1
  • 1
    It's likely there's a better way to achieve whatever you're after than trying to restart your program altogether. – AKX Apr 20 '22 at 18:57

2 Answers2

0

There is a generic way to restart a program

import sys
import os

def restart_program():
   python = sys.executable
   os.execl(python, python, * sys.argv)

Credits: https://www.daniweb.com/programming/software-development/code/260268/restart-your-python-program

Also can help: Restart program tkinter

Jhones
  • 19
  • 3
0

how to make the function restart_gui work properly

You don't need threading

  • In line 17, replace this with command=self.restart
  • In line 25, replace this GUI.restart_gui(self) with GUI.restart_gui(self)
  • In line 44, replace this self.start_gui() with GUI()

Snippet:

from tkinter import *
from tkinter import messagebox
 

has_gui_started = False

class ExcelScript:

    def __init__(self, gui):
        self.gui = gui
        self.b2 = Button(self.gui, 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(self.gui, text='Restart Program', command=self.restart)
            self.b9.grid(column=1, row=10, padx=10, pady=2, sticky="w")
        else:
            self.b2['text'] = "More"
            self.b9.destroy()

    def restart(self):
        print("Nothing is happening.")
        GUI.restart_gui(self)
         
        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()
        GUI()
        #self.start_gui()

if has_gui_started == False:
    GUI()
    has_gui_started = True

Screenshot:

enter image description here

After the widget destroyed. You can see debug'

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19