2014-11-02 84 views
-1

我正在處理我的第一個Python GUI,我試圖修改這個tkinter example,但我根本無法弄清楚如何編寫一個回調函數,它將會傳遞給OK按鈕輸入的主程序的值。Python:Tkinter OK按鈕回調函數

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

from Tkinter import Tk, BOTH, StringVar, IntVar 
from ttk import Frame, Button, Style, Label, Entry 

class Example(Frame): 

    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 
     self.initUI() 

    def initUI(self): 
     self.parent.title("Get Value") 
     self.style = Style() 
     self.style.theme_use("default") 
     self.pack(fill=BOTH, expand=1) 

     valueLabel = Label(self, text="Value: ") 
     valueLabel.place(x=10, y=10) 
     value=StringVar(None) 
     value.set("this is the default value") 
     valueEntry=Entry(self, textvariable=value) 
     valueEntry.place(x=70, y=10) 

     quitButton = Button(self, text="Quit", command=self.quit) 
     quitButton.place(x=10, y=50) 

     okButton = Button(self, text="OK", command=self.quit) 
     okButton.place(x=120, y=50) 

def main(): 

    root = Tk() 
    root.geometry("220x100+300+300") 
    app = Example(root) 
    root.mainloop() 

if __name__ == '__main__': 
    main() 

我讀過一大堆教程,但沒有一個能夠清楚地解釋這一點。理論上,我應該能夠通過value.get()獲得選定的值,但不管我放在哪裏,都會收到錯誤消息。另外,AFAIK,我應該能夠用value.set()定義一個默認值,但是這似乎沒有效果,因爲當我運行程序時文本框是空的。

  1. 什麼是在root.mainloop()終止後將值傳遞給主python程序的最簡單方法? (實際的對話框包含幾個用於輸入字符串和整數值的輸入框。)

    即,我希望能夠使用的東西,如:

    root = Tk() 
    root.geometry("220x100+300+300") 
    app = Example(root) 
    root.mainloop() 
    print value 
    print value2   
    print value3 
    
  2. 如何定義輸入框的默認值?

回答

2

更改每個出現的value變量與self.value。這應該修復它,並顯示默認值。

UPDATE

from Tkinter import Tk, BOTH, StringVar, IntVar 
from ttk import Frame, Button, Style, Label, Entry 

class Example(Frame): 

    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 
     self.initUI() 

    def showMe(self): 
     print(self.value.get()) 

    def initUI(self): 
     self.parent.title("Get Value") 
     self.style = Style() 
     self.style.theme_use("default") 
     self.pack(fill=BOTH, expand=1) 

     valueLabel = Label(self, text="Value: ") 
     valueLabel.place(x=10, y=10) 
     self.value=StringVar(None) 
     self.value.set("this is the default value") 
     valueEntry=Entry(self, textvariable=self.value) 
     valueEntry.place(x=70, y=10) 

     quitButton = Button(self, text="Quit", command=self.quit) 
     quitButton.place(x=10, y=50) 

     okButton = Button(self, text="OK", command=self.showMe) 
     okButton.place(x=120, y=50) 




def main(): 

    root = Tk() 
    root.geometry("220x100+300+300") 
    app = Example(root) 
    root.mainloop() 


if __name__ == '__main__': 
    main() 
+0

我已經試過了。它不起作用。 – 2014-11-02 13:51:32

+1

@NemoXXX,你是否將'textvariable = value'改爲'textvariable = self.value'? – Kidus 2014-11-02 13:57:35

+0

如果我將textvariable = value更改爲textvariable = self.value,我收到以下錯誤消息: 'valueEntry = Entry(self,self.textvariable = value) SyntaxError:關鍵字不能爲表達式' – 2014-11-02 14:02:14

0

無論您quitButton和okButton調用self.quit功能。所以,當你按OK按鈕時,你輸入的值是什麼,你是否正在調用具有自己問題的退出函數,這也超出了你的問題的範圍。 嘗試將value定義爲self.value,並使okButton調用一個函數:print self.value.get()。

+0

我意識到兩個按鈕都是完全相同的事實,我只是添加了該函數,因爲表單不會另外加載。 – 2014-11-02 13:50:52