开发者

Tcl/Tk: entry button - getting values into entry without passing entry

开发者 https://www.devze.com 2023-03-25 05:00 出处:网络
I am implementing an entry and开发者_如何学Go button with the following functionality. On clicking the button I will get a list of check buttons. After selecting check buttons they should get reflecte

I am implementing an entry and开发者_如何学Go button with the following functionality. On clicking the button I will get a list of check buttons. After selecting check buttons they should get reflected in the entry. The following is my implementation. The one thing I don't like is passing the entry (.e) to both get_values and myok. Is there a better solution to my problem ?

entry  .e -width 15 -relief sunken
button .b -text "..." -command [list get_values .e]
pack .e .b -side left

proc get_values { entry } {
  toplevel .values

  checkbutton .values.c1 -text "C1" -variable c1
  checkbutton .values.c2 -text "C2" -variable c2
  button .values.ok -text "OK" -command [list myok $entry .values]
  button .values.cancel -text "Cancel" -command [list mycancel .values]

  pack .values.c1 .values.c2 -side top
  pack .values.cancel .values.ok -side right
}

proc myok { entry warg } {
 variable c1
 variable c2

 $entry delete 0 end

 if { $c1 } {
  $entry insert insert " "
  $entry insert insert "c1"
 }

 if { $c2 } {
  $entry insert insert " "
  $entry insert insert "c2"
 }

 destroy $warg
}

proc mycancel { warg } {
 destroy $warg
}


You can associate a variable with the entry and use it for entry text updating.

entry  .e -width 15 -relief sunken -textvariable e

(whenever variable e is changed the entry will be updated accordingly)

But in this case you have to pass the name of associated variable instead of name of entry instance, if you want to implement universal get_values and myok functions. If that entry is the only, you of cause can hardcode names instead of passing.

Anyway, I do not see any blunder in your implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消