How do I bring a kill window to the front in tkinter? - python

I have a python program that opens up in full screen. In the program window is a button that the user is supposed to use when they want to exit the program. This is the function for when they click the button:
def closeProgram():
file_name = os.path.basename(sys.argv[0])
file = open("SaveFile1.txt", "a")
file.write(file_name)
file.close()
exit()
But when I call the exit() function, the kill window that pops up, asking if I REALLY want to stop the program, shows up underneath the program window. Is there way a to bring the kill window to the front?

This is caused by IDLE, not by python or tkinter. Use raise SystemExit instead to kill only your program and not IDLE. Or (much better) redesign your program so that it reaches the end when done. If you are using a tkinter mainloop you can use root.quit() to end it.

Related

When you call mainloop in tkinter, do you need to call it in the first window that will be opened?

I'm making a login system, and I called mainloop in the first window that will be opened upon starting the program. This window is the window which asks whether the user would like to login or register. Obviously, this means I can't close this window at all, which is impractical and I was hoping to be able to use mainloop instead in the main window that will open once the user has logged in, which will be a window where they will be able to play my game. Will this cause any issues in the program, or will everything run as usual?
call the mainloop for every window you make
for example if you make a window as
root=Tk()
then after writing all the code for the window
write root.mainloop()
in the end

how to stop mainloop function without closing window

I want to debug my program and i want to freeze the window by stopping mainloop but without closing the window.
I tried to use quit() and destroy() but both just close the window.
A little example of part of my code:
from tkinter import Tk, Canvas
root=Tk()
root.bind('<Space>', lambda e: '<------what to put here')
Can I close mainloop without the window always closing so that it can freeze and i can look at it closely. thanks
quit won't destroy the window. However, it might cause your program to stop if you have no code to prevent it from stopping after mainloop returns.
If all you want to do is have it freeze for debugging purposes, there is no need to stop mainloop. The simplest method is to start the pdb debugger which will give you a prompt and prevent the mainloop from processing events until you exit pdb.
def pause(event=None):
import pdb
pdb.set_trace()
root.bind('<Space>', pause)
Of course, you don't have to use pdb here - any function which doesn't return will work. For example, you could call input or read. pdb is convenient because it makes it possible to examine variables, run other code, or step through the program one line at a time.
If you literally want to "close mainloop", call the quit method. That is literally what it does -- it tells mainloop to quit. You're responsible for adding code after mainloop returns to prevent the window from closing.

How to close GLUT Window when input_raw() is active? Python

This might be a silly question, but I couldn't figure this out on my own neither have I find the solution on line. I have a PyOpenGl application that uses GLUT to create its window. The program is supposed to continue running while the user input in the TERMINAL is not "quit". Something like this:
command = raw_input()
while command != "quit":
if command == "add_shape":
draw_cube()
elif command == "remove_shape":
clear_window()
elif command == "add_light":
add_light()
command = raw_input()
sys.exit("Application closed by the user")
The problem is that when I can raw_input() closing the window using the "x" button doesn't work, only typing quit would terminate the task. At first I thought GLUT would have a callback function for closing window that I could use to detect the "x" button click and force terminate the application, turns out it does have such a function void glutCloseFunc(void(*)(void)callback) but it is not called while raw_input() is active. So what is a good solution for this? How can I read user input from the terminal and still be able to close my application by clicking the "x" button?
REQUIRED:
Read user input from the terminal.
OPTIONAL:
Using GLUT is optional. I've tried with PyGame, but couldn't find a solution to read from the terminal since it only treats interactions with the window.
Thank you!
I believe the most logic and easy way to do it, is with the use of threads. SO you would have one thread to listen to raw_inputs() and another thread to listen to the window close event.

Can I use sys.exit() to stop a Tkinter program

Is there a problem with using sys.exit() to stop a Tkinter program?
I know normally people use root.destroy() why is that?
Of course but it will terminate the entire program, including your app.
According to here: https://www.daniweb.com/programming/software-development/threads/66698/exit-a-tkinter-gui-program. root.destroy is a safe, dependable way to exit your Tkinter app. From the link:
destroy() just terminates the mainloop and deletes all widgets. So it seems to be safer if you call your app from another Tkinter app, or if you have multiple mainloops.
I use both. root.destroy() is used to destroy all windows (parent, child) within the root tkinter instance, but it doesn't end the python program.
sys.exit() stops all active applications used by python.
In short, if your python code runs a tkinter GUI exclusively and it's functionality ends after the window is closed then use both root.destroy() followed by sys.exit() to effectively end the session.

Main window doesnt closes after caling new script from it

I am learning python and for GUI model using wxpython, as I am new to programing get stuck every time.
My issue is I have a GUI (main window)with two buttons,when user clicks button1 it opens sub window (seperate python script), I want to close or destroy main window before opening sub window.
self.Destroy()
subprocess.call("python newframe.py",shell=True)
#It will not close main window
What will be the wrong i am trying to do , and please explain what is proper method.
Looking for suggestions thanks .
Sorry for my english .
subprocess.call() wait subprocess to exit. -> button callback will never return until subprogram exit. This keep main window to close. Use subprocess.Popen() which does not wait subprocess.
self.Destroy()
subprocess.Popen('python newframe.py', shell=True)
You shouldn't close the main frame or you'll actually exit the application. Instead you should Hide the main frame and just Show the sub-frame. Then when you close the sub-frame, you can re-show the main frame. I personally think using Pubsub is the easiest way to accomplish this. Here's a link to a tutorial that shows how to do it:
http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
You could also pass a reference to the main frame when you instantiate the sub-frame or call something like GetTopWindow() or GetParent(), but I really recommend Pubsub for this.

Categories

Resources