确定哪个 Tkinter 小部件位于顶部(或可见) [英] Determine which Tkinter widget is on top (or visible)

查看:19
本文介绍了确定哪个 Tkinter 小部件位于顶部(或可见)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定哪个小部件(在本例中为框架)在顶部(或无论如何可见).

I would like to determine which widget (frame in this case) is on top (or visible anyway).

from Tkinter import *
spam1=0
spam2=1000

def busywork():
    global spam1
    global spam2
    #if frame1 is on top / visible
    spam1=spam1+1
    label1.config(text=str(spam1))

    #if frame2 is on top / visable
    spam2=spam2+1
    label2.config(text=str(spam2))
    root.after(10,busywork)

root=Tk()
frame1=Frame(root)
frame2=Frame(root)

button1=Button(frame1,text="Bring Frame 2 to top",command=frame2.lift)
label1=Label(frame1,text=str(spam1))
button1.pack()
label1.pack()
frame1.place(relx=0,rely=0)


button2=Button(frame2,text="Bring Frame 1 to top",command=frame1.lift)
label2=Label(frame2,text=str(spam2))
button2.pack()
label2.pack()
frame2.place(relx=0,rely=0)
root.after(10,busywork)
root.mainloop()

[这是关闭的]但它适用于窗口而不是框架(或一般的小部件)

[This is close]but it works for windows not frames (or widgets in general)

在我的应用程序中,我有几个框架.每个框架上都有许多标签.繁忙工作功能从机床读取所有这些信息.一直获取所有信息会使整个机器变得缓慢.我只想获取和更新用户可以看到的信息.我可以通过手动保留一个告诉我最后一个 .lift()ed 窗口的全局变量来找到一种方法.但这似乎并不干净.(总共有大约 400 个标签被更新.但是在任何给定时间最多只有大约 60 个可见.并且一些标签"实际上是图形仪表面,因此大量的马力被浪费在更新这些当它们不可见时)

In my application, I have several frames. Each frame has many labels on it. The busywork function reads all of this information from a machine tool. Fetching all of the information all of the time makes the over all machine sluggish. I would like to only fetch and update the information that the user can see. I can see a way to do it by manually keeping a global that tells me the last .lift()ed window. But this does not seem clean. ( In total there are about 400 labels that get updated. However at most there are only about 60 that are visible at any given time. And some of the 'Labels' are actually graphical meter faces, so a lot of horse power is wasted updating these when they are not visible)

推荐答案

当你点击按钮时,你可以设置一些变量来保持标签可见的信息.

When you click button you can set some variable to keep information what label is visible.

您可以将该变量添加到标签:

You can add that variable to label:

frame1 = Frame(...)
frame1.visible = False

或者您可以继续访问可见框架

Or you can keep access to visible frame

visible = frame1

如果所有帧都是带有函数 update 的类,它只能更新该帧中的值

If all frames were classes with function update it could update values only in that frame

visible.update( all_data )

这篇关于确定哪个 Tkinter 小部件位于顶部(或可见)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆