Python 如何在tkinter中使用after更改窗口几何图形?

Python 如何在tkinter中使用after更改窗口几何图形?,python,user-interface,tkinter,Python,User Interface,Tkinter,我想要实现的是在另一个应用程序窗口上显示一个关于其坐标的图像。例如,它应该向下显示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_coordinate

我想要实现的是在另一个应用程序窗口上显示一个关于其坐标的图像。例如,它应该向下显示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语句来查看这个函数是如何工作的,在开始时只调用了一次。

好的,我找到了答案。函数中有一个错误。回拨不起作用。右代码:

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()

您的
更新\u坐标
仅更新您的全局变量,它不会更改
root.geometry()
。谢谢。我将
root.geometry(“+{}+{}.format(x+100,y+100))
放在函数内部,但它仍然不会更新它。我用print语句检查了函数,在脚本开头只调用了一次。请编辑您的问题。没有人会解析所有的评论。真的。这篇文章已被编辑。