简体   繁体   English

线程 & Tkinter /Python3

[英]Threading & Tkinter /Python3

I need help connecting an additional stream in Tkinter.我需要帮助在 Tkinter 中连接一个额外的 stream。 I need to constantly update the cursor coordinates in Label, but I just can't figure it out (+ if possible, you can tell me how to make such a construction: "text", a variable, here: Label (text = "Coordinates:", font = "Arial 12").我需要不断更新 Label 中的 cursor 坐标,但我就是想不通(+如果可能的话,你可以告诉我如何进行这样的构造:“text”,一个变量,这里:ZB021DF6ACCtext =“7654C4745FZ46”(坐标:",字体 = "Arial 12")。

import win32api
from threading import Thread
from tkinter import *
root = Tk()
#Подключение модулей}

#Переменные{
xcoord = "null"
ycoord = "null" 
h_x = 0
h_y = 0
#Переменные}
root.title("Utility")
root.geometry("400x500+100+100")
root.columnconfigure(0, weight=1)
root.resizable(False, False)
#root.iconbitmap('') Иконка
root["bg"] = "grey30"
def coordinates():
    while True:
        h_x, h_y = win32api.GetCursorPos() 
        print(h_x, h_y)

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

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

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

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

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


coord_thread = Thread(target = coordinates)
coord_thread.run()
coord_thread.join()

root.mainloop()  

You may not need a Thread to do this.您可能不需要Thread来执行此操作。

You can use root.after() to re-execute coordinates :您可以使用root.after()重新执行coordinates

# other stuff as before, except the creation of these labels:
select_mode_x = Label(text=h_x, font="Arial 12")
select_mode_x['bg'] = "grey30"
select_mode_x['fg'] = "white"
select_mode_x.place(x="140", y="470")

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

# Moved coordinates function down here
def coordinates():
    h_x, h_y = win32api.GetCursorPos() 
    print(h_x, h_y)
    select_mode_x.config(text=h_x)
    select_mode_y.config(text=h_y)
    root.after(100, coordinates)

coordinates()
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM