2 jhts95559 jhts95559 于 2014.12.03 09:51 提问

为什么把 tkinter控件写进类里,就不能触发事件了?

from Tkinter import *

class Gui:

def __init__(self):
    self.root = Tk()
    self.var = IntVar()
    self.label = Label(self.root)
    self.label.pack()
    R1 = Radiobutton(self.root, text="Option 1", variable=self.var, value=1,
              command=self.sel())
    R1.pack( anchor = W )

    R2 = Radiobutton(self.root, text="Option 2", variable=self.var, value=2,
              command=self.sel())
    R2.pack( anchor = W )

    R3 = Radiobutton(self.root, text="Option 3", variable=self.var, value=3,
              command=self.sel())
    R3.pack( anchor = W)

    self.root.mainloop()

def sel(self):
   print 'haha...'
   selection = "You selected the option " + str(self.var.get())
   self.label.config(text =selection)

myapp=Gui()

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!