定义函数下的tkinter问题

时间:2019-01-22 15:44:39

标签: python tkinter

您能帮我解决我所面临的以下问题吗?

当我使用tkinter创建单选按钮时,从列表中选择没有问题。

但是,如果我将相同的脚本放在菜单下,则在此特定情况下,所选选项始终是默认选项(“ Python”,1)。你有什么想法要克服吗?预先感谢!

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()

0 个答案:

没有答案