簡體   English   中英

如何使用after更改tkinter中的窗口幾何形狀?

[英]How to change window geometry in tkinter using after?

我要實現的是在另一個應用程序窗口上顯示有關其坐標的圖像。 例如,它應該向下顯示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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM