【问题标题】:Trying to create an editable Pop-up box试图创建一个可编辑的弹出框
【发布时间】:2016-06-28 04:59:45
【问题描述】:

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

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

我的方法是这样的:

按下按钮时,创建一个包含三个小部件的顶层窗口:

  • 简单的标签“编辑字符串,完成后按确定”;
  • 包含预定义字符串的可编辑条目;
  • 按钮“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

#

【问题讨论】:

    标签: tcl tk


    【解决方案1】:

    在这一行:

    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 ]
    

    【讨论】:

    • 啊。事后如此明显。谢谢!
    【解决方案2】:

    除了 -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
    

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 2014-03-09
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多