标签小部件仅显示在最后一个下部标签上,而不显示在所有下部标签上。
我有2个独立的笔记本,我正在学习如何在所有下部选项卡的上部选项卡中放置特定的小部件。我认为问题出在这里。
def button_display_function(self,):
for name in self.parent.lower_tabs:
for name in self.parent.upper_tabs[:1]:
import tkinter as tk
from tkinter import ttk
from functools import partial
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("class basic window")
self.config(background="LightBlue4")
self.Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
self.lower_tabs = ["Week 1", "Week 2"]
self.lower_tabs_dict = {}
self.upper_tabs = ["Final", "Requests", "Control"]
self.upper_tabs_dict = {}
self.buttons_dict = {}
self.create_lower_tabs()
for name in self.lower_tabs:
self.create_upper_tabs(name) #looping to place grid into all the upper tabs
for name in self.upper_tabs[:2]: #["Final", "Requests"]
self.create_grid(name)
self.week1 = Week(self)
self.week1.pack()
def create_lower_tabs(self):
style1 = ttk.Style()
style1.configure("down.TNotebook", tabposition="sw")
self.tab_control_lower = ttk.Notebook(self, width=1200, height=550, padding=0, style="down.TNotebook")
for name in self.lower_tabs:
self.lower_tabs_dict[name] = tk.Frame(self.tab_control_lower, bg='old lace')
self.tab_control_lower.add(self.lower_tabs_dict[name], text=name)
self.tab_control_lower.pack(fill=tk.BOTH, expand=1)
def create_upper_tabs(self, name):
self.tabControl_upper = ttk.Notebook(self.lower_tabs_dict[name], width=1100, height=300, padding = 0) #create upper notebook and its name
for name in self.upper_tabs:
self.tab=tk.Frame(self.tabControl_upper, bg='thistle') # each tab sits in a frame widget
self.tabControl_upper.add(self.tab, text=name)
self.upper_tabs_dict[name] = self.tab #add the tab widgets to the dict.
self.tabControl_upper.pack(fill=tk.BOTH, expand=1)
####----------create base label grids--------------####
def create_grid(self, name):
for i in range (9):
for day in range(len(self.Days)+2):
self.label = tk.Label(self.upper_tabs_dict[name], relief="ridge", width=11, height=3)
self.label.grid(row=i, column=day, sticky='nsew')
def add_weekday_names(self, name):
#------add in the weekday name labels------------------
i=2
for day in self.Days:
self.label=tk.Label(self.upper_tabs_dict[name], text=day, fg="red", bg="snow")
self.label.grid(row=0, column=i)
self.labeldays_dict[day]=self.label
i +=1
class Week(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.button_display_function()
####-------widgets-----------------
def button_display_function(self):
for name in self.parent.lower_tabs:
for name in self.parent.upper_tabs[:1]:
self.button = tk.Button(self.parent.upper_tabs_dict[name], text="Calculate", fg="salmon")
self.button.grid(row=10, column=8, sticky='e')
if __name__ == '__main__':
Application().mainloop()
“计算”按钮应同时出现在第1周和第2周的“最终”标签下。现在,它仅显示在“第2周”标签上。