So, I am making a data entry window in Tkinter, and the window has the "Sex" option, but I'm having trouble determining which sex is selected. My code:
from Tkinter import *
import tkMessageBox
data_list = []
class Personal_Data:
def __init__ (self, sex, age, height, weight):
self.sex = sex
self.age = age
self.height = height
self.weight = weight
def save_data (sex, age, height, weight, screen):
screen.destroy ()
data = Personal_Data (sex, age, height, weight)
data_list.append (data)
tkMessageBox.showinfo ("", "Entry saved!")
def data_entry ():
data_entry_screen = Tk ()
Label (data_entry_screen, text = "Sex:").grid (row = 0, column = 0)
var = StringVar ()
def sel ():
print var.get ()
male_pick = Radiobutton (data_entry_screen, text = "Male", variable = var, value = "Male", command = sel)
male_pick.grid (row = 0, column = 1, sticky = "W")
female_pick = Radiobutton (data_entry_screen, text = "Female", variable = var, value = "Female", command = sel)
female_pick.grid (row = 0, column = 1, sticky = "E")
sex = var.get ()
Label (data_entry_screen, text = "Age:").grid (row = 1, column = 0)
age_entry = Entry (data_entry_screen)
age_entry.grid (row = 1, column = 1)
Label (data_entry_screen, text = "Height:").grid (row = 2, column = 0)
height_entry = Entry (data_entry_screen)
height_entry.grid (row = 2, column = 1)
Label (data_entry_screen, text = "Weight:").grid (row = 3, column = 0)
weight_entry = Entry (data_entry_screen)
weight_entry.grid (row = 3, column = 1)
Button (data_entry_screen, text = "SAVE", command = lambda: \
save_data (sex, age_entry.get(), height_entry.get(),\
weight_entry.get(), data_entry_screen)\
).grid (row = 4, column = 0)
Button (data_entry_screen, text = "CANCEL", command = \
data_entry_screen.destroy).grid (row = 4, column = 1)
root = Tk ()
root.title ("Main menu")
main_menu = Menu (root)
data_entry_menu = Menu (main_menu, tearoff = 0)
main_menu.add_cascade (label = "Data", menu = data_entry_menu)
data_entry_menu.add_command (label = "Data entry", command = data_entry)
root.config (menu = main_menu)
root.mainloop ()
Actually, the value it returns is None (I can see the returned value printed with the sel
function), but I don't know why... Any ideas?
P.S. Sorry for the long code, but it's killing me...