2017-05-31 105 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'。 –

相關問題