0

I'm making a GUI in Python tkinter. How can I align these Raidobuttons so that they all line up in one column and the columns are side by side. In particular I'd like my columns to be left right and center but now this is how they are showing up now:

enter image description here

This is my code for the radio buttons:

selected_device = StringVar()
devices = ['CAN', 'US', 'EU', 'AU']

for device in devices:
    r = Radiobutton(
        root,
        text=device,
        value=device,
        variable=selected_device
    )
    r.pack(anchor="ne")

selected_source = StringVar()
sources = ['Raw Data', 'Encoded Data']

for source in sources:
    r = Radiobutton(
        root,
        text=source,
        value=source,
        variable=selected_source
    )
    r.pack(anchor="nw")

selected_environment = StringVar()
environments = ['Sprint', 'RC', 'Staging']

for environment in environments:
    r = Radiobutton(
        root,
        text=environment,
        value=environment,
        variable=selected_environment
    )
    r.pack(anchor="n")
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
ENV
  • 877
  • 2
  • 10
  • 32
  • [Have a look at this](https://stackoverflow.com/a/63536506/13629335). If you do want more help than this, please provide a [mre]. – Thingamabobs Jun 06 '21 at 18:30
  • You souldn't use the same *variable* for more then one `Radiobutton()`. – Funpy97 Jun 06 '21 at 18:32
  • 1
    @Marino In fact radio buttons are designed to share a common variable, unlike checkbutton for multiple choice. – Thingamabobs Jun 06 '21 at 18:33
  • You're right, just set the `StringVar()` to `None` or all radiobuttons will seems selected. – Funpy97 Jun 06 '21 at 18:44
  • I am not sure if I get what your problem is but try using frames. You will need 3 frames for the 3 sets of radio button columns. Also if you don't want to use frames, you will need to switch to using `.grid` – TheLizzard Jun 06 '21 at 18:47
  • But if each radio button has a separate variable which groups them, then the orientation is N, NE, NW shouldn't that do the trick? How does `.grid` work to center those Radio Buttons into their own columns – ENV Jun 06 '21 at 18:55
  • I think `pack` reserves the entire row for itself. So the next item would have to go below, unless ofcourse you use `side='right'` or something. So if you give a runnable code, then we could help you much easier – Delrius Euphoria Jun 06 '21 at 19:02
  • @Marino: _"You souldn't use the same variable for more then one Radiobutton()"_ - that is completely false. Radiobuttons are specifically designed to share a single variable. That's what gives them their exclusive-choice behavior. – Bryan Oakley Jun 06 '21 at 19:04
  • @BryanOakley My bad, I was confusing with `Checkbutton`. – Funpy97 Jun 06 '21 at 19:10

1 Answers1

2

I'd like my columns to be left right and center but now this is how they are showing up now:

If that is the case, and you want to continue to use pack, the easiest solution is to create three frames, one for the left, one for the right, and one for the center. That, or switch to using grid with three columns.

Using pack

Create three frames, and also use anchor="w" to get the buttons to be aligned to the left of the frame.

left_frame = Frame(root)
middle_frame = Frame(root)
right_frame = Frame(root)

left_frame.pack(side="left", fill="both", expand=True)
middle_frame.pack(side="left", fill="both", expand=True)
right_frame.pack(side="right", fill="both", expand=True)

for device in devices:
    r = Radiobutton(right_frame, ...)
    r.pack(side="top", anchor="w")
...
for source in sources:
    r = Radiobutton(left_frame, ...)
    r.pack(side="top", anchor="w")
...
for environment in environments:
    r = Radiobutton(middle_frame, ...)
    r.pack(side="top", anchor="w")

Using grid

Since you are wanting to create a three-column layout, grid may be the better choice. The key is to give the same weight to all three columns so that extra space is distributed equally. You can also use the uniform option to make sure they all are exactly the same width.

In this case, I recommend a single frame just for the radiobuttons so that their layout is independent of the rest of your UI.

radio_frame = Frame(root)
radio_frame.pack(side="top", fill="both", expand=True)
radio_frame.grid_columnconfigure((0,1,2), weight=1, uniform="equal")

for row, device in enumerate(devices):
    r = Radiobutton(radio_frame, ...)
    r.grid(row=row, column=0, sticky="w")
...
for row, source in enumerate(sources):
    r = Radiobutton(radio_frame, ...)
    r.grid(row=row, column=1, sticky="w")
...
for row, environment in enumerate(environments):
    r = Radiobutton(radio_frame, ...)
    r.grid(row=row, column=2, sticky="w")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685