windows 无限while循环崩溃Tkinter GUI

jtoj6r0c  于 2023-01-31  发布在  Windows
关注(0)|答案(1)|浏览(146)

我试着做一个实时显示鼠标坐标的应用程序。我知道pyautogui有displayMousePosition(),但不知何故它不起作用。(我用的是Pycharm)

from tkinter import *
from pyautogui import *
from time import * #I thought the sleep() command would help but didn't work :/
app = Tk()
ms_coor = str(position()) #I storaged the mouse position in a variable.
def update():
    while True:
        global ms_coor
        label1.config(text=ms_coor) #I wanted to update the label without creating a new label in next line.
button1 = Button(app, text="Start", command=update) #Starter button.
button1.grid(row=0, column=0)
label1 = Label(app, text="Please Start")
label1.grid(row=1, column=0)
app.mainloop()
qxgroojn

qxgroojn1#

加上pyautogui.position()
代码片段:

def update():
    x, y = pyautogui.position()
    positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
    
    label1.config(text=positionStr)

相关问题