2016-06-17 19 views
-3

機能が実現する必要があります:Tkinterボタンをクリックすると、エントリのテキストが変更されます。ボタンをクリックしたときのTkinterエントリのテキストの変更

import Tkinter as tk 

def create_heatmap_button_callback(): 
    path_entry.delete(0, tk.END) 
    path_entry.insert(0, "clicked!")  

def main(): 
    root = tk.Tk() 
    path_entry = tk.Entry(master = root, text = "not clicked") 
    path_entry.grid(row=1, column=0, sticky = tk.W) 

    create_heatmap_button = tk.Button(master = root, text = "create map", command = create_heatmap_button_callback) 
    create_heatmap_button.grid(row=2,column=0,sticky = tk.W) 

    tk.mainloop() 

if __name__ == "__main__": 
    global path_entry 
    main() 

し、ボタンをクリックしたときに、ここでの出力があります:ここではコードスニペットです

NameError:グローバル名「path_entry」が

が定義されていないこれを行うための正しい方法は何ですか?

+0

Tkinterをどのようにインポートしていますか? – Li357

+1

私はこれがモジュールレベルのコードではないと仮定しています。そうでなければ、 'path_entry'はグローバルに非常に明確に定義されています。コンテキストでコードスニペットを表示してください。 –

+1

私はあなたのコードを実行すると、 'name 'tk'が定義されていません'、グローバル名 'path_entry'が定義されていません。あなたの問題を示す[mcve]を提供してください。 – Kevin

答えて

1

私はおそらく、path_entryをglobal.Pythonのグローバル変数の動作として宣言する必要があることがわかりました。他の言語とは異なります。

import Tkinter as tk 

def create_heatmap_button_callback(): 
    #global path_entry 
    path_entry.delete(0, tk.END) 
    path_entry.insert(0, "clicked!") 

def main():  
    root = tk.Tk() 
    global path_entry 
    path_entry = tk.Entry(master = root, text = "not clicked") 
    path_entry.grid(row=1, column=0, sticky = tk.W) 

    create_heatmap_button = tk.Button(master = root, text = "create map", command = create_heatmap_button_callback) 
    create_heatmap_button.grid(row=2,column=0,sticky = tk.W) 

    tk.mainloop() 

if __name__ == "__main__": 

    main() 
関連する問題