Still new to this and I’ve created an interesting problem for myself that I can’t solve…
I am trying to design my own “pop-up” box that allows the user to edit an existing string. It is popped up when a button is pressed, displaying the string in an entry box. Once the user has edited the string (or not), he hits the “OK” button and it goes away and the script should now have the new string.
My approach is something like this:
On button press, create a toplevel window with three widgets:
- simple label “Edit string, press ok when done”;
- editable entry containing the predefined string;
- button “OK” that destroys the toplevel window when pressed.
I have sort of got that working but can’t figure out how to get the edited string.
I realize my fundamental problem is I am not thinking in “event-driven” terms. It seems like this should be easily doable but but I can’t see the forest at this point.
What am I missing? Am I over complicating this?
#!/usr/bin/wish
# Create the Pop-up box
proc popUpEntry { labelString } {
global myString
puts "POP:myString = $myString"
set top [toplevel .top]
set labelPop [label $top.labelPop -text $labelString ]
set entryPop [entry $top.entryPop -bg white -width 20 -textvar $myString ]
set buttonPop [button $top.buttonPop -text "Ok" -command { destroy .top } ]
pack $labelPop
pack $entryPop
pack $buttonPop
}
# Pop-up command
proc DoPop {} {
global myString
set popUpLabel "Edit string, press ok when done:"
puts "Before: myString = $myString"
popUpEntry $popUpLabel
puts "After: myString = $myString"
}
# Initalize
set myString "String at start"
# Pop-up button invokes the pop-up command
set buttonPop [button .buttonPop -width 10 -text "Pop" -command {DoPop} ]
pack $buttonPop
#
In this line:
you are setting the
entrycontrol’s-textvarto the contents of the variablemyString.You should set it to be the variable itself by removing the
$symbol: