In Tcl, a widget is created by invoking the widget command with the name of the new widget and any configuration parameters. In Python, a widget is created by calling the class with the name of the new widget's parent and any configuration parameters. "-name value" pairs in Tcl become "name=value" keyword arguments in Python

Tcl

Python

button .t.b -text "Click Me"

b = Button(t, text="click me")

Widgets have methods which may accept positional or keyword arguments. Many methods are shared by all widget types, while almost every widget has a few special methods.

Tcl

Python

.t.b invoke

b.invoke()

.t.e insert end {Sample Text} 

e.inert("end", "Sample Text") 

Some widget methods have compound names, which are typically spelled with an underscore in Python and a space in Tcl

Tcl

Python

.t.t tag add newtag 0.0 end

tt.tag_add("newtag", 0.0, "end")

Some methods in Python are separate commands in Tcl:

Tcl

Python

grid .t.b

b.grid()

When a keyword or name in Tcl is a Python reserved word, append a "_" when using it as a keyword argument:

Tcl

Python

grid .t.b -in .t.f

b.grid(in_=f)

Some special values used in Tcl are defined as constants in Tkinter:

Tcl

Python

pack .b -side left

b.pack(side=Tkinter.LEFT)

b.pack(side="left")

   1 # -*- coding: cp1252 -*-
   2 #
   3 # simple tcl => python example
   4 #
   5 # fredrik lundh, june 1998
   6 #
   7 # fredrik@pythonware.com
   8 # http://www.pythonware.com
   9 #
  10 
  11 from Tkinter import *
  12 
  13 
  14 # 1   set i 0
  15 # 2   foreach name { "One"
  16 # 3                  "Two"
  17 # 4                  "Three"} {
  18 # 5     incr i
  19 # 6     set ff [frame .rc.fff.sub$i]
  20 # 7     label $ff.lab -text $name -width 40 -anchor e
  21 # 8     entry $ff.ent -textvariable address($name)
  22 # 9     pack $ff.lab $ff.ent -side left -in $ff
  23 # 10    pack $ff
  24 #
  25 # some python notes:
  26 #
  27 # - since the label and the entry widgets are children of the
  28 #   frame, there's no need to use the "in" packer option
  29 #
  30 # - grid is better than pack for examples like this; see
  31 #   http://www.pythonware.com/library/tkinter/introduction/grid.htm
  32 #   for more information
  33 #
  34 
  35 root = Tk()
  36 
  37 address = {}
  38 
  39 for name in ("One", "Two", "Three"):
  40 
  41     ff = Frame(root)
  42 
  43     address[name] = StringVar()
  44 
  45     label = Label(ff, text=name, width=40, anchor=E)
  46     entry = Entry(ff, textvariable=address[name])
  47 
  48     label.pack(side=LEFT)
  49     entry.pack(side=LEFT)
  50 
  51     ff.pack()
  52 
  53 mainloop()
  54 

tkinter: TranslateTcl (last edited 2011-08-10 02:27:10 by AnthonyMuss)