How to make Tkinter columns of equal width when widgets span multiple columns (Python 2.7)

后端 未结 3 1813
Happy的楠姐
Happy的楠姐 2021-01-19 12:22

In the following, the buttons labelled \'ONE\', \'TWO\', and \'THR\' do not get evenly spaced out. It seems to me that the root of the problem is that Tk is assuming a defau

3条回答
  •  耶瑟儿~
    2021-01-19 12:49

    I think you might want to use the sticky option.

    sticky= Defines how to expand the widget if the resulting cell is larger than the widget itself. This can be any combination of the constants S, N, E, and W, or NW, NE, SW, and SE.

    For example, W (west) means that the widget should be aligned to the left cell border. W+E means that the widget should be stretched horizontally to fill the whole cell. W+E+N+S means that the widget should be expanded in both directions. Default is to center the widget in the cell.

    import Tkinter
    
    master = Tkinter.Tk()
    
    Tkinter.Button(master, text='ONE').grid(row=0, column=0, sticky='NW')
    Tkinter.Button(master, text='TWO').grid(row=0, column=1, sticky='NW')
    Tkinter.Button(master, text='THR').grid(row=0, column=2, sticky='NW')
    Tkinter.Button(master, text='FOU').grid(row=1, column=2)
    Tkinter.Text(master).grid(row=1, column=0, columnspan=2)
    
    
    master.mainloop()
    

    Edit

    What does it look like. Mine looks like this evenly spaced except the text widget takes up two columns as specified.

    Does it look like this

提交回复
热议问题