找回密码
 立即注册
selectmodetextroot | 软件设计/软件工程 2022-05-06 282 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
问题
我需要帮助连接 Tkinter 的附加流。我需要不断更新Label中的光标坐标,但我就是想不通(+如果可能的话,你能告诉我如何构造一个像这样的变量:“text”,这里是Label(text= “坐标:”,字体=“Arial 12”)。
  1. import win32api
  2. from threading import Thread
  3. from tkinter import *
  4. root = Tk()
  5. #Подключение модулей}

  6. #Переменные{
  7. xcoord = "null"
  8. ycoord = "null"
  9. h_x = 0
  10. h_y = 0
  11. #Переменные}
  12. root.title("Utility")
  13. root.geometry("400x500+100+100")
  14. root.columnconfigure(0, weight=1)
  15. root.resizable(False, False)
  16. #root.iconbitmap('') Иконка
  17. root["bg"] = "grey30"
  18. def coordinates():
  19.     while True:
  20.         h_x, h_y = win32api.GetCursorPos()
  21.         print(h_x, h_y)

  22. #Лейбл показа координат{
  23. select_mode = Label(text="Координаты:", font="Arial 12")
  24. select_mode['bg'] = "grey30"
  25. select_mode['fg'] = "white"
  26. select_mode.place(x="10", y="470")

  27. select_mode = Label(text='x = ', font="Arial 12")
  28. select_mode['bg'] = "grey30"
  29. select_mode['fg'] = "white"
  30. select_mode.place(x="120", y="470")

  31. select_mode = Label(text=h_x, font="Arial 12")
  32. select_mode['bg'] = "grey30"
  33. select_mode['fg'] = "white"
  34. select_mode.place(x="140", y="470")

  35. select_mode = Label(text='y = ', font="Arial 12")
  36. select_mode['bg'] = "grey30"
  37. select_mode['fg'] = "white"
  38. select_mode.place(x="200", y="470")

  39. select_mode = Label(text=h_y, font="Arial 12")
  40. select_mode['bg'] = "grey30"
  41. select_mode['fg'] = "white"
  42. select_mode.place(x="220", y="470")


  43. coord_thread = Thread(target = coordinates)
  44. coord_thread.run()
  45. coord_thread.join()

  46. root.mainloop()  
复制代码

回答
您可能不需要线程来执行此操作。

您可以使用 root.after() 重新执行坐标:
  1. # other stuff as before, except the creation of these labels:
  2. select_mode_x = Label(text=h_x, font="Arial 12")
  3. select_mode_x['bg'] = "grey30"
  4. select_mode_x['fg'] = "white"
  5. select_mode_x.place(x="140", y="470")

  6. select_mode_y = Label(text=h_y, font="Arial 12")
  7. select_mode_y['bg'] = "grey30"
  8. select_mode_y['fg'] = "white"
  9. select_mode_y.place(x="220", y="470")

  10. # Moved coordinates function down here
  11. def coordinates():
  12.     h_x, h_y = win32api.GetCursorPos()
  13.     print(h_x, h_y)
  14.     select_mode_x.config(text=h_x)
  15.     select_mode_y.config(text=h_y)
  16.     root.after(100, coordinates)

  17. coordinates()
  18. root.mainloop()
复制代码






上一篇:Linux用户模式下退出init的正确方法
下一篇:如何提取“可执行路径”?使用 PowerShell 的所有服务