Tcl/tk实例-局域发送消息工具

#!/usr/local/bin/wish8.5
# # author: wn0112@gmail.com

package require Ttk
set top .
set sheight [ winfo screenheight $top ]
set swidth [ winfo screenwidth $top ]
wm geometry $top +[ format %.0f [ expr {($swidth-410)/2} ]]+[ format %.0f [ expr {($sheight-220)*0.3} ]]

wm title . "LAN\ Chat"
wm minsize . 409 220
wm maxsize . 400 220
wm aspect . 200 120 200 120
wm resizable . 0 0

proc main { } {
	global lab entry
	set check_os [ array get tcl_platform ]

	ttk::frame .t -borderwidth 1 -relief groove
	pack .t -side right -anchor n

	ttk::frame .num -borderwidth 2 -relief groove
	pack .num -side top -anchor w

	ttk::frame .f -borderwidth 2 -relief groove
	pack .f -side top -anchor w

	ttk::label .num.ls -text "Talking to:" -anchor nw
	ttk::label .num.ldate -text "Message:" -anchor nw
	if { [ string match *Linux* $check_os ] } {
		ttk::combobox .num.s -width 25 
	} elseif { [ string match *osVersion\ 6.1* $check_os ] } {
		ttk::combobox .num.s -width 27 
	} else {
		ttk::combobox .num.s -width 27 
	}

	ttk::scrollbar .t.scroll -command { .t.log yview }
	set lab [ text .t.log -state disabled -width 26 -height 20 -borderwidth 2 -wrap word -relief sunken -yscrollcommand { .t.scroll set }]
	pack .t.scroll -side right -fill y
	pack .t.log -side right -anchor n

	if { [ string match *Linux* $check_os ] } {
		$lab config -width 31
	}

	set entry [ text .f.date -width 19 -height 5 -borderwidth 1 -relief sunken -yscrollcommand { .f.scroll set } ]

	ttk::scrollbar .f.scroll -command { .f.date yview }
	pack .f.scroll -side right -fill y
	if { [ string match *Linux* $check_os ] } {
		$entry config -width 20
	}
	ttk::button .trans -text "Send" -width 10 -command [ list click_send ]
	pack .num.ls .num.s .num.ldate .f.date -anchor nw -pady 3 -ipadx 3
	pack .trans -pady 5 -side top -expand true
	bind . <Escape> exit
	focus -force .num.s

	if { [ string match *Linux* $check_os ] } {
		insertLog "*******************************"
	} elseif { [ string match *osVersion\ 6.1* $check_os ] } {
		insertLog "**************************"
	} else {
		insertLog "**************************"
	}
	insertLog "Run this tool on other computers,  you can send message to them."
	if { [ string match *Linux* $check_os ] } {
		insertLog "*******************************"
	} elseif { [ string match *osVersion\ 6.1* $check_os ] } {
		insertLog "**************************"
	} else {
		insertLog "**************************"
	}
}

set remote ""
# client code
proc client { addr { port 2345 } } {
	global lab
	set err [ catch { set connection [ socket $addr $port ]} result ]
	fconfigure $connection -encoding utf-8
	if { $err != 0 } {
		popErr "Can't connect to this IP address."
		return -1
	} else {
		return $connection
	}
}

proc send { channel data } {
	global lab entry
	puts $channel $data
	flush $channel
	insertLog "I said:\n$data"
	$entry delete 0.0 end
	close $channel
}

proc click_send {} {
	global lab
	if { [ .num.s get ] !="" } {
		set temp [ split [ .num.s get ] . ]
		for { set i 0 } { $i < [ llength $temp ] } { incr i } {
		  if { [ lindex $temp $i ] > 255 || [ lindex $temp $i ] < 0 } {
				insertLog "Invalid IP address"
				return
			} elseif { [ llength $temp ] < 4 || [ llength $temp ] > 4 } {
				insertLog "Invalid IP address"
				return
		  }
		}
	} else {
		insertLog "Please input IP address."
		return
	}
	set connection [ client [.num.s get ]]
	if { $connection != -1 } {
		set value [ .num.s cget -value ]
		if { ![ string match *[ .num.s get ]* $value ] } {
			lappend value [ .num.s get ]
			.num.s config -values $value
		}
		send $connection [ string trimright [.f.date get 0.0 end ] \n ]
	}
}

# server code
proc read_data_s { channel } {
	global lab a remote
	set err [ catch { set data [ read $channel nonewline ] } result ]
	if { $err!=0 } {
		close $channel
		insertLog "$result"
	} elseif {[ eof $channel ]} {
		close $channel
	} else {
		insertLog "$remote said:\n$data"
	}
}

proc accept { channel addr port } {
	global remote
	set remote $addr
	fconfigure $channel -translation auto -blocking 0 -encoding utf-8
	fileevent $channel readable [ list read_data_s $channel ]
}

proc server { port } {
	socket -server accept $port
}

proc insertLog { msg } {
	global lab
	$lab config -state normal
	$lab tag configure blue -foreground blue
	if { [ string match *I\ said:* $msg ] } {
		$lab insert end $msg\n blue
	} else {
		$lab insert end $msg\n
	}
	$lab yview moveto 1.0
	$lab config -state disabled
 }

proc popErr { str } {
	tk_messageBox -type ok -title "Error" -icon error -message "$str"	
}
 
# monitor port 2345
main
server 2345



这本教程花费了我足足两个月的时间写的,每个章节以例子为核心讲解知识点,最大的好处是要用的时候把代码粘贴一下,修改修改就可以上手用了,绝对原创,光是调试这些例子就够我折腾了。知识点覆盖比较全面,要个20分不过分吧,有几本书认真讲过那些常用模块: socket, 数据库操作,xml解析,多线程,最具体的做法多半是一带而过,我可是一个例子一个例子做出来的。 把目录发给大家看看: 2.1 第1课:简单文本输出 5 2.2 第2课:给变量赋值 5 2.3 第3课:命令的赋值与置换一 6 2.4 第4课:命令的赋值与置换二 7 2.5 第5课:命令的赋值与置换三 7 2.6 第6课:算数运算 8 2.7 第7课:文本比较-SWITCH应用 9 2.8 第8课:数值比较-IF应用 10 2.9 第9课:WHILE 循环 11 2.10 第10课:FOR循环和INCR 11 2.11 第11课:过程PROC 12 2.12 第12课:过程PROC的参数定义 13 2.13 第13课:变量的作用域 13 2.14 第14课:LIST结构 14 2.15 第15课:LIST项的增删改 15 2.16 第16课:更多LIST相关 16 2.17 第17课:字符串函数 17 2.18 第18课:更多字符串函数 17 2.19 第19课:修改字符串函数 20 2.20 第20课:正则表达式 21 2.21 第21课:更多正则表达式 22 2.22 第22课:数组 24 2.23 第23课:更多数组相关 25 2.24 第24课:文件存取 28 2.25 第25课:文件信息 30 2.26 第26课:TCL中的子进程调用-OPEN & EXEC 33 2.27 第27课:命令或者变量是否存在-INFO 34 2.28 第28课:解释器状态-INFO 35 2.29 第29课:过程信息-INFO 36 2.30 第30课:模块化-SOURCE 37 2.31 第31课:建库-UNKNOWN & INFO LIBRARY 38 2.32 第32课:创建命令-EVAL 40 2.33 第33课:在EVAL中应用FORMAT & LIST 40 2.34 第34课:不使用EVAL替换-FORMAT & SUBST 42 2.35 第35课:改变工作目录- CD & PWD 43 2.36 第36课:调试和错误-ERRORINFO & ERRORCODE & CATCH 44 2.37 第37课:调试-TRACE 45 2.38 第38课:命令行参数和环境串 46 2.39 第39课:TIME & UNSET 47 2.40 第40课:SOCKET & FILEEVENT & VWAIT 49 2.41 第41课:日期时间-CLOCK 51 2.42 第42课:I/O通道-FBLOCKED & FCONFIG 53 2.43 第43课:子解释器 56 2.44 第44课:数据库操作 57 2.45 第45课:函数或过程数组的输入和输出方法 59 2.46 第46课:INFO的用法 60 2.47 第47课:多线程 61 2.48 第48课:解析XML 72
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值