【问题标题】:Top-aligned Tkinter labels on macosmacos 上的顶部对齐 Tkinter 标签
【发布时间】:2018-06-04 09:22:20
【问题描述】:

我正在尝试在 Macos (Python 2.7) 上的 Tkinter 中创建一个 GUI 应用程序。我按照教程创建了大纲,但选项卡按钮中的文本是顶部对齐的,即使没有指定该方向的任何内容。我怎样才能让它居中without using a custom theme?

代码如下:

#!/usr/bin/python
from Tkinter import *
import ttk

main = Tk()
main.title('Mie Solver')
main.geometry("640x320")

rows = 20
columns=2
main.rowconfigure(rows, weight=1)
main.columnconfigure(columns, weight=1)

nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=20, rowspan=20, sticky='NESW')
nb.enable_traversal()

model_tab = ttk.Frame(nb)
nb.add(model_tab, text='Model')

solver_tab = ttk.Frame(nb)
nb.add(solver_tab, text='Solver')

cs_tab = ttk.Frame(nb)
nb.add(cs_tab, text='Cross Section')

results_tab = ttk.Frame(nb)
nb.add(results_tab, text='Results')

# Model Tab:
ttk.Label(model_tab, text="Geometry").grid(row=1, column=0, sticky=W)
Radiobutton(model_tab, text="Solid Sphere").grid(row=2, column=0, sticky=W)
ttk.Radiobutton(model_tab, text="Core-Shell").grid(row=3, column=0, sticky=W)
ttk.Radiobutton(model_tab, text="Cylinder").grid(row=4, column=0, sticky=W)
ttk.Separator(model_tab, orient=HORIZONTAL).grid(row=5, column=0, sticky=EW)

main.attributes("-topmost", True)
main.mainloop()

【问题讨论】:

  • 你为什么反对自定义主题?
  • @BryanOakley 因为如果可能的话,我希望界面在不同平台上看起来是原生的。据我了解,如果我做一个自定义样式,外观基本上是固定的,对吧?
  • 您可以先命名您所在的操作系统,然后根据需要应用客户样式。我相信您可以使用import os 然后print(os.name) 来检查您使用的操作系统。将此与您需要具有服装风格的操作系统名称列表结合起来,您应该能够解决这个问题。您也可以使用平台库中的platform.system()

标签: python macos tkinter


【解决方案1】:

这个问题似乎与您使用的 Python 2.7 和 Tcl/Tk 版本有关。在 macOS HightSierra 上使用 Tcl/Tk 8.6.9 在 Python 3.7 中测试您的代码时,选项卡标签正确居中。

无论如何,python 2.7 中的标签标签问题是由于填充造成的,您可以通过编辑笔记本的padding 样式属性来修复它。同时,您还可以修复选中和禁用状态的标签文本颜色以匹配原始 macOS 主题:

...
style = ttk.Style() # create a style instance

# configure the notebook tabs padding
style.configure("TNotebook.Tab", padding=[8, 8, 8, 0])

# configure the label text color for the selected and disabled state
style.map("TNotebook.Tab", foreground=[("selected", 'white'), ("disabled", "grey")])
...

结果如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多