2017-03-31 120 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

不客气,对我来说是一种享受! (这是我第一次调试别人的代码:-)) –