How to place radiobuttons horizontal in python
Asked Answered
F

1

2
AANTAL = [(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"),(6,"6"),]
v= StringVar()
v.set("1")
for text, mode in AANTAL:
    but = Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, text=text, variable=v, value=mode, indicatoron=0)
    but.grid()    

The above code shows some radiobuttons numbered 1 to 6. However, it displays them vertically instead of horizontally. Does anyone know how I could fix this?

I already tried putting row=0 in the grid command but this only stacks the buttons on top of each other instead of spreading them out over a row.

Flay answered 4/8, 2017 at 14:40 Comment(1)
You should read the documentation on the grid command before asking such a basic question. grid has options that let you specify a specific row and a specific column.Caudle
O
1

grid has two options for placing a widget. row and column. You need to specify both.

buttons = []
vars = []
for idx, (text, mode) in enumerate(AANTAL):
    vars.append(StringVar(value="1"))
    buttons.append(Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, text=text, variable=vars[-1], value=mode, indicatoron=0))
    buttons[-1].grid(row=0, column=idx) 

Also, when using loops to create widgets, it is much better to store them in a list because you can access them later in your program.

Obregon answered 4/8, 2017 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.