Skip to content
Advertisement

Toggle tkinter window visibility

I’m trying to toggle tkinter window visibility when I click p.

toggle = True

if keyboard.is_pressed('p'):
    toggle = not toggle

if toggle:
    app.wm_attributes("-alpha", 0)
else:
    app.wm_attributes("-alpha", 1)

Advertisement

Answer

Not sure what keyboard is though I am using it is the keyboard library. Anyway you can use tkinter itself for this. Here is an example to set you up:

from tkinter import *

app = Tk()
toggle = True # Initially true

def check(e):
    global toggle 
    if toggle: # If true
        app.attributes('-alpha',0) # Then hide
        toggle = False # Set it to False
    else: # If not true
        toggle = True 
        app.attributes('-alpha',1) # Bring it back

app.bind('<p>',check) # Bind to the 'p' key.

app.mainloop()

Also keep a note that wm_attributes() and attributes() are the same.

Advertisement