2

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.

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.