Home > Blockchain >  How would I get these two pieces of code to run together?
How would I get these two pieces of code to run together?

Time:01-05

I am looking for a way to hide / show a tkinter window using the key p.

import keyboard
root = tk.Tk()
root.geometry("1000x1000")
greeting = tk.Label(text="Hello, Tkinter.")
greeting.pack(pady=10)
root.mainloop()
while not keyboard.is_pressed('p'):
    root.withdraw()
while not keyboard.is_pressed('p'):    
    root.deiconify()
  • My problem is that I can't get the code to run infinitely without messing up the root.mainloop().
  • I seriously have no idea what to do.
  • The code I'm talking about is after the mainloop.

CodePudding user response:

You have to bind the key to do something. Heres an example:

import tkinter as tk

root = tk.Tk()

def key_presses(e):
    print('q was pressed')

root.bind('q', key_pressed)

The code above prints 'q was pressed', well every time it's pressed.

  •  Tags:  
  • Related