'How to aling Tkinter Checkbutton left when using sticky="EW"?

I want to aling dynamicaly created Tkinter Checkbuttons to left while having widgets added by grid with sticky="EW" at the same time. I also want to display a background color. Here is code and example of such case but where widgets are not alinged to left:

from tkinter import *

root = Tk()

row = 0
check_buttons = []
for i in range(10):
    text = str(i)*row
    check = Checkbutton(root, text=text)
    check.grid(row=row, sticky="WE")
    row += 1
    check.config(bg="green")

root.mainloop()

enter image description here

Here is another example where sticky="W" is used instead. Problem is that widget borders are not alinged from the right side.

enter image description here

I need to have these on grid. Anyone have any idea how to aling only button and text to left while expanding entire widget at the same time?



Solution 1:[1]

I figured it out. Adding anchor="w" when creating widget fixes issue. Here is full example code and picture (of what I was looking for):

from tkinter import *

root = Tk()

row = 0
check_buttons = []
for i in range(10):
    text = str(i)*row
    check = Checkbutton(root, text=text, anchor="w")
    check.grid(row=row, sticky="WE")
    row += 1
    check.config(bg="green")

root.mainloop()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heikki