0

I've designed two GUIs. One GUI opens when the python code is run and the second GUI opens when the user exits the first one.

The first GUI is to inform the user of an issue with the second GUI and contains a solution to fix the issue if it happens. I added the first GUI in after I completed the second one and realised an issue with the design which can't be fixed.

Right now, the first GUI has an 'Understand and Accept' button on it. I want to add a button which stops the python code running.

I've created a very basic version of what I mean below. I've cut out the unnecessary bits and renamed others to better be able to explain, so that's why the grid values don't match up.

import tkinter as tk
from tkinter import *

def runwarningwindow():
    #This is the first GUI which opens warning tab
    warningscreen = Tk()
    warningscreen.resizable(width=False, height=False)
    warningscreen.title("First Window - Trial Warning Window")

    #This is the Warning message
    warningmessage = Label(warningscreen, text="Warning Message")
    warningmessage.grid(row=1, column=0)
    #This is the Accept Button
    warningunderstoodbutton = tk.Button(warningscreen, text='Understand and Accept', command=warningscreen.destroy)
    warningunderstoodbutton.grid(row=3, column=0)
    rejectedButton= tk.Button(warningscreen, text='Stop')
    rejectedButton.grid(row=4, column=0)
    warningscreen.mainloop()

runwarningwindow()

trialGUI = Tk()
trialGUI.geometry('710x320')
trialGUI.title("Second GUI - Test GUI")
trialGUI.mainloop()

I know one of the possible solutions is to turn the 'trialGUI' into

def runtrailGUI():
    trialGUI = Tk()
    trialGUI.geometry('710x320')
    trialGUI.title("Second GUI - Test GUI")
    trialGUI.mainloop()

and then make the 'Understand and Accept' button run it, but this is a solution I'm trying to avoid. The code for the second GUI is over 3000 lines (and counting, I'm still adding more to it), so I'm looking for another solution. Is there a command to stop python code being run?

  • 1
    Since everything in tkinter runs on the main thread, dispatching some of the work can help reduce GUI thread hang-ups: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch09s07.html – Mark R. Jan 28 '21 at 14:25
  • `rejectedButton= tk.Button(warningscreen, text='Stop',command=warningscreen.destroy)` – Ajay Jan 28 '21 at 14:33

1 Answers1

0

When you want to stop the program, you can use the following command:

raise SystemExit
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • This is a bad idea imo, exiting a program can be done in so many better ways; stopping a loop with an event, changing a global var... All are better practices than raising an exception without even initializing it – TheZadok42 Jan 28 '21 at 14:35
  • @TheZadok42 `raise SystemExit` or `sys.exit()` are the most widely used and standardized way of exiting a python program. See here: https://docs.python.org/3/library/exceptions.html#SystemExit – DapperDuck Jan 28 '21 at 14:49
  • the fact that it exists does not mean you should use it. `eval` is a thing and is the worst practice in the entirety of python. Exiting a program should be done gracefully. If you use `sys.exit`, one can never know when and why the program will exit. It is extremely confusing and impossible to debug. – TheZadok42 Jan 28 '21 at 15:03
  • @TheZadok42 I would recommend, you read this highly upvoted Stack Overflow answer: https://stackoverflow.com/a/19747562/12602208 – DapperDuck Jan 28 '21 at 15:08
  • I read the answer and I have to respectfully disagree. In my experience, exiting a program using these methods only leads to a lot of confusion. If you have open threads, processes, or anything related, I am 100% sure this will lead to some form of problem; memory leak, open connections, open file handlers... – TheZadok42 Jan 28 '21 at 15:17
  • 1
    @TheZadok42 These are valid concerns, and I respect your opinion. – DapperDuck Jan 28 '21 at 16:03
  • @DapperDuck I've used your suggestion. I had to put in `def stopAll():/ raise SystemExit` so I could be able to use a button to use it. It seems to work fine and after testing there's no issues. Thanks. –  Jan 29 '21 at 10:17
  • No problem! I believe that all the standard tkinter gui elements run on the same thread, the main thread, so there shouldn't be any issue afaik. – DapperDuck Jan 29 '21 at 12:18