简体   繁体   English

在ttk中更新选项卡开关上的框架。

[英]Update frame on tab switch in ttk.Notebook

I have a custom Frame with an update method that I want to call every time the user switches tab in a notebook using python2.7 ttk . 我有一个带有update方法的自定义Frame ,每次用户使用python2.7 ttk在笔记本中切换选项卡时,我都希望调用该python2.7 ttk

My first thought was to intercept some "tab switch event", get the frame from the event and call the update method. 我的第一个想法是拦截一些“选项卡切换事件”,从事件中获取帧并调用update方法。 However, googling this yields no good results. 但是,使用谷歌搜索不会产生好的结果。 I can't seem to catch the event, nor get the frame from a "tab click event" in a good way. 我似乎无法捕捉到该事件,也无法很好地从“选项卡单击事件”中获取框架。 My current solution below works okay but I'm looking for a better alternative. 我目前的以下解决方案可以,但是我正在寻找更好的选择。 (Also with my real update method I get some nasty alloc errors). (还有我真正的更新方法,我会收到一些讨厌的分配错误)。

import ttk
import Tkinter as tk

class MyFrame(ttk.Frame):
    def __init__(self, master, name):
        ttk.Frame.__init__(self, master)
        self.name = name
        self.pack()

    def update(self):
        # Update the contents of the frame...
        print "Updating %s" % self.name

class App():
    def __init__(self):
        root = tk.Tk()

        nb = ttk.Notebook(root)

        f1_name = "Tab1"
        f2_name = "Tab2"
        f1 = MyFrame(nb, f1_name)
        f2 = MyFrame(nb, f2_name)

        nb.add(f1, text=f1_name)
        nb.add(f2, text=f2_name)

        def tab_switch(event):
            if event.widget.identify(event.x, event.y) == "label":
                index = event.widget.index("@%d,%d" % (event.x, event.y))
                title = event.widget.tab(index, "text")

                if title == f1_name:
                    f1.update()
                elif title == f2_name:
                    f2.update()
                # and so on...

        nb.bind("<Button-1>", tab_switch)
        nb.pack(fill="both", expand=True)

        f1.update()  # Initial update of first displayed tab

        root.geometry("200x200")
        root.mainloop()

App()

As you can imagine this becomes very non-DRY as the amount of tabs increases... 可以想象,随着制表符数量的增加,这变得非常不干燥...

You can bind to the <Visibility> or <Map> event of the frame. 您可以绑定到框架的<Visibility><Map>事件。 For example: 例如:

class MyFrame(ttk.Frame):
    def __init__(self, master, name):
        ttk.Frame.__init__(self, master)
        self.name = name
        self.pack()
        self.bind("<Visibility>", self.on_visibility)

    def on_visibility(self, event):
        self.update()

Instead of 代替

def tab_switch():
    # see question...

nb.bind("<Button-1>", tab_switch)

you can do the following trick 您可以执行以下技巧

b.bind("<<NotebookTabChanged>>", 
       lambda event: event.widget.winfo_children()[event.widget.index("current")].update())

This also removes the need to call 这也消除了呼叫的需要

f1.update()  # Initial update of first displayed tab

However, the solution proposed by @BryanOakley might work better when you have different types of frames where you cannot be sure if an .update() method exists. 但是,@ BryanOakley提出的解决方案可能在您具有不同类型的帧(无法确定是否存在.update()方法.update()时更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM