Hi all,
I'm running into some difficulty in wrapping a Tk widget from Python in
a way that will allow me to execute Python callbacks/commands.
I'm trying to incorporate widgets from TclMacBag
(http://tclmacbag.autons.net/) into one of my Tkinter programs.
Specifically, I'm trying to use the "stylebutton" widget. At their
base, the TclMacBag stylebuttons are just ttk::buttons with a custom
style. However, they are constructed as megawidgets, their internal API
is quite complex, and so getting at their underlying button and style
implementation from Tkinter is difficult.
My current approach is to define a simple Tcl/Tk package that exposes
the functionality I need, and then use self.tk.call() to access this
from Tkinter. Here's an abbreviated example:
##Tcl code in "macstylebutton" package
proc macstylebutton {w child img cmd layout msg row column } {
::tclmacbag::stylebutton $w.$child -image $img -style $layout
-command $cmd
grid $w.$child -row $row -column $column
balloon $w.$child $msg
}
#Python function
def styleButton(self, parent, child, img, cmd, style, msg, row, column):
self.tk.call('package', 'require', 'macstylebutton')
self.tk.call('macstylebutton', parent, child, img, cmd, style,
msg, row, column)
#Python GUI code that calls the function
self.deletebutton=self.styleButton(self.topframe, 'delete',
self.logout, lambda: self.authorizeCommand(self.deletePackage), 'mail',
'Delete package', 0, 2)
This code lays the buttons out correctly, but the Python callback is not
executed. It seems to be correctly set up; running "print cmd" yields
output like this:
<bound method phynchronicityApp.infoPackage of
<__main__.phynchronicityApp instance at 0x3f1af8>>
but the actual callback doesn't fire.
My theory as to the problem is this: it appears that the widget is
responding to the "cmd" parameter as a Tcl command, and since it's
actually a Python command, nothing's happening. I know Python/Tkinter
can execute Tcl commands through 'call' or 'eval,' but it doesn't appear
that Python commands can be passed to raw Tk widgets. Indeed, I had
tried to simply configure the buttons via something like
'self.deleteButton.configure(command=foo'), but Tkinter returned an
error--something about a NoneType object lacking a 'configure' method.
My question is this: how can I get Python callbacks to fire when a raw
Tk widget is being called via 'self.tk.call'? Do I have to write a fancy
wrapper that turns the widget into a Python object that will respond to
Python callbacks, or is there a simpler way to do this?
Thanks,
Kevin
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss