是否可以在 tkinter 中更改笔记本选项卡的宽度,而无需在选项卡名称中输入 space
?我尝试过设置宽度,但没有成功。
是否有像 tabwidht
或类似的选项,以便我可以固定选项卡的大小?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style(root)
style.configure('lefttab.TNotebook', tabposition='wn',width=80)
notebook = ttk.Notebook(root, style='lefttab.TNotebook')
f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)
notebook.add(f1, text="frame 1")
notebook.add(f2, text="frame 2 longer")
notebook.grid(row=0, column=0, sticky="nw")
root.mainloop()
最佳答案
定义选项卡文本时使用字符串格式:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
notebook = ttk.Notebook(root)
f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)
notebook.add(f1, text=f'{"frame 1": ^20s}')
notebook.add(f2, text=f'{"frame 2 longer": ^20s}')
notebook.grid(row=0, column=0, sticky="nw")
root.mainloop()
使用您选择的尺寸,而不是“20”。 请小心使用正确的引号字符:
f'{"something": ^20s}'
或者反过来:
f"{'something': ^20s}"
关于python - 更改 tkinter 中笔记本选项卡的宽度,无需,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181368/