首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用after更改tkinter中的窗口几何形状?

如何使用after更改tkinter中的窗口几何形状?
EN

Stack Overflow用户
提问于 2019-01-28 22:21:26
回答 1查看 1K关注 0票数 1

我想要实现的是在另一个应用程序窗口上显示与其坐标相关的图像。例如,它应该向下显示100像素,并向左上角显示100像素。我正在使用透明窗口的图像,它显示图像正确,但它不更新它的坐标。

下面是我的代码:

代码语言:javascript
复制
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语句来查看此函数是如何工作的,并且它在开始时只被调用了一次。

EN

回答 1

Stack Overflow用户

发布于 2019-01-28 22:52:50

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

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54403949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档