2017-05-31 54 views
2

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

+1

请显示[MCVE。你确定你想改变风格而不是改变如何用'grid'或'pack'把它添加到窗口中? –

+0

@JakubBlàha让我知道,如果这对你有用 –

+0

我试图使用'grid',但这并没有帮助。 –

回答

1

你的问题有点不清楚。

下面是一些代码来说明什么包左,右,没有分配将做什么锚e,w和没有分配将做。

这应该给你一个关于如何使用pack vs 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() 

产生的程序应该是这样的: enter image description here

+0

我不能使用锚选项,因为我使用'tkinter.ttk'。使用选项'-anchor'导致错误'_tkinter.TclError:未知选项“-anchor”'。 –

+0

@Jakub:你可以在'.pack()'中定位ttk。我用例子更新了我的答案。 –

+0

非常感谢:) –

1

也许您在寻找anchor选项。这需要代表罗盘上的一个点一个字符串(例如:"w" = "west",这意味着该文本被固定在左)::

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

'_tkinter.TclError:未知选项“-anchor”'我使用'tkinter.ttk'。 –

相关问题