0

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...

mdenci
  • 297
  • 2
  • 6
  • 17

1 Answers1

0

I don't see where sex is the value None, but I do see where it's the empty string, ''. You are doing sex = var.get() immediately on creating and setting up the new window (which should be done with Toplevel(), by the way, not another root object). Since it wasn't set() at that point, it's the empty string, ''. Changing the chosen value of that button later on doesn't affect that saved value. Instead, remove sex = var.get() and retrieve the radio button's value only when the "SAVE" button is clicked, like you are already doing with all the other entered data:

...save_data(var.get(), age_entry.get(),...
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • that did it, changed the `data_entry_screen = Tk ()` into `data_entry_screen = Toplevel ()`, and removed the `sex = var.get ()`, retrieved it's value with the `save_data (var.get ()...)...` – mdenci Feb 20 '16 at 16:25