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

相关推荐
杰锅就是爱情1 天前
OpenObserve Ubuntu部署
linux·运维·ubuntu
心随_风动1 天前
Ubuntu 文件复制大师:精通cp命令完整指南
数据库·ubuntu·postgresql
空灵之海1 天前
Ubuntu Server 22.04.5系统安装教程
linux·运维·ubuntu
kk5791 天前
【Ubuntu】sudo apt update出现E :仓库***没有Release文件
linux·运维·ubuntu
~光~~1 天前
【问题解决】VMware +Ubuntu20.04创建用户后无法登陆的问题
ubuntu
PAQQ1 天前
解决 ubuntu 重启串口号变化
linux·运维·ubuntu
desssq2 天前
ubuntu 18.04 泰山派编译报错
linux·运维·ubuntu
喵喵爱自由2 天前
Ubuntu 24.04 Server 版系统安装及配置
数据库·ubuntu
清风笑烟语2 天前
Ubuntu 24.04 搭建k8s 1.33.4
linux·ubuntu·kubernetes
CheungChunChiu2 天前
嵌入式 Linux 启动机制全解析:从 Boot 到 Rootfs
linux·运维·服务器·ubuntu·uboot·boot·extboot