Python 2.7和Tkinter/tkk:笔记本标签的文本顶部对齐 [英] Python 2.7 and Tkinter/tkk: Notebook tabs text top-aligned

查看:147
本文介绍了Python 2.7和Tkinter/tkk:笔记本标签的文本顶部对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,运行代码时,我的tkk笔记本选项卡中的字体是顶部对齐的.我在使用python 2.7的macOS 10.11.6上.看起来是这样的:

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:

考虑到ttk的本来面目,这很麻烦.

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?

这是我的代码:

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]

这篇关于Python 2.7和Tkinter/tkk:笔记本标签的文本顶部对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆