在 Tab1 上,我有一个“测试”按钮,可以通过直接按“测试”按钮或按“激活按钮”来“打开”该按钮。在选项卡 2 中,我想按“选项卡 1 上的激活按钮”,并使选项卡 1 上的“测试”按钮“打开”并显示绿色 ON 状态。如果我删除“笔记本”并将所有内容都放在一页上,它将可以工作,但是当我有两个选项卡时,我无法通过从选项卡 2 激活它来显示处于打开状态的“测试”按钮。
#!/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.buttonB = tk.Button(self, text="activate TEST button", command=self.button) #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 self.buttonA.config('relief')[-1] == 'raised':
self.buttonA.config(relief='sunken', text="ON", bg='green', font='Helvetica 14', width='4', height='1', bd='8')
else:
if self.buttonA.config('relief')[-1] != 'raised':
self.buttonA.config(relief="raised", text="test", bg="gray", font='Helvetica 14', width='4', height='1')
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)
## ENTRY STYLE
style = {'fg': 'black', 'bg': 'white', 'font': 'Helvetica 14 bold', 'width':'6', 'bd':'2',
'highlightbackground':"black", 'justify':'center', 'relief':'sunken',
'insertontime': '0', 'takefocus': '0' } # highlightcolor':"red"
self.entry1_var=tk.StringVar()
#self.entry1_var.set("00:00")
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.resizable(False, False)
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) #pack(expand = 1, fill ="both")
if __name__ == '__main__':
app = App()
app.mainloop()
您的代码可在 Python 版本 3.12.7 及以上版本中运行。
建议升级到Python 3.12.7。
从 Tab2 我想按“Tab 1 上的激活按钮”并拥有 选项卡 1 上的测试按钮“打开”并显示绿色 ON 状态。
self.buttonB.config
方法中插入 button(self)
。片段:
def button(self):
if self.buttonA.config('relief')[-1] == 'raised':
self.buttonA.config(relief='sunken', text="ON", bg='green', font='Helvetica 14', width='4', height='1', bd='8')
self.buttonB.config(text="ON") # <== Add this.
else:
if self.buttonA.config('relief')[-1] != 'raised':
self.buttonA.config(relief="raised", text="test", bg="gray", font='Helvetica 14', width='4', height='1')
self.buttonB.config(text="activate TEST button") # <== Add this.
屏幕截图(仅显示
buttonB
激活):