turtle.ontimer() equivalent for tkinter - python

I am writing a program that requires the tkinter canvas to update graphics. In its previous form it was written in turtle. The turtle function to update the canvas is
turtle.ontimer()
Is there a tkinter equivalent?

You can use after method:
For example:
root = Tk()
...
root.after(2000, callback) # call `callback` function in 2000 ms.

Related

Cancel user window sizing

After a user manually resizes a tkinter window, it no longer shrinks to fit.
What tkinter command would revert it back to the 'shrink to fit' behavior?
I had a hard time remembering how to this, as it has been so long.
In Tcl use:
wm geometry . {}
Can one of the python people edit the answer and translate this for tkinter, thanks.
#Atlas435 wrote this code for tkinter:
import tkinter as tk
root=tk.Tk()
def normal():
root.geometry('')
b = tk.Button(root,text='shrink', command=normal)
b.pack()
root.mainloop()

Why doesn't Tkinter call the function bound to the root window?

I'm creating a desktop application using Tkinter GUI which uses Control+Alt+F1 hotkey. But unfortunately, Tkinter doesn't call the function, when I press these keys.
I've tried root.bind("<Control-Alt-Key>", function), and it works. I've even tried to use root.bind("<Control-Shift-F1>", function) - and it works, too.
try:
# for Python 3
from tkinter import *
except:
# for Python 2
from Tkinter import *
root = Tk() # create the root window
# create bindings for root window
root.bind("<Control-Alt-F1>", lambda event: print("Ctrl+Alt+F1 pressed!"))
root.bind("<Control-Shift-F1>", lambda event: print("Ctrl+Shift+F1 pressed!"))
root.mainloop() # start the mainloop
Why Tkinter doesn't react to the Control+Alt+F1 keypress?
PS. I'm using Windows 10, Python 3.7.2
Most likely, the bios, OS, or window manager you're running on is intercepting that event before it is sent to tkinter. A classic example is ctrl-alt-delete on windows, which cannot be trapped by ordinary programs.
If that is the case, there's nothing you can do in tkinter to work around that.

Tkinter object sensitivity to mouse-pointer

I am making a program with python using tkinter, but I have a problem. I need to add a code which makes my tkinter object sense when the mouse-pointer is touching it.
Does anyone have recommendations for what I should do? So far, I've been thinking that I could write code which runs like this: If the coordinates of the mouse-pointer are the same as the coordinates of the object, the object would react to that instead.
You can bind to the <Enter> and <Leave> events. They will fire whenever the mouse pointer enters and leaves a widget.
Here's a small demo:
import tkinter as tk
def on_mouse_enter(event):
print("enter...", event.widget)
def on_mouse_leave(event):
print("leave...", event.widget)
root = tk.Tk()
for i in range(10):
label = tk.Label(root, text="Item #{}".format(i), name='label-{}'.format(i))
label.pack()
label.bind("<Enter>", on_mouse_enter)
label.bind("<Leave>", on_mouse_leave)
tk.mainloop()

How to remove title bar in Tkinter program?

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?

Python turtle get tkinter root

Python turtle works with tkinter. How to get the root you know from tkinter?
Like this:
import tkinter
root = tkinter.Tk()
but for turtle.
The top-level widget is available through the winfo_toplevel method of the turtle canvas:
import turtle
canvas = turtle.getcanvas()
root = canvas.winfo_toplevel()
It is of a subtype of Tk:
import tkinter
assert type(root) is turtle._Root
assert isinstance(root, tkinter.Tk)
As pointed out by #das-g
root = turtle.getcanvas().winfo_toplevel()
gives you an object representing the turtle root window.
However, if your use case is to integrate turtle graphics with a full-blown Tkinter application, the explicit approach should be preferred at all times:
from tkinter import *
import turtle
root = Tk()
turtle_canvas = turtle.Canvas(root)
turtle_canvas.pack(fill=BOTH, expand=True) # fill the entire window
protagonist = turtle.RawTurtle(turtle_canvas)
protagonist.fd(100) # etc.
This adds the extra benefit of being able to control position and size of the turtle canvas. Plus, having explicit code helps others understanding it.
turtle.getcanvas()
returns the object you are (I am) looking for.

Categories