0

I have been creating a gui using tkinter.

I want to receive a filename as input from the user, open the file and display a message box with text generated by a function.

Below is the code, can someone explain why this is not working?

   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()

I get "name 'tksimpledialog' is not defined" error.

2
  • you described the expected result but what is the error? Commented Mar 31, 2017 at 20:35
  • Edited the question. Commented Mar 31, 2017 at 20:44

1 Answer 1

2

You need a window for the askstring function to work:

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

Then there is some issue with your line :

output=values

It should be placed after the definition of the function, not before. And contain parenthesis at the end. Like :

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

This fixes the error I had when trying to run your script.

Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome, it was a pleasure for me! (it's the first time I debug someone else's code :-))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.