【问题标题】:How to aling Tkinter Checkbutton left when using sticky="EW"?How to aling Tkinter Checkbutton left when using sticky=\"EW\"?
【发布时间】:2022-12-27 22:37:10
【问题描述】:

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()

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

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?

【问题讨论】:

    标签: tkinter grid sticky tkinter.checkbutton


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      You don't needed do this config when on fly. Just added in line 9 bg='green'

      from tkinter import *
      
      root = Tk()
      
      row = 0
       
      for i in range(10):
          text = str(i)*row
          check = Checkbutton(root, text=text, bg="green")
          check.grid(row=row, sticky="WE")
          row += 1
          
      root.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2022-12-19
        • 2022-11-09
        • 2022-12-27
        • 2022-12-01
        • 2022-12-01
        • 2022-12-02
        • 2021-04-26
        • 2022-12-19
        • 2022-12-02
        相关资源
        最近更新 更多