【发布时间】:2021-09-24 08:31:08
【问题描述】:
我是 Python 的血腥初学者。我正在学习使用 tkinter pkg 在 python 中创建选项卡。我已成功创建 2 个选项卡,但我无法在两个选项卡中显示文本(标签)。当我在一个选项卡 (heading.pack()) 中仅显示 1 个文本时,它可以完美运行。但是当我在选项卡 2 (heading2.pack()) 中显示另一个文本时,帧 Resulation Broked。我还在Frame 选项中设置了宽度和高度。我想修复resulation(在添加第二个文本resulation之前)。这是之前和之后的截图
Before adding 2nd Text
After adding 2nd Text
这是代码:
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('Tab Widget')
root.geometry('600x400')
book=ttk.Notebook(root)
book.pack(pady=2)
tab1=Frame(book,width=600,height=500,bg='white')
tab2=Frame(book,width=600,height=500,bg='white')
tab1.pack(fill='both',expand=1)
tab2.pack(fill='both',expand=1)
book.add(tab1,text='Tab 1')
book.add(tab2,text='Tab 2')
heading=Label(tab1,text='This is Tab 1',font='arial 20 bold',bg='white')
heading2=Label(tab2,text='This is Tab 2',font='arial 20 bold',bg='white')
heading.pack() #Show Text in Tab 1
heading2.pack() #Show Text in Tab 2
root.mainloop()
【问题讨论】: