what does wait visibility method mean in python? - 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)

Related

Tkinter pyimage1 doesn't exist [duplicate]

I am learning about Tkinter and was wondering if it would cause errors if I did the following:
import tkinter as tk #import modules
from tkinter import ttk
parent=tk.Tk() #create first instance
card1="k spades"
card2="k diamonds"
comboform=ttk.Combobox(parent,textvariable='form',values=[card1,card2,"both","neither"])#create combobox input form
comboform.grid(row=0,column=0)#added to grid
parent.geometry("200x200")
parent.mainloop()#displays tkinter window
#window exited
parent=tk.Tk()#new instance created
label=tk.Label(parent,text="hi")#label produced
label.pack()#added to window
parent.mainloop()
If I click the exit cross is that the same as parent.destroy(); is that good practice? I know you're only supposed to run mainloop() once and have one Tk() instance but if it's destroyed is it going to cause problems? It's not like I'm creating a class the produces a Tk() instance, where there's a risk of multiple instances existing at once.
I am hoping to, eventually, have an application running in the IDLE and then have a tkinter window appear, presenting an input widget of some kind. After the user gives their input, the window would close and the user would continue in the main window. But could I then do it again, opening new windows (like the above code) on the provision that the instance of Tk() is destroyed each time?
If you've destroyed the root window and then create a new one, that's perfectly fine.
The problem with creating multiple instances of Tk is that most people don't understand what that actually does. Having multiple instances of Tk is fine as long as you realize that they operate in completely memory spaces and widgets and bindings in one can't interact with widgets and bindings in the other.
All of that being said, the best practice is to create a single root window at the start of the program, and it stays alive until the program exits. If you need additional windows, the best practice is to create instances of Toplevel.

Is there any way to minimize python application to system tray with Tkinter GUI?

I'm making a simple GUI Application using Tkinter(Python). And I want it to minimize to System Tray and keep running. Is there any way to do so?
Based on this answer, you can use root.withdraw() method or packages like pystray.
The entire solution consists of two parts:
Hide/restore tkinter window
Create/delete systray object
1:
#import stuff...
root = tk.Tk()
root.withdraw() #hide
root.deiconify() #show again
Also remember to use mainloop()
2:
You can use packages like pystray,...
I don't know much about pystray or whatever like that, but you can follow its documentation and try its examples.

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

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.

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.

Tkinter Toplevel window and multiple workspaces - place window in originating linux workspace

I run this on Debian Gnome, but I think this is relevant for most UNIX distributions and interfaces:
from time import sleep
from Tkinter import Tk,Toplevel
tk=Tk()
def run():
sleep(3)
Toplevel(tk)
run()
In Gnome, running this and switching a workspace will cause the Toplevel window to appear in your current workspace, instead of the one that actually runs the command. Is there a way to fix this so the new window appears where the command runs? If this is a Gnome specific thing let me know and I'll amend the question and tags.
To be clear: I have an app that can pop these windows up on it's own while working - and I'm doing other things. The sleep in the above is there to emulate that.
This makes sense it would work this way, but I'm wondering if there is any easy bypass? I have an application using Toplevels as "patience, running" windows, and it's annoying when they appear in different workspaces.

Categories

Resources