How to bind mouse clicks to functions in all windows in Tkinter - python

I am trying to bind the middle mouse click to a function:
root = Tk()
def leftclick(self):
print("Yep!")
root.bind("<Button-2>", leftclick)
root.mainloop()
This works, however only on the Tkinter window, any ideas for other windows?

Tkinter has no support for what you ask. Tkinter can only bind functions to windows that it creates. If you want to bind functions to events in other windows you'll have to use some platform-specific third-party library.

Related

what does wait visibility method mean in python?

I would like to know what this method means: root.wait_visibility(root),
In the tikinter app. The intention is to change the visibility of the window. I recently switched from windos to linux and I'm having difficulties in the differences between operating systems, because in windows this same function is different.
code:
import tkinter as tk
root=tk.Tk()
root.geometry("600x400+400+200")
root.title('My GUI Application')
**root.wait_visibility(root)**
root.wm_attributes('-alpha',0.50)

RPI Tkinter Window, I want to use a command like overrideredirect and maintain entry box functionality

When using overrideredirect or something like root.wm_attributes('-type', 'splash') my entry box cannot get focus on user's click.
I am developing a GUI which I want to maintain the style of previous windows and not have this window stand out with the window header. How do I get rid of the RPI window bar but maintain entry box functionality.
When using the splash window type (Linux only), you can make the entry get the keyboard focus by using focus_force(), e.g. binding it to the left click.
import tkinter as tk
root = tk.Tk()
root.wm_attributes('-type', 'splash')
e = tk.Entry(root)
e.pack()
# force focus on left click:
root.bind('<1>', lambda ev: ev.widget.focus_force())
root.mainloop()

How to open a root window, close it and then open another window in tkinter after a few seconds?

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.

Python tkinter main window closes all other windows too, why?

I made a gui programm with Tkinter. In the program are several buttons which open new main tkinter windows but when i click on the 'x' of one of these windows all the other ones close too although they are different windows. Only the Buttons, which cause the new tkinter main windows, are in the same main window.
please help :))
This is how tkinter is designed to work. When you destroy a window, all of its children are destroyed. Since everything is a child of the root window or one of its children, when you destroy the root window all other widgets are destroyed along with it.

Minimize the window tkinter in the windows system tray

I made a GUI with Tkinter in python 3. Is it possible to close the window and have the application stays in the Windows System Tray?
Is there any lib or command within Tkinter for this.
The entire solution consists of two parts:
hide/restore tkinter window
create/delete systray object
Tkinter has no functionality to work with the system tray.
(root.iconify() minimizes to the taskbar, not to the tray)
step 1) (more info) can be done by
window = tk.Tk()
window.withdraw() # hide
window.deiconify() # show
step 2) can be done by site-packages, e.g. pystray
(an example, the same example, and more info)
You can use the wm_protocol specifically WM_DELETE_WINDOW protocol. It allows you to register a callback which will be called when the window is destroyed. This is an simple example:
import tkinter as tk
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", root.iconify)
root.mainloop()
.iconify turns the window into an icon in System Tray.

Categories

Resources