2016-11-06 11 views
0

私はかなり新しいpythonですが、現在学校プロジェクトに取り組んでいますが、私の目標はデータファイルの検索に使用できる検索バーを作成することですが、私は苦労しています検索バーが正しく機能するようにします。私はtkinterエントリウィジェットを使用しています。 .get()を呼び出すと、エントリウィジェットの文字列は表示されません。ここに私のコードは...Tkinterエントリウィジェット.getが機能しない

from tkinter import * 

def searchButton(): 
     text = searched.get() 
     print (text) 

def drawStatWindow(): 
    global searched 
    statWindow = Tk() 
    statWindow.title("View Statistics") 
    statWindow.config(bg = "grey") 
    statWindow.geometry('800x900') 

    searched = StringVar() 
    searchBox = Entry(statWindow, textvariable = searched) 
    searchBox.place(x= 450, y=50, width = 200, height = 24) 
    enterButton = tkinter.Button(statWindow, text ="Enter", command =searchButton) 
    enterButton.config(height = 1, width = 4) 
    enterButton.place(x=652, y=50) 

drawStatWindow() 

入力ウィジェットに文字列を入力してEnterボタンを押すと何も起こりません。 私は経験はあまりありませんが、これは私の最初のプロジェクトですが、tkinterのエントリーウィジェットについて読むと、なぜこれがうまくいかないのか理解できません。 私はpython V3.4.0を使用しています ありがとうございました。

+0

質問のコードは実行されません。問題を説明する_actual_コードを投稿する時間を取ってください。 –

答えて

0

あなたのコードにはmainloop()がありません。

statWindow.mainloop() 

あなたがクラスにコードを再構築することがあります:あなたはdrawStatWindow()関数の最後にそれを追加してみてください可能性があります。これは、あなたがグローバル変数の使用を避けることを可能にすると、一般的に、アプリケーションのためのより良い組織提供:

from tkinter import * 

class App: 
    def __init__(self, statWindow): 
     statWindow.title("View Statistics") 
     statWindow.config(bg = "grey") 
     statWindow.geometry('800x900') 

     self.searched = StringVar() 
     searchBox = Entry(statWindow, textvariable=self.searched) 
     searchBox.place(x= 450, y=50, width = 200, height = 24) 
     enterButton = Button(statWindow, text ="Enter", command=self.searchButton) 
     enterButton.config(height = 1, width = 4) 
     enterButton.place(x=652, y=50) 

    def searchButton(self): 
     text = self.searched.get() 
     print(text) 


root = Tk() 
app = App(root) 
root.mainloop() 
+0

ありがとう、私はこのような私のコードを再構成したいと思っていますが、私は前にクラスを使用したことがないので、これは私に良い出発点を与えます。私はこれをさらに調べて、このような私の全プログラムを再構築しようとします。 –

-2

textvariableを使用する必要はありませんが、あなたはこれを使用する必要があります。

searchBox = Entry(statWindow) 
searchBox.focus_set() 
searchBox.place(x= 450, y=50, width = 200, height = 24) 

は、あなたが検索ボックスを使用することができます.get()、それは文字列になります。

+0

ありがとうございますこれは正常に動作します!ちょうど興味のないfocus_set()は何ですか? –

+0

それは、エントリに書かれた文字列を返します、私はそれをよく知っていません。 – Karak

+1

いいえ、 'focus_set()'は、 "エントリに書き込まれた文字列を返しません"。名前が意味するように、それは単にエントリにフォーカスを設定します。 'searchBox.get()'の結果には影響しません。 –

0

に実行する必要があるため、mainloop()を追加する必要があります。

tkinterを使用するIDLEでコードを実行すると、IDLEが自身のmainloop()を実行し、コードは正常に動作しますが、通常は最後にmainloop()を追加する必要があります。

tkintertkinter.Buttonに削除する必要があります。

from tkinter import * 

def searchButton(): 
    text = searched.get() 
    print(text) 

def drawStatWindow(): 
    global searched 

    statWindow = Tk() 
    statWindow.title("View Statistics") 
    statWindow.config(bg="grey") 
    statWindow.geometry('800x900') 

    searched = StringVar() 

    searchBox = Entry(statWindow, textvariable=searched) 
    searchBox.place(x= 450, y=50, width=200, height=24) 

    # remove `tkinter` in `tkinter.Button` 
    enterButton = Button(statWindow, text="Enter", command=searchButton) 
    enterButton.config(height=1, width=4) 
    enterButton.place(x=652, y=50) 

    # add `mainloop()` 
    statWindow.mainloop() 

drawStatWindow() 
関連する問題