python - 更改 tkinter 中笔记本选项卡的宽度,无需

标签 python tkinter

是否可以在 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/

相关文章:

列表的Python列表

c++ - 将 C++/Qt4 示例转换为 PyQt4

python - 根据上次某些条件成立的时间,将一列中的数据与另一行对齐

python - 如何更改在循环中初始化的 Tkinter 按钮的文本

python - tkinter grid 和 pack 在不同类中的问题

python - 使用 httplib 使用 python 上传文件

python - 如何获取特定文件的图标或缩略图

python - Tkinter .after 方法卡住窗口?

python - 为什么将相同的功能绑定(bind)到键盘会改变 tkinter 按钮的工作方式?

python - 如何使用 Python 中的 Tkinter 检查当前是否按下了某个键?