2

I would like to have four horizontal buttons in one column (the entire UI is one column). I can't figure out how to do this with the pack geometry manager. I have resorted to having the buttons vertically, but there must be a way to do this. I am new to Python, so my code is pretty simple:

v = IntVar()

Radiobutton(root, text="Red", variable=v, value=1, command = red).grid(row=1) 
Radiobutton(root, text="Blue", variable=v, value=2, command = blue).grid(row=2)
Radiobutton(root, text="Green", variable=v, value=3, command = green).grid(row=3)
Radiobutton(root, text="Other", variable=v, value=4, command = Other).grid(row=4)

if I put them in the same row, they are stacked on top of each other. I read another post about Grouping the buttons, but that was for an Android app.

user1332449
  • 21
  • 1
  • 1
  • 3

1 Answers1

5

When buttons are stacked vertically they are all in one column.
Assuming you meant: "I would like to have four horizontal buttons in one row.",
with each button in a separate column of that row,
my first recommendation would be to use a frame to contain the buttons.

Here's an example using grid with its row and column options:

import Tkinter as tki # tkinter in Python 3

root = tki.Tk()

frm = tki.Frame(root, bd=16, relief='sunken')
frm.grid()

var = tki.StringVar()

mild = tki.Radiobutton(frm, text='Mild', variable=var)
mild.config(indicatoron=0, bd=4, width=12, value='Mild')
mild.grid(row=0, column=0)

medium = tki.Radiobutton(frm, text='Medium', variable=var)
medium.config(indicatoron=0, bd=4, width=12, value='Medium')
medium.grid(row=0, column=1)

hot = tki.Radiobutton(frm, text='Hot', variable=var)
hot.config(indicatoron=0, bd=4, width=12, value='Hot')
hot.grid(row=0, column=2)

root.mainloop()

or you could use pack, with the side option set to 'left'.
Here's an example where the buttons are created in a loop, and bound to keys in a dictionary:

import Tkinter as tki

def print_var(*args):
    print root.getvar(name=args[0])
    # or
    print var.get()

root = tki.Tk()

frm = tki.Frame(root, bd=16, relief='sunken')
frm.pack()

var = tki.StringVar()
var.trace('w', print_var)

b_dict = {'Mild':0, 'Medium':0, 'Hot':0}

for key in b_dict:
    b_dict[key] = tki.Radiobutton(frm, text=key, bd=4, width=12)
    b_dict[key].config(indicatoron=0, variable=var, value=key)
    b_dict[key].pack(side='left')

root.mainloop()

Information about the variable classes, and their methods, can be found → here.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
  • I don't know classes yet, but that definitely got me going in the right direction! Thanks. – user1332449 Apr 16 '12 at 20:46
  • If you are creating a simple row or column I recommend `pack` over `grid`; it requires a little less code. `pack` excels in creating single rows or single columns. – Bryan Oakley Apr 16 '12 at 20:50
  • 1
    @user1332449 Recommended reading: [4.4](http://infohost.nmt.edu/tcc/help/pubs/tkinter/layout-mgt.html#grid) & [class example 1](http://www.daniweb.com/software-development/python/code/216596/a-simple-class-inheritance-example-python) , [2](http://stackoverflow.com/a/10039671/1217270) – Honest Abe Apr 16 '12 at 20:58