我有tkinter.Notebook个包含多个标签的标签. 我有tkinter.Frame个"运行箱"与一套控制,我想在几个选项卡共享.具体地说,Test infoTest list应该有它.

enter image description here

目前,我将整个"运行框"复制到每个需要它的选项卡中.它在某种程度上工作得很好,但感觉太过了,而且不正确,因为"Run box"控件背后的代码几乎是相同的.

理想情况下,我希望这个"运行箱"是单一的实例,并动态显示在活动选项卡上.有没有人能建议如何从一个标签上拆卸"Run Box",然后重新连接到不同的标签上?我会在Notebook Switch事件上这么做.

推荐答案

一个小部件一次只能出现在一个位置.如果不在活动选项卡更改时将其从一个选项卡中移除并将其添加到另一个选项卡中,则无法在多个笔记本选项卡之间共享框架.

如果这就是您想要做的,您可以创建一次"Runbox",然后在每个选项卡中添加一个容器作为占位符.您可以向<Visibility>事件添加绑定,以便在占位符变为可见时将Runbox添加到占位符.

这可能会在您切换选项卡时导致一些闪烁.

要做到这一点,关键是使用packin_参数,它允许您将一个小部件打包到另一个小部件中.它还使用lift机制来确保"Runbox"在z顺序上高于占位符.只要Runbox的可见性发生变化,就会通过绑定到<Visibility>事件来重新打包.

这里有一个例子.它创建一个Runbox的实例,并在两个选项卡之间交换它.

import tkinter as tk
from tkinter import ttk

class RunParameters():
    """Parameters to be shared by all tabs"""
    def __init__(self):
        self.magic_num = tk.IntVar(value=0)
        self.iter_count = tk.IntVar(value=1)
        self.duration = tk.IntVar(value=0)
        self.result = "fail"
        self.repeat = tk.BooleanVar(value=True)

class Runbox(tk.Frame):
    def __init__(self, parent, run_parameters):
        super().__init__(parent)
        radiobuttons = []
        for i in (range(8)):
            rb = tk.Radiobutton(
                self, text=i, variable=run_parameters.magic_num, value=i
            )
            radiobuttons.append(rb)

        itercount_spinbox = tk.Spinbox(
            self, from_=1, to=100,
            textvariable=run_parameters.iter_count
        )
        duration_spinbox = tk.Spinbox(
            self, from_=0, to=100,
            textvariable=run_parameters.duration
        )

        radiobuttons[0].grid(row=0, column=0)
        radiobuttons[1].grid(row=0, column=1)
        radiobuttons[2].grid(row=0, column=2)
        radiobuttons[3].grid(row=0, column=3)
        radiobuttons[4].grid(row=1, column=0)
        radiobuttons[5].grid(row=1, column=1)
        radiobuttons[6].grid(row=1, column=2)
        radiobuttons[7].grid(row=1, column=3)
        itercount_spinbox.grid(row=3, column=0, columnspan=4)
        duration_spinbox.grid(row=4, column=0, columnspan=4)

class TestInfo(tk.Frame):
    def __init__(self, parent, runbox):
        super().__init__(parent)
        self.runbox = runbox
        self._container = tk.Frame(self)
        label = tk.Label(self, text="Something unique to the Test Info tab")
        self._container.pack(side="top", fill="x")
        label.pack(side="bottom", fill="both", expand=True)
        self.bind("<Visibility>", self.on_vis)

    def on_vis(self, event):
        if self.winfo_viewable():
            self.runbox.pack(in_=self._container, fill="both", expand=True)
            self.runbox.lift()

class TestList(tk.Frame):
    def __init__(self, parent, runbox):
        super().__init__(parent)
        self.runbox = runbox
        self._container = tk.Frame(self)
        listbox = tk.Listbox(self)
        listbox.insert("end", "Test 1", "Test 2", "Test 3")
        self._container.pack(side="top", fill="x")
        listbox.pack(side="bottom", fill="both", expand=True)
        self.bind("<Visibility>", self.on_vis)

    def on_vis(self, event):
        if self.winfo_viewable():
            self.runbox.pack(in_=self._container, fill="both", expand=True)
            self.runbox.lift()

class Trace32(tk.Frame):
    pass

class TMEConfig(tk.Frame):
    pass

root = tk.Tk()
params = RunParameters()

nb = ttk.Notebook(root)
nb.pack(fill="both", expand=True)

runbox = Runbox(nb, params)

test_info = TestInfo(nb, runbox = runbox)
test_list = TestList(nb, runbox = runbox)
trace32 = Trace32(nb)
tme_config = TMEConfig(nb)

nb.add(test_info, text="Test Info")
nb.add(test_list, text="Test List")
nb.add(trace32, text="Trace32")
nb.add(tme_config, text="TME config")

root.mainloop()

Python相关问答推荐

将轨迹优化问题描述为NLP.如何用Gekko解决这个问题?当前面临异常:@错误:最大方程长度错误

三个给定的坐标可以是矩形的点吗

在Python中处理大量CSV文件中的数据

PMMLPipeline._ fit()需要2到3个位置参数,但给出了4个位置参数

Python解析整数格式说明符的规则?

Django REST Framework:无法正确地将值注释到多对多模型,不断得到错误字段名称字段对模型无效'<><>

利用Selenium和Beautiful Soup实现Web抓取JavaScript表

让函数调用方程

在嵌套span下的span中擦除信息

使用Python和文件进行模糊输出

Tkinter菜单自发添加额外项目

Polars asof在下一个可用日期加入

在Python中使用if else或使用regex将二进制数据如111转换为001""

Pandas:填充行并删除重复项,但保留不同的值

不允许 Select 北极滚动?

在Django中重命名我的表后,旧表中的项目不会被移动或删除

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?

如何在SQLAlchemy + Alembic中定义一个"Index()",在基表中的列上

Pandas:计数器的滚动和,复位

BeatuifulSoup从欧洲志愿者服务中获取数据和解析:一个从EU-Site收集机会的小铲子