-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.tcl
178 lines (161 loc) · 4.82 KB
/
flash.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
lappend auto_path [file join [file dirname [info script]] ..]
package require oowidgets
# Examples
namespace eval ::flash {}
# create the wrapper function - creates proc flash::button
oowidgets::new ::flash::Button
# implementation of a sample widget
oo::class create ::flash::Button {
superclass oowidgets::BaseWidget
constructor {path args} {
my install ttk::button $path -flashtime 500
my configure {*}$args
}
method flash {} {
set ot [my cget -text]
set ft [my cget -flashtime]
for {set i 0} {$i < 5} {incr i} {
my configure -text "......"
update idletasks
after $ft
my configure -text $ot
update idletasks
after $ft
}
puts flashed
my configure -text $ot
}
}
#oowidgets::new ::flash::Label
oowidgets::widget ::flash::Label {
constructor {path args} {
my install ttk::label $path -flashtime 500
my configure {*}$args
}
method flash {} {
set fg [my cget -foreground]
for {set i 0} {$i < 10} {incr i} {
my configure -foreground blue
update idletasks
after [my cget -flashtime]
my configure -foreground $fg
update idletasks
after [my cget -flashtime]
}
puts labelflashed
}
}
# now a composite widget pack both widgets into a frame
oowidgets::widget ::flash::LabEntry {
variable ent
variable lab
constructor {path args} {
# the main widget is the frame
# add an additional label
my install ttk::frame $path -labeltext "Testlabel"
set lab [ttk::label $path.lab]
set ent [ttk::entry $path.ent]
pack $lab -side left -padx 5 -pady 5
pack $ent -side left -padx 5 -pady 5
my configure {*}$args
}
# expose if desired the internal widgets using subcommands
method label {args} {
$lab {*}$args
}
method entry {args} {
$ent {*}$args
}
method configure {args} {
if {[llength $args] < 2 || [expr {[llength $args]%2}] == 1} {
return [next {*}$args]
}
next {*}$args
array set opts $args
if {[info exists opts(-labeltext)]} {
my label configure -text $opts(-labeltext)
}
}
# delegate all methods to the entry widget
method unknown {args} {
$ent {*}$args
}
}
# simpler approach using only delegation
oowidgets::widget ::flash::LEntry {
variable ent
variable lab
constructor {path args} {
# the main widget is the frame
# add an additional label
my install ttk::frame $path
set lab [ttk::label $path.lab]
set ent [ttk::entry $path.ent]
pack $lab -side left -padx 5 -pady 5
pack $ent -side left -padx 5 -pady 5
my configure {*}$args
}
# expose the internal widgets using subcommands
method label {args} {
$lab {*}$args
}
method entry {args} {
$ent {*}$args
}
}
# mixin test
# keep standard widgets as classes
namespace eval ::oow { }
oowidgets::widget ::oow::Label {
constructor {path args} {
my install ttk::label $path
my configure {*}$args
}
}
oo::class create ::oow::LblFlash {
method flash {{flashtime 300}} {
set fg [my cget -foreground]
for {set i 0} {$i < 5} {incr i} {
my configure -foreground green
update idletasks
after $flashtime
my configure -foreground $fg
update idletasks
after $flashtime
}
puts "Mixin for label flashed"
}
}
oo::define oow::Label { mixin ::oow::LblFlash }
set lbl [oow::label .lbl -text "Hello" -foreground blue]
pack $lbl -side top
set btn [ttk::button .btn -text "Exit" -command exit]
pack $btn -side top
$lbl flash
set fb [flash::button .fb -text "Exit" -flashtime 100 -command exit]
pack $fb -side top -pady 10 -pady 10 -fill both -expand true
if {true} {
set fl [flash::label .fl -text "FlashLabel" -flashtime 50 -anchor center]
pack $fl -side top -padx 10 -pady 10 -fill both -expand true
set le [flash::labentry .le -relief ridge -borderwidth 5 -labeltext "Label 2:"]
#$le label configure -text " Label 1:"
$le entry insert end "Entryvalue"
$le delete 0 5
pack $le -side bottom -fill x -expand false
puts "label value: [$le cget -labeltext]"
$le configure -labeltext "Hello"
puts [$le configure]
set le2 [flash::lentry .le2 -relief ridge -borderwidth 5]
$le2 label configure -text "LEntry Example" -width 20
$le2 entry configure -show *
$le2 entry insert 0 "password"
puts "le2: [$le2 entry get]"
pack $le2 -side bottom -fill x -expand false
$fb flash
puts "done 1"
.fb flash
puts "done 2"
.fl flash
$fl flash
$fb invoke
}