我想要实现的是在另一个应用程序窗口上显示与其坐标相关的图像。例如,它应该向下显示100像素,并向左上角显示100像素。我正在使用透明窗口的图像,它显示图像正确,但它不更新它的坐标。
下面是我的代码:
import tkinter as tk # Python 3
from functions import window_coordinates   # it takes x, y, width, heigth of the window
x, y, w, h = window_coordinates("window_name")
def update_coordinates():
    global x, y, w, h
    x, y, w, h = window_coordinates("window_name")
    root.geometry("+{}+{}".format(x + 100, y + 100))
    print("update_coordinates function")
root = tk.Tk()
# The image must be stored to Tk or it will be garbage collected.
root.image = tk.PhotoImage(file='path/to/image.png')
label = tk.Label(root, image=root.image, bg='white')
root.overrideredirect(True)
root.geometry("+{}+{}".format(x+100, y+100))
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
label.after(100, update_coordinates)
label.mainloop()谢谢你的帮助。
EDIT1:我将root.geometry("+{}+{}".format(x + 100, y + 100))放在函数中,但它没有帮助。此外,我还添加了print语句来查看此函数是如何工作的,并且它在开始时只被调用了一次。
发布于 2019-01-28 22:52:50
好了,我找到答案了。函数中有一个错误。回调不起作用。正确的代码:
import tkinter as tk # Python 3
from functions import window_coordinates
x, y, w, h = window_coordinates("3949206036")
def update_coordinates():
    global x, y, w, h
    x, y, w, h = window_coordinates("3949206036")
    root.geometry("+{}+{}".format(x + 100, y + 100))
    label.after(1, update_coordinates)           #  This addition was needed
root = tk.Tk()
# The image must be stored to Tk or it will be garbage collected.
root.image = tk.PhotoImage(file='d:/Python/Projects/Data-mining/samples/avatar.png')
label = tk.Label(root, image=root.image, bg='white')
root.overrideredirect(True)
root.geometry("+{}+{}".format(x+100, y+100))
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
label.after(1, update_coordinates)
label.mainloop()https://stackoverflow.com/questions/54403949
复制相似问题