Я с тех пор возился с этой проблемой и придумал это предварительное (и в значительной степени непроверенное) решение для отображения виджета на нескольких вкладках ttk.Notebook.
Хотя физический родительский элемент виджета постоянно устанавливается при создании виджета, его геометрический родительский элемент (определяющий, где он появляется) МОЖЕТ быть изменен с помощью опции .grid () in_. Таким образом, теоретически должно быть возможно, чтобы виджет появлялся на нескольких вкладках ноутбука, просто меняя его геометрического родителя всякий раз, когда пользователь переключается на новую вкладку (NotebookTabChanged)
Кажется, что следующий код работает как рекламируется, хотя я действительно не проверял его вне игрушечного кода ниже:
import tkinter as tk
import tkinter.ttk as ttk
class myApp(tk.Tk):
def __init__(self):
super().__init__()
# Create the Notebook and bind the tab-select event
self.notebook = ttk.Notebook(self, width=500, height=200)
self.notebook.grid()
self.notebook.bind("<<NotebookTabChanged>>", self.select_tab)
# Create a status bar which displays the name of the currently selected tab
self.status = ttk.Label(self)
self.status.grid(sticky='w')
# Create three frames - one for each of three tabs - and store them in
# a dictionary with the tab names as keys
self.tabs = dict()
self.tabs['PAGE 1'] = ttk.Frame(self.notebook)
self.tabs['PAGE 2'] = ttk.Frame(self.notebook)
self.tabs['PAGE 3'] = ttk.Frame(self.notebook)
# Create the tabs in the notebook
for t in self.tabs.keys():
self.notebook.add(self.tabs[t], text=t, underline=0, sticky='nsew')
# Put a widget on the middle tab, just to have soemthing there
ttk.Label(self.tabs['PAGE 2'],text="A SIMPLE LABEL").grid(row=0,column=0)
# Create a button - this is the widget we wish to appear on all tabs
self.btn = tk.Button(self,text='PRESS ME!',command=self.button_pressed)
# This is the method called when the user selectes a new tab. It
# updates the status bar and moves the button to the new tab.
def select_tab(self, event):
id = self.notebook.select()
name = self.notebook.tab(id, "text")
text = f"--- {name} is currently selected ---"
self.status.config(text=text)
self.btn.grid(row=4,column=0,in_= self.tabs[name])
def button_pressed(self):
print('BUTTON PRESSED')
if __name__ == "__main__":
app = myApp()
app.mainloop()