简体   繁体   中英

How to convert the python Tkinter listbox into Tcl/Tk?

How to convert the following listbox python code in to Tcl/Tk?

from tkinter import *

root = Tk()
root.title("Dude0")
root.geometry("400x400")

my_listbox = Listbox(root)
my_listbox.pack(pady=15)

root.mainloop()

Seems like all the examples for tk are for python these days instead of TCL.

Here is the translation:

package require Tk

wm title . "Dude0"
wm geometry . "400x400"

listbox .my_listbox
pack .my_listbox -pady 15

With wish you automatically get a root window named ".", and wish will automatically listen for events so you don't need to call the equivalent of mainloop . For other widgets such as your listbox, you need to give them a path that uses "." as separators. So .my_listbox represents a child of the root window. You can assign that to a variable, but in my experience it's good enough to just give the widgets themselves user-friendly names.

Most other things that are methods in tkinter will be procs in tcl, and kwargs in tkinter are option/value pairs in tcl (options begin with a dash). So instead of my_listbox.pack(padx=15) , in tcl you would do pack.my_listbox -padx 15 .

tkdocs.com has an excellent tutorial that lets you see examples written in python, tcl, ruby, and perl.

I don't know if my transcription of your python code will help you, but I'm agree with @GlennJackman, tkdocs will be more interesting than my response... that said, the code written in Tcl:

package require Tk ; # from tkinter import *

set root .root 
toplevel $root ; # root = Tk()

wm title $root "Dude0" ; # root.title("Dude0")
wm geometry $root "400x400" ; # root.geometry("400x400")

listbox $root.lb ; # my_listbox = Listbox(root)

pack $root.lb -pady 15 ; # my_listbox.pack(pady=15)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM