【问题标题】:How to align checkbutton in ttk to the left side如何将ttk中的checkbutton对齐到左侧
【发布时间】:2017-11-01 12:26:40
【问题描述】:

我想配置tkinter.ttk.Checkbutton 的样式style.configure('TCheckbutton', background=theme, foreground='white', anchor=tkinter.W) 以将该复选按钮对齐到左侧,因为现在它位于中心。非常感谢每一个答案:)

【问题讨论】:

  • 请显示minimal reproducible example。您确定要更改样式而不是更改使用grid 或pack 将其添加到窗口的方式吗?
  • @Jakub Blàha 让我知道这是否适合你
  • 我尝试使用grid,但没有帮助。

标签: python tkinter alignment ttk


【解决方案1】:

也许您正在寻找anchor 选项。它需要一个代表罗盘上一个点的字符串(例如:"w" = "west",表示文本锚定在左侧)::

Label(..., anchor="w").grid(...)

【讨论】:

  • _tkinter.TclError: unknown option "-anchor" 我正在使用tkinter.ttk。
【解决方案2】:

你的问题有点不清楚。

这里有一些代码来说明左、右和无赋值包将做什么以及锚点e、w和无赋值将做什么。

这应该让您更好地了解如何使用 pack 与 anchor 以及何时使用它。

from tkinter import *
import tkinter.ttk as ttk

root = Tk()

packLabel = Label(root)
packLabel.pack(side = LEFT)
packLabel.config(text = "Packed LEFT")

pack2Label = Label(root)
pack2Label.pack()
pack2Label.config(text = "no Pack side")

pack3Label = Label(root)
pack3Label.pack(side = RIGHT)
pack3Label.config(text = "Packed RIGHT")

anchorLabel = ttk.Label(root,width = 50, background = "green", anchor = 'e')
anchorLabel.pack(side = BOTTOM)
anchorLabel.config(text = "anchor = 'e'")

anchor2Label = Label(root,width = 50, background = "orange", anchor = 'w')
anchor2Label.pack(side = BOTTOM)
anchor2Label.config(text = "anchor = 'w'")

anchor3Label = Label(root,width = 50, background = "black", fg = "white")
anchor3Label.pack(side = BOTTOM)
anchor3Label.config(text = "no anchor while packed BOTTOM")

checkButton = ttk.Checkbutton(root)
checkButton.config(text = "Checkbutton anchor = 'w'")
checkButton.pack(anchor = "w") # anchor the pack for ttk.

root.mainloop()

生成的程序应如下所示:

【讨论】:

  • 我不能使用锚选项,因为我使用的是tkinter.ttk。使用选项-anchor 会导致错误_tkinter.TclError: unknown option "-anchor"。
  • @Jakub:您可以在.pack() 中锚定 ttk。我用示例更新了我的答案。
  • 很高兴我能帮上忙。我为此做了一些研究并自己学到了一些东西:)
猜你喜欢
  • 2016-08-31
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多