Ubuntu下的tcl/tk编程快速入门

一、Tcl/Tk 简介

接口介绍
https://www.tutorialspoint.com/tcl-tk/tcl_tk_quick_guide.htm

GUI Canvas接口
https://www.tutorialspoint.com/tcl-tk/tk_canvas_widgets.htm

tcl语言 https://www.tcl-lang.org/

官方教程 https://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html

二、Tcl/Tk安装及其快速使用入门

bash 复制代码
sudo apt-get update
sudo apt-get install tcl tk

一个简单单tcl文件demo.tcl

bash 复制代码
#!/usr/bin/tclsh
set x 10
set y 20

# 输出
puts $x $y

执行脚本

bash 复制代码
$ tclsh ./demo.tcl
10 20

三、Tcl快速入门教程

1、基本使用语法

  • 定义变量set及使用变量 puts
bash 复制代码
# 定义
set x 10
# 使用
puts $x
  • [ ]中括号
    [ ]中括号内的为独立的TCl语句
bash 复制代码
set x 10
set y 20
set m [expr $x + $y]
puts $m

# 或直接使用
puts "[expr $x + $y]"
  • 转义字符\
bash 复制代码
# 输出Tab
puts "\t $x"
# 输出换行
puts "\n $x"
  • { } 话括号
  • 在华括号中,所有特殊字符都将作为普通字符,不会被解释
bash 复制代码
set x 10
set y 20
puts {\t[expr $x + $y]}

# 直接输出
# \t[expr $x + $y]
  • 动态变量+字符组合
bash 复制代码
set i 10
puts ${i}_1
# 输出
# 10_1
  • 条件语句if else
bash 复制代码
if {$a > $b} {
    puts $a
} else {
    puts $b
}
  • 循环指令foreach while
bash 复制代码
set list {a b c}

foreach i $list {
    puts $i
}

set n 10
while {$n > 0} {
   puts $n
   incr n -1;#set n [expr $n -1]
}
  • 分割字符串
bash 复制代码
set str "abc;abb;ddc"
set list [split $str ";"]
foreach e $list {
    if {$e == "abb"} {
     break
     # continue
  }
  puts $e
}

2、函数调用proc

bash 复制代码
proc add {a b} {
set sum [expr $a + $b]
return $sum
}

# 调用
puts [add 10 20]
# 输出30

3、TCL文件调用

但A文件,需要调用B文件的函数时,如何操作?

首先定义个math.tcl

bash 复制代码
#!/usr/bin/tclsh
proc add {a b} {
	set sum [expr $a + $b]
	return $sum
}

再编写文件调用test.tcl

bash 复制代码
#!/usr/bin/tclsh
source math.tcl

puts [add 10 20]
# 输出30

4、执行字符串evel

有时,我们需要把字符串当做Tcl脚本来执行,这是就可以使用强大的evel命令

5、Tcl中的数学运算expr

tcl中执行数学运算,Tcl中的操作符与C语言保持一致,但是注意需要使用exp,否则只会被当成字符串处理;

exp支持+-*/ 等等基本运算,按位操作运算<<|等,还支持ceil, floor, roundsin、cos等数学函数;

bash 复制代码
# 定义变量
set x1 10
set y2 20

# 运算
set y [ expr { $x1 + $x2}]
# 输出
puts $y


# 使用数学三角函数
set x 3.14
set a [ expr sin($x / 2)]
puts $a

四、Tk图形编程GUI

一个Tk的demo.tk

bash 复制代码
#!/usr/bin/wish

canvas .myCanvas -background white -width 800 -height 600 
pack .myCanvas
.myCanvas create arc 500 200 600 300 -fill blue
.myCanvas create arc 0 0 20 20 -fill white
.myCanvas create line 10 10 50 50 30 100 -arrow both
.myCanvas create polygon 50 150 100 80 120 120 100 190 -fill white -outline black
.myCanvas create rectangle 150 150 170 170
.myCanvas create text 170 20 -text "Hello" -font {Helvetica -18 bold}
.myCanvas create bitmap 180 50 -bitmap info


# 按钮
proc but_event1 {args} {
    puts "hello world"
}

proc but_event2 {args} {
    puts "hello tcl/tk"
}

button .myButton1  -text "button1"   -command but_event1
button .myButton2  -text "button2"   -command but_event2
# 添加求解按钮
pack .myButton2 .myButton1 -side right

注意,Tk的启动命令为wish而不是tclsh

bash 复制代码
$ wish ./demo.tk

关于Tk中的GUI编程及Canvas组件的图像编程的更多使用命令参考如下地址:
https://www.tutorialspoint.com/tcl-tk/tk_canvas_widgets.htm

相关推荐
好奇的菜鸟10 小时前
如何在 Ubuntu 24.04 (Noble) 上使用阿里源
linux·运维·ubuntu
好奇的菜鸟12 小时前
如何在Ubuntu上检查MySQL是否启动并放开3306端口
mysql·ubuntu·adb
ZPC821015 小时前
ubuntu 6.8.0 安装xenomai3.3
linux·运维·ubuntu
电脑能手16 小时前
遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
linux·ubuntu·ssh
snoopyfly~16 小时前
Ubuntu 24.04 安装配置 Redis 7.0 开机自启
linux·redis·ubuntu
精英的英16 小时前
在Ubuntu 24.04主机上创建Ubuntu 14.04编译环境的完整指南
linux·运维·ubuntu
奇妙之二进制18 小时前
计算机科学导论(10)什么是BIOS
ubuntu·计算机基础
岁月玲珑18 小时前
【如何判断Linux系统是Ubuntu还是CentOS】
linux·ubuntu·centos
Kevin不想说话9261919 小时前
Ubuntu 24.04 安装搜狗输入法完整教程
ubuntu
矩阵老炮1 天前
Ubuntu20.4编译AOSP源码实践
ubuntu·aosp