2

I'm making a character creator and I need to ask the user what their character's gender is. I have lists of pronouns to refer to depending on the gender, but for some reason, they are only effective with the last option. I've been using radiobuttons for the user to select their character's gender.

I have the last option to be gender-neutral, and every time I run the program, no matter what I choose, it always uses gender-neutral pronouns. I'm not sure how else I can fix this, I've tried creating each button individually outside a for loop, I tried placing the function in different places, nothing has worked.

Here's a snippet of my code:

class gender(tk.Frame):
    def __init__(self, master):
        global gender
        name = e1.get()
        tk.Frame.__init__(self, master)
        tk.Label(self, text=("What gender is your character?"), bg='#e6f7f5', fg='#33271b', font='Fixedsys 24 bold').pack()
        greet = ("Hey {}!".format(name))
        tk.Label(self, text=(greet)).pack()
        
        
        v = tk.IntVar()
        
        def ShowChoice():
            print(v.get())
            
        
        genderchoices = [('Male', 1), ('Female', 2), ('Other', 3)]
        def setgender(x):
            global gender
            gender = x
            return gender
        
        for g, val in genderchoices:
            tk.Radiobutton(self,
                           text=g,
                           padx=20,
                           variable=v,
                           value=val,
                           command=lambda:[ShowChoice,setgender(val)]).pack(anchor=tk.W)


        tk.Button(self, text="Next",
            command=lambda: [master.switch_frame(PageThree)]).pack()
    
class PageThree(tk.Frame):
    def __init__(self, master):
        global gender
        tk.Frame.__init__(self, master)
        next = "hey {}.".format(name)
        more=0
        if gender == 1:
            more = "i like {}!".format(mpronouns[1])
        if gender == 2:
            more = "i like {}!".format(fpronouns[1])
        if gender == 3:
            more = "i like {}!".format(npronouns[1])
        tk.Label(self, text=(next)).pack()
        tk.Label(self, text=(more)).pack()
JacksonPro
  • 3,135
  • 2
  • 6
  • 29
chris
  • 21
  • 1
  • I recommend not using `lambda` with a list of functions. It makes the code harder to understand and harder to debug. Create a proper function specifically for the radiobuttons. – Bryan Oakley Jan 15 '21 at 01:33

0 Answers0