2017-03-31 132 views
1

我一直在創建一個gui使用tkintertkinter在Python中的輸入輸出3

我想從用戶接收一個文件名作爲輸入,打開該文件並顯示與由函數生成的文本的消息框。

下面是代碼,有人可以解釋爲什麼這是行不通的?

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'未定義「錯誤。

+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 

應該放在函數的定義後,不之前。 並在最後包含括號。像:

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

這修復了我在嘗試運行腳本時遇到的錯誤。

+0

謝謝!代碼作品 – Yar

+0

不客氣,對我來說是一種享受! (這是我第一次調試別人的代碼:-)) –