Joe
Joe

Reputation: 397

tkinter input and output in Python 3

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.

Upvotes: 0

Views: 1299

Answers (1)

user7744776
user7744776

Reputation:

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.

Upvotes: 2

Related Questions