You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter Notebook问题:从Tab2激活Tab1的按钮

解决Tkinter Notebook标签页间控件状态同步问题

你的代码中直接通过读取按钮的relief属性判断状态,当Tab1处于非激活标签页时,Tkinter对未显示控件的属性读取/修改可能无法立即触发界面刷新,导致状态切换看起来无效。改用变量跟踪状态的方式可以彻底解决这个问题。

修改后的完整代码

#!/usr/bin/python3.9
    
import tkinter as tk
from tkinter import ttk
    
class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        self.labelA = ttk.Label(self, text = "This is MainFrame")
        self.labelA.grid(column=1, row=1, padx = 30, pady = 30)
    
        self.buttonA = tk.Button(self, text="test", command=self.button)
        self.buttonA.grid(column=1, row=2, pady=20)
        self.buttonA.config(relief="raised", bg="gray", font='Helvetica 14', width='4', height='1', bd='8')
        # 添加变量跟踪按钮激活状态
        self.button_active = False

        self.buttonB = tk.Button(self, text="activate TEST button", command=self.button)
        self.buttonB.grid(column=1, row=3, pady=20)

        self.buttonC = tk.Button(self, text="get time from Tab 2", command=self.get_time)
        self.buttonC.grid(column=1, row=4, padx=40, pady=20)


    def button(self):
        # 基于变量判断状态,而非读取控件属性
        if not self.button_active:
            self.buttonA.config(relief='sunken', text="ON", bg='green', font='Helvetica 14',  width='4', height='1', bd='8')
            self.button_active = True
        else:
            self.buttonA.config(relief="raised", text="test", bg="gray", font='Helvetica 14', width='4', height='1')
            self.button_active = False

    def get_time(self):
        self.Frame2.get_time()

            
class SubFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)
    
        self.labelB = ttk.Label(self, text = "This is SubFrame")
        self.labelB.grid(column=1, row=2, padx = 30, pady = 30)
    
        self.buttonB = tk.Button(self, text="activate button in MainFrame", command=self.test)
        self.buttonB.grid(column=1, row=3, padx=40, pady=20)

        self.buttonD = tk.Button(self, text="get time", command=self.get_time)
        self.buttonD.grid(column=1, row=5, padx=40, pady=20)
       
        style = {'fg': 'black', 'bg': 'white', 'font': 'Helvetica 14 bold', 'width':'6', 'bd':'2', 
                        'highlightbackground':"black", 'justify':'center', 'relief':'sunken',
                        'insertontime': '0', 'takefocus': '0' }
    
        self.entry1_var=tk.StringVar()
        self.entry1=tk.Entry(self, textvariable=self.entry1_var, width=5, justify="center",)
        self.entry1.grid(column=1, row=4, padx=(0, 0), pady=(0,10))
        self.entry1.configure(style)

    def get_time(self):
        print (self.entry1.get())
    
    def test(self):
        self.Frame1.button()

    
class App(tk.Tk):
     def __init__(self):
        super().__init__()

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
    
        self.geometry('800x500')
        self.title("TABS")

        self.notebook = ttk.Notebook(self)

        self.Frame1 = MainFrame(self.notebook)
        self.Frame2 = SubFrame(self.notebook)

        self.Frame1.Frame2 = self.Frame2
        self.Frame2.Frame1 = self.Frame1

        self.notebook.add(self.Frame1, text='TAB 1')
        self.notebook.add(self.Frame2, text='TAB 2')

        self.notebook.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)

    
if __name__ == '__main__':
        
    app = App()
    app.mainloop()

核心修改说明

  • 在MainFrame的初始化方法中添加self.button_active = False,用布尔变量记录按钮的激活状态,替代之前读取按钮relief属性的方式。
  • 修改button方法,基于self.button_active的值切换按钮外观和状态,避免了因标签页未激活导致的属性读取异常。

修改后,无论Tab1是否处于当前显示状态,点击Tab2的按钮都能正确切换Tab1中Test按钮的状态,切换回Tab1时即可看到绿色的ON状态。

内容的提问来源于stack exchange,提问作者bob_the_bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:02:03