Как изменить геометрию окна в tkinter, используя after? - PullRequest
0 голосов
/ 28 января 2019

Чего я хочу добиться, так это отобразить изображение в другом окне приложения относительно его координат.Например, он должен отображаться на 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, чтобы увидеть, как работает эта функция, и она вызывается только один раз в начале.

1 Ответ

0 голосов
/ 28 января 2019

ОК, я нашел ответ.Произошла ошибка в функции.Обратный звонок не работал.Правильный код:

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...