亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 tkinter 窗口上顯示實時鼠標位置

如何在 tkinter 窗口上顯示實時鼠標位置

慕萊塢森 2023-06-06 15:07:33
我想知道是否有辦法在 tkinter 窗口上繼續顯示實時鼠標位置。我知道如何找到鼠標坐標。x, y = win32api.GetCursorPos() mousecords = Label(self.root, text='x : ' + str(x) + ', y : ' + str(y)) mousecords.place(x=0, y=0)但是我需要標簽在鼠標移動時不斷更新。幫助將不勝感激 謝謝!
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

這只會在Label鼠標位于 tkinter 窗口內時更新:


無需使用 win32api,tkinter 內置了它。我們可以將一個函數綁定到root的<Motion>鍵上,并使用給定的位置參數event來檢索鼠標的坐標。


from tkinter import Tk, Label


root = Tk()

label = Label(root)

label.pack()

root.bind("<Motion>", lambda event: label.configure(text=f"{event.x}, {event.y}"))

root.mainloop()


查看完整回答
反對 回復 2023-06-06
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

您可以使用它after()來定期獲取鼠標坐標并更新標簽。


下面是一個例子:


import tkinter as tk

import win32api


root = tk.Tk()


mousecords = tk.Label(root)

mousecords.place(x=0, y=0)


def show_mouse_pos():

    x, y = win32api.GetCursorPos()

    #x, y = mousecords.winfo_pointerxy() # you can also use tkinter to get the mouse coords

    mousecords.config(text=f'x : {x}, y : {y}')

    mousecords.after(50, show_mouse_pos) # call again 50ms later


show_mouse_pos() # start the update task

root.mainloop()


查看完整回答
反對 回復 2023-06-06
  • 2 回答
  • 0 關注
  • 255 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號