What does the "tk.call" function do in Python/Tkinter?
Asked Answered
E

2

14

I have seen call function in Python scripts, called as tk.call(), but I don't understand the meaning of the same. There is no documentation related to it too.

Can any one please explain about the functionality of call() function.

This is a simple example:

p.tk.call(p, 'put', color, '-to', 0, 0, p['width'], p['height']) 

The functionality of this function, where p is the PhotoImage widget.

Ezzell answered 21/2, 2015 at 9:11 Comment(1)
As others have said, it's a direct call to the underlying tcl/tk interpreter. For the "put" command, you need to look at the docs for the tcl/tk photoimage here: tcl.tk/man/tcl/TkCmd/photo.htm#M30Negress
L
24

Tkinter isn't pure python. Underlying it is a live Tcl interpreter with an extension called "tk" loaded into the interpreter. Most Tkinter commands, methods and objects eventually wind up as invocations of tcl commands. For example, when you do something like:

root = tk.Tk()
f = tk.Frame(root)
b = tk.Button(f, text="Press me!")

... it gets translated into something (roughly) like this:

package require tk
frame .f
button .f.b -text "Press me!"

(note: Tkinter actually generates more complex names than .f and .f.b, but the concept is the same)

The call method is the interface to this underlying tcl interpreter. It allows you to construct a tcl command and ask the interpreter to run it. It is a bridge between python and tcl.

It is not typically used in application-level code, though it can be useful in the rare cases where the Tkinter wrapper around tcl/tk doesn't provide access to some feature supported by tcl/tk.

Lauricelaurie answered 18/3, 2015 at 17:27 Comment(2)
Very nice answer! I would like to ask another thing about the use of call method. We can give tcl commands (instead of Python ones) using this method, but we still use the Python way of giving commands (calling a method and passing parameters) through the method itself. Is there a way to know how to use the call method for Python programmers? Is there some documentation?Carousal
@Rinzler: the only documentation that would be useful is the tcl/tk documentation. When you call call, it literally just directly translates the arguments to a tcl command (tcl statements are simply lists made up of a command and zero or more arguments). tcl.tk/manLauricelaurie
R
5

Tk.call() is from the Tkinter module, and it can be used to execute tcl-based commands directly from a Tkinter widget. Example Tkinter command to get the user's OS:

root.tk.call('tk', 'windowingsystem')

Where windowingsystem is a feature from tcl.

As far as I know there is no official documentation for tk.call().

Rager answered 21/2, 2015 at 11:53 Comment(3)
Thanks a lot for the answer. but could you explain this too. p.tk.call(p, 'put', color, '-to', 0, 0, p['width'], p['height']) The functionality of this function, where 'p' is the photoImage element.Ezzell
Sorry, not that big a user of Tkinter, can't help you there.Rager
I'd just found this messy reference that should list most of the commands tcl.tk/man/tcl/TkCmd which you would use like so: tk.call('wm', 'title', window, string)Finale

© 2022 - 2024 — McMap. All rights reserved.