0

I'm new to Python and are trying to do a small program in which the GUI-window is supposed to re-open while clicking the button.

I read online that the line of code

os.execl(sys.executable, sys.executable, *sys.argv)

is supposed to make your program restart, but all it does is exit the GUI-window and restart Shell totally clean.

from tkinter import *
import sys
import os


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

window = Tk()
window.title("Test")
button = Button(window, text="RESTART", width=6, command=restart_program).grid(row=5, column=0)



window.mainloop()

This is the relevant part of my code atm (I didn't include all the code - some more widgets are adding to the existing, but it's not relevant to the problem). Is there any way to re-open the window called "Test", especially without having to put it in a function? Would be very thankful for help! :)))

Emelie
  • 1
  • 1
  • 2
  • What's wrong with putting it in a function? Reusing code is exactly what functions are for. – Aran-Fey Jul 26 '18 at 18:44
  • Where are you calling the `mainloop` function? I added the line `window.mainloop()` to the bottom of your code given, and when I run it and click the button it restarts as it should. – johmsp Jul 26 '18 at 18:47
  • Oh sorry, of course there is going to be a mainloop function in the end (changed it in the question now). In my case, I only get this:>>> =============================== RESTART: Shell =============================== >>> I would like for the original window, the "Test"-window with the button, to appear again! Thankful for help! – Emelie Jul 26 '18 at 18:50
  • The reason I don't want to put it in a function is because I have lots of more code that runs in to trouble when I make a new function... (I'm adding GUI to a already complete program) and nevertheless, the code os.excel.... seems to distant Shell from all previous functions. It's almost like Shell is restarting clean with nothing else – Emelie Jul 26 '18 at 18:54
  • 1
    Are you running this in an IDE (e.g. IDLE or Anaconda) or in the command line? It will probably work in command line. – johmsp Jul 26 '18 at 19:07
  • There are a few post already about this exact problem. [here](https://stackoverflow.com/questions/731887/resetting-the-main-gui-window) and [here](https://stackoverflow.com/questions/41655618/restart-program-tkinter). Probably a good place to start. – Mike - SMT Jul 26 '18 at 19:30
  • Possible duplicate of [Resetting the main GUI window](https://stackoverflow.com/questions/731887/resetting-the-main-gui-window) – Mike - SMT Jul 26 '18 at 19:31
  • I have tested your code using Sypder3 IDE and also the terminal on Ubuntu18 and your code works correctly. As @johmsp mentioned, this issue might be related to your IDE or development environment. – SuperKogito Jul 26 '18 at 23:59

0 Answers0