尝试创建可编辑的弹出框

时间:2012-08-02 21:15:04

标签: tcl tk

还是新手,我为自己创造了一个有趣的问题,我无法解决......

我正在尝试设计自己的“弹出式”框,允许用户编辑现有的字符串。按下按钮时会弹出它,在输入框中显示该字符串。一旦用户编辑了字符串(或不编辑),他就会点击“确定”按钮然后消失,脚本现在应该有新的字符串。

我的方法是这样的:

按下按钮,创建一个带有三个小部件的顶层窗口:

  • 简单标签“编辑字符串,完成后按确定”;
  • 包含预定义字符串的可编辑条目;
  • 按钮“OK”按下时会破坏顶层窗口。

我有点工作,但无法弄清楚如何获得编辑过的字符串。

我意识到我的根本问题是我不是在考虑“事件驱动”的术语。看起来这应该很容易,但我现在看不到森林了。

我错过了什么?我是不是太复杂了?

#!/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

#

2 个答案:

答案 0 :(得分:2)

在这一行:

set entryPop [entry $top.entryPop -bg white -width 20 -textvar $myString ]

您要将entry控件的-textvar设置为变量myString内容

您应该通过删除$符号将其设置为变量:

set entryPop [entry $top.entryPop -bg white -width 20 -textvar myString ]

答案 1 :(得分:0)

除了-textvar $ myString之外,代码将无效,因为popUpEntry函数将在创建弹出窗口后立即返回 - 在用户有机会输入新内容之前。

你必须等待弹出窗口关闭。这可以使用popUpEntry中的另一个全局变量来完成:

...
global popup_closed
...
set buttonPop [button $top.buttonPop -text "Ok" -command {
      set edit_ready 1 
      destroy .top
      }

...

set edit_ready 0
popUpEntry $popUpLabel
vwait edit_ready
相关问题