[问题]class内呼叫function有没有括号的差别?

楼主: j3234567 (鸟科学先生)   2015-02-27 14:00:41
小弟最近初学python有一个问题有点困惑
我用Tkinter写了一个小视窗
from Tkinter import *
class radio_practice:
def __init__(self, master):
self.v = IntVar()
self.frame = Frame(master)
self.label = Label(master, text = 'choose a language',
justify = LEFT, font = ('times',26))
self.label.pack(fill = BOTH, side = TOP)
self.botton1 = Radiobutton(master, text = 'python',
variable = self.v, value = 1,
command = self.print_variable)
self.botton1.pack(fill = BOTH, side = LEFT, expand = True)
self.botton2 = Radiobutton(master, text = 'C#', v
ariable = self.v, value = 2,
command = self.print_variable)
self.botton2.pack(fill = BOTH, side = LEFT, expand = True)
def print_variable(self):
print self.v.get()
root = Tk()
program_choose = radio_practice(root)
root.mainloop()
后面那个print_variable function是为了观察一下self.v会不会被我按下button后改变
的状况,但是我的command如果 = self.print_variable()程式一跑self.v就会被印出来
而且任凭我怎么点按钮也不会显示出被改过的self.v,但是如果没有那个括号就可以如
预期看到self.v被改变
想请问在class里面呼叫function有没有括号的意义有何不同?
档案请由此下载:
https://www.dropbox.com/s/mgml6286tfmi5g8/radiobutton.py?dl=0
作者: mail6543210 (mail6543210)   2015-02-27 19:04:00
command=func是接收一个参数,该参数是一个function事件发生时才call该func,cmd=func()变成呼叫func并将其回传值None当作command;因print_var回传None,所以相当于command=None,即事件发生时不处理.简言之,这是因func可视为物件,并传递.

Links booklink

Contact Us: admin [ a t ] ucptt.com