How can I destroy Tkinter window on Mac?
My code works fine with window and I could easily exit by clicking on X but on Mac, because of mainloop() and there's no X, I can't close.
So I tried to create shortcut or hotkey to easily close the window. (or tell me if there's other way around)
import tkinter
window=tkinter.Tk()
window.title("tkinter")
window.geometry("640x400+100+100")
window.resizable(False, False)
window.mainloop()
This is my Code. I could easily close on Window but not on Mac.
Maybe if I insert
def close_window(self):
window.destroy()
window.bind('<Control-q>',close_window)
I could simply close by pressing Ctrl+Q but idk why this also returns error.
Please help me. Thank you
Related
This question already has answers here:
Running a Tkinter window and PysTray Icon together
(3 answers)
Closed 10 months ago.
I want to make a script that runs every hour but i don't want the GUI to be visible in taskbar. I don't mind if the GUI is visible when the program is first opened but if the user decides to press the X i want the program to disappear from the taskbar and be visible in the bottom right corner(Skype, discord, ccleaner behaviour)
The GUI is made with tkinter.
How can i do this behaviour? If at all.
To detect if the close button is pressed in tkinter, use the following right before you run root.mainloop()
root.protocol("WM_DELETE_WINDOW", on_closing)
this will call the on_closing function when the user presses the close button, which you can use the following code to make it into a button
root.iconify()
This will minimize it to the taskbar as a button.
Note this will not work on Mac as the window will just be hidden and you will have no way to actually closing it unless killing it manually
so something like this
from tkinter import tk
root = tk.Tk()
#define widgets and other things here
def on_closing():
root.iconify()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
If you want it to be on the system tray, tkinter have no support for that, so you'll have to use something like pystray
I have made a GUI using Tkinter for my Python script for a Voice Assistant. It is working pretty well. But I want to add an animation window to display an animation that I have created using After Effects as an intro to the app. I want it to open without the default close(x), maximize and minimize buttons. The window should stay till the animation is completed, and then it would disappear. The main window would then open normally to launch the GUI. To disable the close, maximize and minimize buttons I have used the root.overrideredirect(True) method. But I am not being able to simultaneously open the two windows one after the other as mentioned. I would be highly obliged if somebody would help me out with this! I have tested it with a code on a simple GUI. I am providing the code below for help with the problem!
from tkinter import *
import time
root = Tk()
root.geometry('500x300')
root.overrideredirect(True) # To disable the default Window decoration
time.sleep(5) # Assuming that the animation runs for 5 seconds
root.destroy() # This window destroys after being on screen for 5 seconds
root.mainloop()
root2 = Tk() # After the previous window is destroyed, this window opens up. Assume that this is the main window
root2.geometry('500x300')
root.mainloop()
Please help me out!
The thing you call "animation window" is actually called "splash". There is a way to do what you want. You need to create a root window for the app (a Tk instance), and then you should hide (root.withdraw()) it. Now create a Toplevel for the splash, wait for 5 seconds, destroy it, and show (root.deiconify()) the Tk window again.
Note: time.sleep(5) should never be used with Tkinter, use root.after(ms, func) instead.
from tkinter import *
def show_splash():
splash = Toplevel()
splash.geometry('500x300')
splash.overrideredirect(True) # To disable the default Window decoration
splash.after(5000, splash.destroy) # This window destroys after being on screen for 5 seconds
splash.wait_window()
root = Tk()
root.withdraw()
show_splash()
root.deiconify()
root.mainloop()
PS: Procedural programming is not a good idea for pretty complex Tkinter-based apps. You should consider using OOP instead.
PPS: Take a look at this and this answers.
I have some Python experience and wish to learn about GUI development using tkinter.
I can create a simple widget, but when I try to close the window, things "hang" and nothing happens. Only by restarting the Python kernel can I get the window to close.
I'm running Python 3.7 using Spyder and, based upon some simple examples I've found in other forums tried the following:
import tkinter
root = tkinter.Tk()
root.title("Hello!")
simple_label = tkinter.Label(root, text="Easy, right?")
closing_button = tkinter.Button(root, text="Close window",
command=root.destroy)
simple_label.pack()
closing_button.pack()
root.mainloop()
As I mention above, the window does not close when I click my mouse on the Close Window button. I just get the "swirly" indicator on my Mac indicating the program is not responding. However, I am able to perform calculations in the Spyder console.
The code works fine, but what you should do is open the program in the Python IDLE that comes when you download Python. When you run the program it should open up the Python Launcher, which allows you to interact your your GUI (And it will let you close click the close button) I am using a Mac and everything works fine.
I was analyzing your case and we BOTH did something we didn't notice after.
When you use the "root.destroy" command, you should add parenthesis after.. Had the same thing happening to me
Added root.destroy ( ) and worked flawlessly. <-- Use it without SPACES
Hope it helps someone in the future!
I am aware that there are commands such as .destroy() , .exit() etc. However, when taking these out of the 'command' from the button parameter of what action to take when pressed, they don't work.
My scenario is when a user logins successfully, the Tkinter window including its widgets should close whilst short after a GUI in pygame opens. I just don't want the Tkinter window to be there once I don't need it ever again whilst also not exiting Python. I don't want a button as I want the process to be automatic.
The thing that baffles me is why when taking out this command to be on its own, it does not work:
Button(root, text="Quit", command=root.destroy).pack() #works
root.destroy() #don't works
Without seeing more of the source code, I guess the problem is based on where you call root.destroy()
If it comes after the blocking tk.mainloop(), it will never be reached. Please read Tkinter understanding mainloop regarding this issue.
A possible solution based on the above:
while True:
tk.update_idletasks()
tk.update()
if login_successful: # or whatever your login check looks like
root.destroy()
You replace the mainloop with your custom loop, which includes a check for a successful login.
I created a minimal Tkinter GUI as follows:
import Tkinter
root = Tkinter.Tk()
root.mainloop()
Everything is fine if I run the above code on RHEL5, except that the maximize button does not work properly(resizing is available). If I click the button, the window does not expand to occupy the whole screen. And I belive this issue is platform-specific, because there is no such issue for the same code on Windows.
Does anyone know the reason for this? Is there any solution? Thanks!
It works for me in Fluxbox. Try putting something in root like a label so it has something to display. Probably won't make any difference but worth a try.