Tkinter adjusts the width of the column

I've been around and found documents 3 viable option within columnconfigure:

  • minsize
  • pad
  • weight

But what if you want the column to be as small as possible?

I have 3 control buttons that I want to line up next to each other.

I found the work was:

weekly.grid(row = 2, column = 1, sticky = W) monthly.grid(row = 2, column = 1, padx = 75, sticky = W) yearly.grid(row = 2, column = 1, padx = 150, sticky = W) 

Whether there is a "nice" way to do this?

(Maybe it should be said that when using the columns 2 and 3 are separated waaaay too much: - ()

Yours faithfully,

Casper

+4
source share
1 answer

Behavior administrator grid geometry by default is that columns will be as small as possible, so you do not need to do anything (assuming that you are using the correct grid).

The behavior you describe, where too much space between the elements is probably due to the fact that you have more widgets in the same column that are greater. The default column will have the smallest width that will contain the widest element. It or else your code you give a non-zero weight of some of the columns that make them expand.

Before talking about the decision, let me make sure that the method of using the grid to accommodate multiple widgets in one and the same cell is definitely the wrong way to do it. There is absolutely no reason to resort to such a decision. The general rule is that you should never put more than one widget into a cell.

The simplest solution for you - and combine grid package. Put all of your control buttons in a frame and pack them on the left side of the frame. Then place the frame in a grid by using sticky="w" . Then, no matter how large the screen, control buttons will always be stuck at the left of the frame containing them.

Please note that this decision does not violate the rule of thumb, which I mentioned earlier. You put only one widget in a cell: the frame. You can put whatever you want in this internal frame, but from the point of view of the grid at each grid cell has only one widget.

Here is an example of the working base on python 2.7:

 import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() args, ** kwargs) import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() "Choice import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() "Choice import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() "Choice import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() fill = None, expand = False) import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() fill = None, expand = False) import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() fill = None, expand = False) import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() it import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() same column import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() always be "stuck" import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() . import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() the column grows import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() checkbuttons import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() fill = "both", expand = True) import Tkinter as tk class ExampleView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) cbframe = tk.Frame(self) cb1 = tk.Checkbutton(cbframe, text="Choice 1") cb2 = tk.Checkbutton(cbframe, text="Choice 2") cb3 = tk.Checkbutton(cbframe, text="Choice 3") cb1.pack(side="left", fill=None, expand=False) cb2.pack(side="left", fill=None, expand=False) cb3.pack(side="left", fill=None, expand=False) # this entry is for illustrative purposes: it # will force column 2 to be widget than a checkbutton e1 = tk.Entry(self, width=20) e1.grid(row=1, column=1, sticky="ew") # place our frame of checkbuttons in the same column # as the entry widget. Because the checkbuttons are # packed in a frame, they will always be "stuck" # to the left side of the cell. cbframe.grid(row=2, column=1, sticky="w") # let column 1 expand and contract with the # window, so you can see that the column grows # with the window, but that the checkbuttons # stay stuck to the left self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": root = tk.Tk() view = ExampleView(root) view.pack(side="top", fill="both", expand=True) root.wm_geometry("400x200") root.mainloop() 

Of course, you can also put the control buttons in a single column that is often easier, but you may need to other elements in the other rows and columns covered a few have dealt with the weights columns. Since it is not clear what your problem based on your description, the above decision is likely to be the easiest for you.

+8
source

All Articles