python - ttk notebook 可以嵌套吗?
问题描述
我正在尝试将一个 ttk 笔记本嵌套在另一个笔记本中,以便我可以有多个选项卡级别。
想象一个上面的笔记本,每个食物组都有一个选项卡,在每个食物组选项卡中,都有一个用于该组食物示例的选项卡。选项卡式层次结构。
ttk笔记本可以吗?我找不到任何处理这个问题的参考资料或例子。
看起来这段代码应该可以工作。我没有收到任何错误,但我看不到二级标签。任何帮助,将不胜感激。
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
nb1 = ttk.Notebook(root)
nb1.pack()
f0 = Frame(nb1)
f0.pack(expand=1, fill='both')
###Make the second notebook
nb2 = ttk.Notebook(f0)
nb2.pack()
#Make 1st tab
f1 = Frame(nb1)
#Add the tab to notebook 1
nb1.add(f1, text="First tab")
#Make 2nd tab
f2 = Frame(nb1)
#Add 2nd tab to notebook 1
nb1.add(f2, text="Second tab")
###Make 3rd tab
f3 = Frame(nb2)
#Add 3rd tab to notebook 2
nb2.add(f3, text="First tab")
###Make 4th tab
f4 = Frame(nb2)
#Add 4th tab to notebook 2
nb2.add(f4, text="Second tab")
root.mainloop()
解决方案
解决了:
这是用符号简化的工作代码。希望其他人会发现它具有启发性。本示例使用 College Program>Terms>Courses 的模型
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
program = ttk.Notebook(root) #Create the program notebook
program.pack()
#Make the terms frames for the program notebook
for r in range(1,4):
termName = 'Term'+str(r) #concatenate term name(will come from dict)
term = Frame(program) #create frame widget to go in program nb
program.add(term, text=termName)# add the newly created frame widget to the program notebook
nbName=termName+'courses'#concatenate notebook name for each iter
nbName = ttk.Notebook(term)#Create the notebooks to go in each of the terms frames
nbName.pack()#pack the notebook
for a in range (1,6):
courseName = termName+"Course"+str(a)#concatenate coursename(will come from dict)
course = Frame(nbName) #Create a course frame for the newly created term frame for each iter
nbName.add(course, text=courseName)#add the course frame to the new notebook
root.mainloop()
推荐阅读
- r - 如何使用 ggplot2 在 R 中制作不连续的轴?
- javascript - App.vue 中的 Templete 未显示在 index.html 中
- c - 给定函数具有整数返回数据类型,如何返回字符?
- r - 有没有用标签替换 value.labels 的功能?R
- amazon-web-services - 多个 terraform 项目共享基础设施
- node.js - 我如何通过 API 使用节点服务器在 mongo db 中存储二维矩阵
- javascript - 如何实现开帘动画
- swift - SwiftUI - 如何在上传到 Firebase 存储之前调整图像大小
- sql - 我试图在结果中检索所有产品及其总销售额
- python-3.8 - 从获取请求中下载多个页面