I'm using tkinter in python for mac.
Simply put, I want to override the "minimize" button's functionality.
I'm already overriding the "X" button functionality in this fashion:
root = Tk()
root.protocol('WM_DELETE_WINDOW', doClose)
What I tried:
I looked into the WM states and there is no WM_ICONIFY_WINDOW or WM_MINIMIZE_WINDOW. I also tried working with WM_SAVE_YOURSELF but couldn't catch any interrupt.
what's the best known way of making tkinter do what I want when people hit "minimze" rather than the default settings?
Thanks!
There is no standard WM_PROTOCOL message for minimization. It seems, the best solution - catching <Unmap> events:
from Tkinter import *
root = Tk()
def callback(event):
print event
frame = Frame(root, width=100, height=100)
frame.bind("<Unmap>", callback)
frame.pack()
root.mainloop()
Related links: 1, 2, 3
Related
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
In creating a Python Tkinter program, I wish to create a button that will close the program. I have tried the
#with master = Tk()
master.quit()
method. And it did absolutely nothing to my program - apart from stopping anything from working, although I received no Tracebacks.
The other method I have tried is:
#with master = Tk()
master.destroy()
This again did nothing to my program - it did give me a traceback error though which was:
_tkinter.TclError: can't invoke "button" command: application has been destroyed
My full code is:
from tkinter import *
master = Tk()
exitbutton = Button(master,text="Exit",(all the other personalization stuff here),command=(master.quit()))
#or I used master.destroy() in the command area.
exitbutton.grid(column=0,row=0)
None of the above methods have worked.
Many Thanks
(For the future)
You must pass the function's name rather than as a callable:
from tkinter import *
master = Tk()
exitbutton = Button(master,text="Exit",command=master.destroy)##dont include the parentheses
##or I used master.destroy() in the command area.
exitbutton.grid(column=0,row=0)
This should fix your problem.
Problem:
The only problem is that you are using parentheses() while passing the function(exit or destroy) to the Button as a command, Which causes it to be executed at the point where it is defined.
Solution:
The solution is to remove the parentheses() while passing the function(exit or destroy) to the Button as a command.
Fixed Code:
from tkinter import *
master = Tk()
exitbutton = Button(master, text="Exit", command=master.quit) # you can also use master.destroy
exitbutton.grid(column=0, row=0)
master.mainloop()
Tip:
As importing all(*) is not a good practice, you should import tkinter as tk
or as anything you want. The only change you will is to add tk. before each object belonging to tkinter.
Then your code will be as follows.
Best Practice:
import tkinter as tk
master = tk.Tk()
exitbutton = tk.Button(master, text="Exit", command=master.quit) # you can also use master.destroy
exitbutton.grid(column=0, row=0)
master.mainloop()
You want to pass a function object into the command keyword, so don't use parentheses. Also you should be using the destroy function for TKinter.
exitbutton = Button(master,text="Exit",(all the other personalization stuff here),command=master.destroy)
I can't find any solution, so I hope you will help me.
I'm using in my project entry widgets and wm_state('iconic') and wm_state('normal) function. Unfortunatelly after restoring tkinter main window by using wm_state('normal) function I'm unable to use entry widget. Manually minimizing and restoring the window solves the problem. Do you have any idea, how can I avoid it? Below is the testing code I prepared for better understanding. Thank you for the help. I thought root.withdraw() is the solution, but unfortunately, program disappears then from the taskbar, what is not good in my case.
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('200x200')
def test_function():
root.wm_state('iconic')
time.sleep(0.5)
root.wm_state('normal')
button = tk.Button(root, text='MINIMIZE BUTTON', command=test_function)
button.grid(row=0, column=0)
entrywidget = tk.Entry(root, width='10')
entrywidget.grid(row=1, column=0)
entrywidget.focus()
root.mainloop()
Using:
root.withdraw()
root.deiconify()
Instead of:
root.wm_state('iconic')
root.wm_state('normal')
Solves the entry widget problem, but program is no longer visible on the taskbar after root.withdraw() until root.deiconify() method call.
I am working on a project using Tkinter library for making a GUI. This GUI will be displayed on a touch screen using raspberry pi 3.
I want to prevent user from exiting or minimising the program.
Is there any way to disable or remove the title bar? Or is there a better way to achieve this?
Since you mentioned a raspberry pi I suppose you are using Linux. In this case you can use root.attributes('-type', 'dock') (assuming your Tk instance is called root). This way, your window will have no decoration (so no close or minimize buttons) and will be always on top. If you don't want it always on top, you can use type 'splash' instead. In any case, you will need to use focus_force to be able to get keyboard focus.
import tkinter as tk
root = tk.Tk()
root.attributes('-type', 'dock')
root.geometry('200x200')
tk.Entry(root).pack()
root.focus_force()
root.mainloop()
Otherwise, you can prevent the window from being closed by setting the 'WM_DELETE_WINDOW' protocol and redisplay the window each time it is minimized:
import tkinter as tk
root = tk.Tk()
def unmap(event):
if event.widget is root:
root.deiconify()
root.protocol('WM_DELETE_WINDOW', lambda: None) # prevent closing
root.bind('<Unmap>', unmap) # redisplay window when it's minimized
root.mainloop()
root = tk.Tk()
root.wm_attributes('-type', 'splash')
For more details go to this link: Remove titlebar without overrideredirect() using Tkinter?
I am trying to delete text inside a text box after waiting 5 seconds, but instead the program wont run and does sleep over everything else. Also is there a way for me to just make my textbox sleep so i can run other code while the text is frozen?
from time import time, sleep
from Tkinter import *
def empty_textbox():
textbox.insert(END, 'This is a test')
sleep(5)
textbox.delete("1.0", END)
root = Tk()
frame = Frame(root, width=300, height=100)
textbox = Text(frame)
frame.pack_propagate(0)
frame.pack()
textbox.pack()
empty_textbox()
root.mainloop()
You really should be using something like the Tkinter after method rather than time.sleep(...).
There's an example of using the after method at this other stackoverflow question.
Here's a modified version of your script that uses the after method:
from time import time, sleep
from Tkinter import *
def empty_textbox():
textbox.delete("1.0", END)
root = Tk()
frame = Frame(root, width=300, height=100)
textbox = Text(frame)
frame.pack_propagate(0)
frame.pack()
textbox.pack()
textbox.insert(END, 'This is a test')
textbox.after(5000, empty_textbox)
root.mainloop()
You can emulate time.sleep in tkinter. For this we still need to use the .after method to run our code alongside the mainloop, but we could add readability to our code with a sleep function. To add the desired behavior, tkinter provides another underestimated feature, wait_variable. wait_variable stops the codeblock till the variable is set and thus can be scheduled with after.
def tksleep(t):
'emulating time.sleep(seconds)'
ms = int(t*1000)
root = tk._get_default_root('sleep')
var = tk.IntVar(root)
root.after(ms, var.set, 1)
root.wait_variable(var)
Real world examples:
update a Label to display a clock while-loop
animated writing nested for-loops
Limitation:
tkinter does not quit while tksleep is used.
Make sure there is no pending tksleep by exiting the application.
Using tksleep casually can lead to unintended behavior
UPDATE
TheLizzard worked out something superior to the code above here. Instead of tkwait command he uses the mainloop and this overcomes the bug of not quitting the process as described above, but still can lead to unintended output, depending on what you expect:
import tkinter as tk
def tksleep(self, time:float) -> None:
"""
Emulating `time.sleep(seconds)`
Created by TheLizzard, inspired by Thingamabobs
"""
self.after(int(time*1000), self.quit)
self.mainloop()
tk.Misc.tksleep = tksleep
# Example
root = tk.Tk()
root.tksleep(2)
What's the Linux version of "<Control-Shift-u>" for keybindings in Tkinter? You might be tempted to think it's exactly that, but, alas, it does not seem to be. For instance, the Linux version of "<Control-Shift-Tab>" is "<Control-ISO_Left_Tab>". I've searched and haven't found any documentation for this.
The following will do what you want:
from Tkinter import *
def proof(event=None):
print 'ping'
root = Tk()
frame = Frame(root, height=100, width=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-U>', proof)
frame.pack()
root.mainloop()
The u becomes capitalized because of the shift modifier and you want to capture the KeyPress event.