有没有办法使用 python 在 tkinter 中保留两个具有不同标签样式的笔记本?
Is there a way to keep two notebooks with different tab style in tkinter using python?
如果是标签或按钮,我知道它是怎么做的。
style=ttk.Style()
style.configure('One.TLabel', font=('Arial', 32))
style.configure('Two.TLabel', font=('Arial', 18))
h1=Label(root, text='Heading', style='One.TLabel')
h2=Label(root, text='Sub-heading', style='Two.TLabel')
h1.pack()
h2.pack()
这有效。
但如果是笔记本标签,假设我想更改标签内边距和字体。同样这个方法不行。
style=ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 14), padding=20)
style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)
# adding book1 tabs
book1=Notebook(root, style='One.TNotebook')
# adding book2 tabs
book2=Notebook(root, style='Two.TNotebook')
book1.pack()
book2.pack()
我们该怎么做?有解决方法吗?
您可以使用ttk.Style()
,对于笔记本如下,为同居在同一个window中的两个不同笔记本定义两种不同的样式:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 24), padding=40)
book1 = ttk.Notebook(root, style='One.TNotebook')
book1.pack()
frame1 = ttk.Frame(book1, width=400, height=200, relief=tk.SUNKEN)
frame2 = ttk.Frame(book1, width =400, height=200, relief=tk.SUNKEN)
book1.add(frame1, text = 'One')
book1.add(frame2, text = 'Two')
style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)
book2 = ttk.Notebook(root, style='Two.TNotebook')
book2.pack()
frame3 = ttk.Frame(book2, width=400, height=200, relief=tk.FLAT)
frame4 = ttk.Frame(book2, width =400, height=200, relief=tk.FLAT)
book2.add(frame3, text='Three')
book2.add(frame4, text='Four')
root.mainloop()
如果是标签或按钮,我知道它是怎么做的。
style=ttk.Style()
style.configure('One.TLabel', font=('Arial', 32))
style.configure('Two.TLabel', font=('Arial', 18))
h1=Label(root, text='Heading', style='One.TLabel')
h2=Label(root, text='Sub-heading', style='Two.TLabel')
h1.pack()
h2.pack()
这有效。
但如果是笔记本标签,假设我想更改标签内边距和字体。同样这个方法不行。
style=ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 14), padding=20)
style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)
# adding book1 tabs
book1=Notebook(root, style='One.TNotebook')
# adding book2 tabs
book2=Notebook(root, style='Two.TNotebook')
book1.pack()
book2.pack()
我们该怎么做?有解决方法吗?
您可以使用ttk.Style()
,对于笔记本如下,为同居在同一个window中的两个不同笔记本定义两种不同的样式:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 24), padding=40)
book1 = ttk.Notebook(root, style='One.TNotebook')
book1.pack()
frame1 = ttk.Frame(book1, width=400, height=200, relief=tk.SUNKEN)
frame2 = ttk.Frame(book1, width =400, height=200, relief=tk.SUNKEN)
book1.add(frame1, text = 'One')
book1.add(frame2, text = 'Two')
style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)
book2 = ttk.Notebook(root, style='Two.TNotebook')
book2.pack()
frame3 = ttk.Frame(book2, width=400, height=200, relief=tk.FLAT)
frame4 = ttk.Frame(book2, width =400, height=200, relief=tk.FLAT)
book2.add(frame3, text='Three')
book2.add(frame4, text='Four')
root.mainloop()