gpt4 book ai didi

python - 使用Tkinter时线程停止循环

转载 作者:行者123 更新时间:2023-12-03 13:02:01 24 4
gpt4 key购买 nike

我的代码如下。它旨在在用户单击CTRL + X时弹出tkinter窗口,并在按下CTRL + A时关闭该窗口。问题是,打开窗口后关闭窗口时,lookForKeys中的while循环停止。

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()
任何帮助表示赞赏。

最佳答案

我把函数closeOverlay放在一个线程中,它起作用了。

        if k.is_pressed("ctrl+a") and running == True:
running = False
closeOverlayThread = Thread(target=closeOverlay)
closeOverlayThread.start()

关于python - 使用Tkinter时线程停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65273254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com