Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align tkinter widgets?

How do I align my Radiobuttons? I can add spaces to test4 but that solution doesn't seem proper. Here's what it looks like at the moment—as you can see text111111 has extra chars. I've tried using padx.

screenshot of current results

My code:

from tkinter import *

class GUI:
    def __init__(self, master):

        self.iconnum = IntVar()
        self.master = master
        self.master.title('test')
        self.master.resizable(width=False, height=False)
        self.master.maxsize(500, 250)
        self.master.minsize(500, 250)

        self.test1 = Radiobutton(master, text="test11111111", variable=self.iconnum,
                                 value=1, )
        self.test2 = Radiobutton(master, text="test2", variable=0, value=2, )
        self.test3 = Radiobutton(master, text="test3", variable=0, value=3, )
        self.test4 = Radiobutton(master, text="test4", variable=0, value=4)
        self.test1.grid(row=2, columnspan=1)
        self.test2.grid(row=2, columnspan=2)
        self.test3.grid(row=2, column=1)
        self.test4.grid(row=3, columnspan=1)

        self.Checker = Radiobutton(master, text="test5", indicatoron=0, height=1, width=35,
                                   value=0, command=self.icon_switcher) #var=Selection)
        self.Turbo = Radiobutton(master, text="test6", indicatoron=0, height=1, width=35,
                                 value=1, command=self.icon_switcher) #var#=Selection)

        self.Checker.grid(row=1)
        self.Turbo.grid(row=1, column=1,   )

    def icon_switcher(self):
        print("Hello")

root = Tk()
gui = GUI(root)

root.mainloop()

1 Answers

You should use the sticky keyword argument in order to align your widgets better when using grid.

import Tkinter as tk

window = tk.Tk()
radio = tk.Radiobutton(window, text="I'm a radio")
radio.grid(column=0, row=0, sticky=tk.N+tk.S+tk.W+tk.E)

window.mainloop()

You can use any combination of the N, S, W and E members of the Tkinter module. This parameter will make your widgets stick to the sides of the cell you have specified, somewhat like justification in text. If your widget is resizable, such as with Button, the widget will also automatically resize to fit the cell if you use all of the N, S, W and E members.

Important to note is that this can only do so much as to make the widgets stick to the edges of the cell. Sometimes it is necessary to actually resize the cell or move your widget to another cell.

In your example image, you have Buttons with a set size that is larger than the default size (the example code you provide is incomplete). This causes the cell, and the whole columns that the cells of these Buttons are in, to become wider. In this case, you might want to use the columnspan keyword argument to divide your column into smaller, resizable, parts, so that your Radiobuttons can be aligned still better.

import Tkinter as tk

window = tk.Tk()
radio_one = tk.Radiobutton(window, text="I'm a radio")
radio_two = tk.Radiobutton(window, text="I'm another radio")
button = tk.Button(window, text="I am a very long button", width=50)
button.grid(row=0, column=0, columnspan=2, sticky=tk.N+tk.S+tk.W+tk.E)
radio_one.grid(column=0, row=1, sticky=tk.N+tk.W)
radio_two.grid(column=1, row=1, sticky=tk.N+tk.W)

window.mainloop()

If you would like more information on what parameters the grid geometry manager can use, I suggest you read this tutorial, I have found it to be very helpful in the past.

As a sidenote, please note that you use the variable keyword argument in the declaration of your Radiobuttons incorrectly. You must pass either a tk.StringVar, tk.IntVar or some other comparable object, as described here.

like image 124
RedFantom Avatar answered Oct 24 '25 15:10

RedFantom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!