单选按钮功能

时间:2015-01-23 22:05:55

标签: tkinter

所以我有一个代码,我想把它称为函数,例如 如果选择了python打印的东西

from tkinter import *

r = Tk()
v = IntVar()  


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

def ShowChoice():
    print ("your choice is",v.get())

Label(r, text="Choose your favourite programming language", justify=LEFT, padx=20).pack()

for txt, val in languages:
    Radiobutton(r, text=txt, padx=20, variable=v, command=ShowChoice, value=val).pack(anchor=W)


mainloop()

1 个答案:

答案 0 :(得分:0)

扩展当前功能:

def ShowChoice():
    print ("your choice is",v.get())

您必须说明"如果按下v,请执行某些操作"。像这样:

def ShowChoice():
    if v.get()==1:
        #Do Something
    else:
        print("your choice is", v.get())