2012-08-02 72 views
0

不過新來這和我創建了一個有趣的問題我自己,我解決不了......想要創建一個可編輯的彈出框

我試圖設計我自己的「彈出「框允許用戶編輯現有的字符串。當按下按鈕時彈出,在輸入框中顯示字符串。一旦用戶編輯了字符串(或不),他點擊「確定」按鈕,它就會消失,腳本現在應該有新的字符串。

我的做法是這樣的:

按下按鈕,創建一個頂層窗口有三個部件:

  • 簡單的標籤「編輯字符串,按確定完成時」;
  • 包含預定義字符串的可編輯條目;
  • 按下「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

在這一行:

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 ] 
+0

啊。事實如此明顯。謝謝! – user1074069 2012-08-03 14:42:10

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