python - Why only some Tkinter callback functions need to have an argument but others don't -


i'm using python 2.7, if matters.

here code wrote fun:

def p():     root = tk()      def cmd(event):         print int(slider.get())      slider = scale(root, orient = "horizontal", from_ = 0, = 100, command = cmd, state = "disabled")      def enable():         slider.config(state = "active")      b = button(root, text = "enable slider", command = enable)      b.grid()     slider.grid(row = 1)      root.mainloop() 

for code, i'm wondering why command scale requires event, button not. seems widgets in tkinter commands need have "event" argument, , other don't. why? how distinguish them?

thanks.

scale doesn't take event. takes current value. try this:

def cmd(value):     print int(value) 

if read tk tutorial, explains this:

there "command" configuration option, lets specify script call whenever scale changed. tk automatically append current value of scale parameter each time invokes script (we saw similar thing parameters being added scrollbar callbacks , on widgets scroll).

or, if read actual manpage:

specifies prefix of tcl command invoke whenever scale's value changed via widget command. actual command consists of option followed space , real number indicating new value of scale.

in other words, way distinguish them read docs. unfortunately, tkinter docs aren't complete—they assume know how tcl/tk works, or how yourself. that's why docs start off list of links sources of tk documentation.

if prefer figure out trial , error, it's not hard see gets passed:

def cmd(*args):     print('scale command says {}'.format(args))  def enable(*args):     print('button command says {}'.format(args)) 

but won't tell need know; there other callbacks arguments aren't obvious enough figure out without lot more work, or configurable (e.g., validate callback).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -