0

Can you please help me with follwing issue I face:

When I am creating radiobutton with tkinter, I don't have a problem to select from the list.

However, if I put the same script under a menu, then selected option is always the default one, ("Python",1) in this specific case. Do you have any idea how to overcome this? Thanks in advance!

import tkinter as tk


root_m = tk.Tk()
root_m.geometry("400x200")
frame_m = tk.Frame(root_m)
root_m.title("NUMERIC UNIVERSE")
frame_m.pack()


def chars_merging():

    languages = [
                ("Python",1),
                ("Perl",2),
                ("Java",3),
                ("C++",4),
                ("C",5)
                ]

    root = tk.Tk()
    root.geometry("400x200")
    frame = tk.Frame(root)
    root.title("SELECT SOMETHING")
    frame.pack()


    v = tk.IntVar()
    v.set(0)  # initializing the choice, i.e. Python


    label = tk.Label(frame, 
             text="""Choose your favourite 
             programming language:""",
             justify = tk.LEFT,
             padx = 20)

    label.pack()

    def ShowChoice():     
        global data_sel
        data_sel = v.get()
        print(data_sel)




    for val, language in enumerate(languages):
        tk.Radiobutton(frame, 
                       text=language,
                       indicatoron = 0,
                       width = 20,
                       padx = 20, 
                       variable=data_sel, 
                       command=ShowChoice,
                       value=val).pack(anchor=tk.W)




    root.mainloop()

    #return(languages[v.get()])  

    print("You selected", languages[v.get()])  



button3 = tk.Button(frame_m,
                  text="3.Prepare and merge chars",
                  command=chars_merging,
                  width=25)
button3.pack()


#  CLOSING THE WINDOW ---------------------------------------------------------
def finish():
    root_m.destroy()

button_n = tk.Button(frame_m,
                  text="Finish",
                  command=finish,
                  width=25)
button_n.pack()
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
root_m.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • This is code for RadioButton which works. Can you show the code for menu implementation that does not work? – amanb Jan 22 '19 at 16:13
  • The script is working w/o errors but the value it returns from the radiobutton selection is always the default one, not selected one – Antonia Popova Jan 22 '19 at 18:16
  • Possible duplicate of [tkinter 'var' for radiobutton is always returning 0](https://stackoverflow.com/questions/49855681/tkinter-var-for-radiobutton-is-always-returning-0) – JackU Jan 22 '19 at 18:27
  • Possible duplicate of [this](https://stackoverflow.com/questions/49855681/tkinter-var-for-radiobutton-is-always-returning-0). You should only have 1 instance of `.mainloop()` and one instance of `root`. When creating a pop up window you should `Toplevel`. – JackU Jan 22 '19 at 18:30

0 Answers0