Python 2.7 and Tkinter/tkk: Notebook tabs text top-aligned

℡╲_俬逩灬. 提交于 2019-12-14 02:02:48

问题


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:

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()

回答1:


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]



来源:https://stackoverflow.com/questions/41115607/python-2-7-and-tkinter-tkk-notebook-tabs-text-top-aligned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!