python tkinter库引用tkk,Python 2.7和Tkinter / tkk:笔记本标签的文本顶部对齐

For some reason the font in my tkk notebook tab is top-aligned when running the code. I'm on macOS 10.11.6, using python 2.7. This is what it looks like:

52454e555e699098082332de2fd228b6.png

It's a nuissance considering the otherwise solid native look of ttk.

Does anyone know how the theme/style could be tweaked to resolve this issue, or any other fix entirely?

Here is my code:

import Tkinter as tk

import ttk

win = tk.Tk()

frame = ttk.Frame()

win.title("Python GUI")

tabControl = ttk.Notebook(win)

tab1 = ttk.Frame(tabControl)

tabControl.add(tab1, text='Tab 1')

tab2 = ttk.Frame(tabControl)

tabControl.add(tab2, text="Tab2")

ttk.Label(tab1, text="Hello, this is a tab").grid(column=0, row=0)

ttk.Label(tab2, text="Hello, this is another tab").grid(column=0, row=0)

tabControl.pack(expand=0, fill="both")

win.mainloop()

解决方案

It's a bit difficult to say as I am unable to recreate the error you are experiencing. I think the best option would be to create a custom theme and then to change the layout options for your tabs

import Tkinter as tk

import ttk

win = tk.Tk()

frame = ttk.Frame()

style = ttk.Style()

style.theme_create("custom_tabs", parent="alt", settings={

"TNotebook.Tab": {

"configure": {"padding": [10, 10, 10, 10]}

}})

style.theme_use("custom_tabs")

win.title("Python GUI")

tabControl = ttk.Notebook(win)

tab1 = ttk.Frame(tabControl)

tabControl.add(tab1, text='Tab 1')

tab2 = ttk.Frame(tabControl)

tabControl.add(tab2, text="Tab2")

ttk.Label(tab1, text="Hello, this is a tab").grid(column=0, row=0)

ttk.Label(tab2, text="Hello, this is another tab").grid(column=0, row=0)

tabControl.pack(expand=0, fill="both")

win.mainloop()

Simply change the padding as you need it, the values are as follows:

[left padding, top padding, right padding, bottom padding]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值