Use Tkinter to create GUI development tools (42) Tkinter and TCL/Tk mixed programming

Use Tkinter to create GUI development tools (42) Tkinter and TCL/Tk mixed programming
Tcl/Tk GUI tool set, which is the Tcl/Tk graphical interface established by John Ousterhout with the support of Scriptics. It is a graphics extension library and module written in C language, which realizes the functions of the X window system similar to the Windows window. Steen Lumholt and Guido van Rossum wrote the Python Tkinter module. The Tkinter module utilizes the C extension library (_tkinter) of Tcl/Tk. Developing programs with Tkinter does not need to pay attention to the interface and implementation of Tcl/Tk, so Tkinter is regarded as an independent Python extension module. At present, Tkinter has become the standard GUI library of Python. In other words, the Python installation package downloaded from the official website already contains the Tkinter library. Many Python tools are built on the basis of the Tkinter library, for example:
Python IDLE, Turtle drawing library... etc. are all written in Tkinter.
Python is an interpreted scripting language, and TCL is also an interpreted scripting language. Because of the existence of the Tkinter library, Python can indirectly support the TCL language and can run TCL programs and TCL module files.
Let's first look at the method of directly running the TCL program.

import tkinter as tk

#查看tkinter版本号
print('Tkinter版本:', tk.TkVersion)

#建立一个tcl的解释器
tcl = tk.Tcl()

#察看tcl版本号
print( 'Tcl版本:',tk.TclVersion)

#tcl语言脚本
tclcode='''
proc add {n m} {
    return [expr $n+$m]
}
'''
#执行tcl语言脚本
tcl.eval(tclcode)
#调用函数,tcl_str为返回值
tcl_str=tcl.eval('add 30 40')
#显示结果
print( tcl_str)

The program execution results are as follows:

Tkinter版本: 8.6
Tcl版本: 8.6
70

If you want to execute the TCL module file, you can do as follows.
1. Establish a tcl program file

proc sayHello {} {
    return "Hello TCL world!"
}

proc add {num1 num2} {
	return [expr $num1 + $num2]
}

2. The running Python code is as follows:

import tkinter as tk

#建立一个tcl的解释器
tcl = tk.Tcl()

#导入tcl文件
tcl.eval('source tcltest.tcl')

#调用函数,tcl_str为返回值
tcl_str=tcl.eval('add 35  15')
print( tcl_str)

tcl_str=tcl.eval('sayHello')
print(tcl_str)

root=tk.Tk()
lb=tk.Label(root,text=tcl_str)
lb.pack()

The results are as follows:

50
Hello TCL world!

Through the above test, we can say that two languages ​​are already included in Python. Friends who want to learn the Tcl language can use Python to develop a TCL learning tool.

What everyone sees above is not the point, but below is the point.
Since Tkinter is implemented on the basis of TCL/tk, we can directly call the underlying TCL/tk scripts in the program to develop Tkinter programs, and we can also enhance the functions of Tkinter by compiling tcl control modules by ourselves.
The python tcl module is in the "tcl" subdirectory under the directory where the python.exe executable file is located.
The interface library "_tkinter.lib" of python's tcl is in the "libs" subdirectory of the directory where the python.exe executable file is located.
Tkinter calls the TCL/tk library through the "tk.call" or "_tk.call" in the "_tkinter.lib" library.
The demo code is given directly below.

import tkinter as tk
#建立一个tcl的解释器
tcl = tk.Tcl()

#察看tcl版本号
print( 'Tcl版本:',tk.TclVersion)
#导入tcl文件
tcl.eval('source tcltest.tcl')
#调用函数,tcl_str为返回值
tcl_str=tcl.eval('add 35  15')
print( tcl_str)

tcl_str=tcl.eval('sayHello')
print(tcl_str)

#建立Tkinter主窗口
root=tk.Tk()
lb=tk.Label(root,text=tcl_str)
lb.pack()

lb2=tk.Label(root,text="标签2")
root.tk.call('pack','configure', lb2._w)  #lb2.pack()
root.tk.call('update')  #root.update()


height=root.tk.call('winfo', 'height', root._w) #获取主窗口高度
print('root窗口高度:',height)

root.mainloop()

Program execution results:

Tcl版本: 8.6
50
Hello TCL world!
root窗口高度: 46

The display graphics are as follows:
Insert picture description here
Through the introduction of this article, do you feel that Tkinter has great potential for in-depth development?

You can communicate with me in the following ways:
#独狼荷蒲qq:2775205
#通通小白pythonquantization group:524949939
#电话微信:18578755056
#通通小白pythonquantification group:524949939
#tkinter,pyqt,gui,Python learning group: 647866213

Guess you like

Origin blog.csdn.net/hepu8/article/details/106600039