Radio buttons on tkinter

Post here if you need help with creating a Graphical User Interface in Python.

Radio buttons on tkinter

Postby eneskristo » Thu Jan 02, 2014 1:36 pm

Hello People!
I'm a new python developer that needs a bit of help with radio buttons on tkinter for python 3.3
I am doing an application that requires me to select several radio buttons like this:
o Me o Him
o She o Her
o Tim o Sir Robin
________
| |
|Submit|
|______|
I want to select only one option for each line, then submit them, but I have been having problems on doing unique identifiers. I was suggested IntVar() but I don't quite understand it. Please help me! I can provide additional info if needed. Thank you in advance!
Last edited by Mekire on Thu Jan 02, 2014 1:44 pm, edited 1 time in total.
Reason: First post lock.
eneskristo
 
Posts: 1
Joined: Thu Jan 02, 2014 1:29 pm

Re: Radio buttons on tkinter

Postby wuf » Tue Jan 07, 2014 12:36 am

Hi eneskristo

Here one solution:
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import tkinter as tk

MAIN_WIN_XPOS   = 10
MAIN_WIN_YPOS   = 10

def submit():
    for var in option_vars:
        print(var.get())
       
app_win = tk.Tk()

app_win.geometry("+%d+%d" % (MAIN_WIN_XPOS, MAIN_WIN_YPOS))
 
main_frame = tk.Frame(app_win)
main_frame.pack(fill='both')

left_frame = tk.Frame(main_frame)
left_frame.pack(side='left', fill='both')

right_frame = tk.Frame(main_frame)
right_frame.pack(side='left', fill='both')

options = [("Me", "Him"), ("She", "Her"), ("Tim", "Sir Robin")]
option_vars = list()

for option_pair in options:
    var = tk.StringVar()
    for index, option in enumerate(option_pair):
        if index == 0:
            tk.Radiobutton(left_frame, text=option, value=option,
                variable=var).pack(anchor='w')
            var.set(option)
        if index == 1:
            tk.Radiobutton(right_frame, text=option, value=option,
                variable=var).pack(anchor='w')

    option_vars.append(var)
   
submit_button = tk.Button(app_win, text="Submit", command=submit)
submit_button.pack() 

app_win.mainloop()
wuf ;)
wuf
 
Posts: 48
Joined: Fri Feb 08, 2013 6:42 am


Return to GUI

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests