2013-10-13 53 views
0

我對交互式python編程非常陌生,所以請耐心等待。我在Python 3.3中使用PyCharm。tkinter小部件界面交互按鈕

我試圖建立以下:

我要生成一個交互式窗口有兩個文本輸入框和兩個按鈕拉起功能:

-The第一個按鈕(START)運行小文本搜索功能(我已經編寫和測試過),而第二個按鈕(退出)將退出應用程序。

- 第一個文本輸入字段需要搜索字符串(例如:「Hello Stack World」),而另一個文本輸入字段需要在第一個輸入字符串中搜索字符串(例如:「Stack」) 。

計劃是,一旦填入了兩個文本字段,按下'開始'按鈕將開始文本搜索功能,'QUIT'按鈕則停止程序。

問題是,'QUIT'按鈕的工作方式,但'開始'按鈕什麼也不做。我認爲它實際上將我的程序發送到無限循環。

任何和所有的幫助真的很感激它。我是接口/ widget編程的新手。

在此先感謝!

這裏是我的代碼,因爲我有現在:

import tkinter 
from tkinter import * 

class Application(Frame): 

def text_scan(self): 
    dataf = str(input()) '''string to be searched''' 
    s = str(input())  ''' string to search for''' 
    ''' ... I will leave out the rest of this function code for brevity''' 

def createWidgets(self): 

    root.title("text scan") 
    Label (text="Please enter your text:").pack(side=TOP,padx=10,pady=10) 
    dataf = Entry(root, width=10).pack(side=TOP,padx=10,pady=10) 

    Label (text="Please enter the text to find:").pack(side=TOP,padx=10,pady=10) 
    s = Entry(root, width=10).pack(side=TOP,padx=10,pady=10) 

    self.button = Button(root,text="START",command=self.text_scan) 
    self.button.pack() 

    self.QUIT = Button(self) 
    self.QUIT["text"] = "QUIT" 
    self.QUIT["fg"] = "red" 
    self.QUIT["command"] = self.quit 

    self.QUIT.pack({"side": "left"}) 

def __init__(self, master=None): 
    Frame.__init__(self, master) 
    self.filename = None 
    self.pack() 
    self.createWidgets() 

root = Tk() 
root.title("text scan") 
root.quit() 
app = Application(master=root) 
app.mainloop() 

回答

2

你不能用input混合的GUI。要從輸入小部件獲取值,您需要執行s.get()dataf.get()。但是,在創建小部件之前,您需要刪除對pack的調用,並將其移至單獨的語句。原因是pack返回None,所以目前datafsNone。您還需要將對這些小部件的引用保存爲類屬性。

def text_scan(...): 
    dataf_value = self.dataf.get() 
    ... 
... 
self.dataf = Entry(...) 
self.dataf.pack(...) 
... 
+0

純粹的天才!非常感謝。既然你在這個主題上顯然不是新手,你能指出我的正確方向:我如何讓text_scan功能輸出(搜索結果)在同一個窗口中彈出?現在,它在控制檯中打印出來。再次感謝! –