Reputation: 232
My code is below. It is meant to pop a tkinter window when the user clicks CTRL + X and close that window when CTRL + A is pressed. The problem is that when the window is closed after is opened the while loop in lookForKeys
stops.
import tkinter as tk
from threading import Thread
import keyboard as k
running = False
def main():
global root
print('Opening...')
root = tk.Tk()
root.geometry("340x740+1550+50")
root.title('Custom Mic')
root.resizable(0,0)
root.attributes('-topmost', True)
root.update()
root.mainloop()
def closeOverlay():
print('Closing...')
root.destroy()
root.quit()
def openOverlay():
global overlayThread
print('Opening...')
overlayThread = Thread(target = main)
overlayThread.start()
def lookForKeys():
global running
while True:
print(running)
if k.is_pressed("ctrl+x") and running == False:
running = True
openOverlay()
if k.is_pressed("ctrl+a") and running == True:
running = False
closeOverlay()
if __name__ == "__main__":
mainThread = Thread(target = lookForKeys)
mainThread.start()
Any helped is appreciated.
Upvotes: 2
Views: 121
Reputation: 2075
I put the function closeOverlay
in a thread and it worked.
if k.is_pressed("ctrl+a") and running == True:
running = False
closeOverlayThread = Thread(target=closeOverlay)
closeOverlayThread.start()
Upvotes: 1