定义窗口并添加 mainloop() 后 Tkinter 窗口未打开

我试图制作一个改进的自动答题器,具有更多功能,例如:

  1. 按一个键保存光标位置

  2. 按键时执行

  3. 按一下键杀死程序这是代码:

import win32api, win32com

import pyautogui

import tkinter as tk

from time import sleep

import keyboard


win = tk.Tk()

cursor = (0,0)

di = 0


e = tk.Entry()


def geti():

    di = float(e.get())



l = tk.Label(text="Info:").pack()

l1 = tk.Label(text="Press q to stop clicking").pack()

l2 = tk.Label(text="Press c to copy mouse position to execute on").pack()

l3 = tk.Label(text="Press e to start executing").pack()


dl = tk.Label(text="Delay between eack click (s)").pack()

e.pack()

b = tk.Button(text="save delay time",command=geti).pack()


def click(x, y):

    win32api.SetCursorPos((x,y))

    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)

    sleep(di)

    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)


while True:


    if(keyboard.is_pressed('c') == True):

        cursor = pyautogui.position()


    if(keyboard.is_pressed('q') == True):

        break

    

    if(keyboard.is_pressed('e') == True):

        click(cursor[0], cursor[1])

        

win.mainloop()


慕莱坞森
浏览 108回答 1
1回答

精慕HU

你的 while 循环会阻塞 win.mainloop()。您可以使用带有绑定的 tkinters 事件系统,而不是检查按键:win.bind('key', callback)# thanks to @acw1668's comment: added evtwin.bind('e', lambda evt: click(cursor[0], cursor[1]))lambda 是必要的,因为您希望调用函数而不是结果。来自tkinter 文档:如果这样做,Python 将在创建小部件之前调用回调函数,并将函数的返回值传递给 Tkinter。然后,Tkinter 尝试将返回值转换为字符串,并告诉 Tk 在激活按钮时调用具有该名称的函数。这可能不是您想要的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python