2017-03-31 21 views
1

guiを作成しました。tkinterを使用しています。Python 3のtkinterの入出力。

ユーザーからの入力としてファイル名を受け取り、ファイルを開き、関数によって生成されたテキストのメッセージボックスを表示したいとします。

以下はコードですが、なぜこれが機能しないのか説明できますか?

import tkinter as tk 
    import csv 
    import tkinter.simpledialog 
    import tkinter.messagebox 
    from tkinter import ttk 

    file=tkinter.simpledialog.askstring("File: ","Enter your file name") 

    with open(file, 'r') as f: #this line reads the file 
    reader = csv.reader(f, delimiter=',') 

    output=values 
    def values(): #And this is the function 
    print("Some text")#which should return whatever info is inside 'print' function 


    def __init__(self, parent, controller): 
    tk.Frame.__init__(self, parent) 
    self.controller = controller 
    button = ttk.Button(self, text="Submit", #I prefer using the button but any other way will do 
         command=tkmessagebox.showinfo("Results",output)) 
    button.pack() 

"name 'tksimpledialog' not defined"エラーが発生しました。

+0

予想される結果が説明されていますが、エラーは何ですか? – dparoli

+0

質問を編集しました。 – Yar

答えて

2

あなたが動作するようにaskstring機能のためのウィンドウを必要とする:

... 
window = tk.Tk() 
window.withdraw() #hides the window 
file = tkinter.simpledialog.askstring("File: ","Enter your file name") 
... 

次に、あなたのラインといくつかの問題があります:

output=values 

それは、関数の定義の後に置かれるべきではなく、前。 最後に括弧が含まれています。 Like:

def values(): #And this is the function 
    print("Some text") 
    # which should return whatever info is inside 'print' function 
output=values() 

これは、スクリプトの実行時に発生したエラーを解決します。

+0

ありがとうございました!コードは – Yar

+0

で動作します歓迎です、私にとっては喜びでした! (私は誰かのコードをデバッグするのは初めてです:-)) –

関連する問題