1

Using the Python Tkinter module, I'm trying to build an app which accepts user data; Calls a function on a pandas dataframe-- created via a csv file; then outputs the result to the ap's screen box.

Here is my attempt, I do not get any output:

import pandas as pd
import ttk

def parsefile(*args):
    try:
      df=pd.read_csv("dataforcluster.csv")

      # do something to the data...

      df.ix[(df.sp-x).abs().argsort()[:5]]

    except ValueError:
      pass

root = Tk()
root.title("Closest_5")

 mainframe = ttk.Frame(root, padding="3 3 12 12")
 mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
 mainframe.columnconfigure(0, weight=1)
 mainframe.rowconfigure(0, weight=1)

 sp_price_change = StringVar()
 output = StringVar()

 sp_price_change_entry = ttk.Entry(mainframe, width=7, textvariable=sp_price_change)
 sp_price_change_entry.grid(column=2, row=1, sticky=(W, E))

 ttk.Label(mainframe, textvariable=output).grid(column=2, row=2, sticky=(W, E))
 ttk.Button(mainframe, text="Calculate", command=parsefile).grid(column=3, row=3, sticky=W)

 ttk.Label(mainframe, text="sp_price_change").grid(column=3, row=1, sticky=W)
 ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
 ttk.Label(mainframe, text="output").grid(column=3, row=2, sticky=W)

 for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

   sp_price_change_entry.focus()
   root.bind('<Return>', calculate)

  root.mainloop()

My sample data can be obtained here: https://www.dropbox.com/s/yxx0rtio15dzmfq/dataforcluster.csv

Thanks in advance for any suggestions.

3
  • You haven't posted your calculate function. Either way, you need to update your 'output' variable with a string form of the result with output.set(<result>) Commented Jul 24, 2013 at 21:18
  • Sorry please consider that to read 'parsefile'. (That's how I compiled it). When I add your suggestion just above root.mainloop() , I get 'syntax error'. Am I not understanding what you mean by <result>? Commented Jul 24, 2013 at 21:41
  • What sort of output are you expecting? A print statement? Display in a widget? What widget? Commented Jul 30, 2013 at 2:16

1 Answer 1

1

You're leaving out some important details in your question. Let me take a guess.

My guess is, you're expecting the result of the calculation to appear in the label that has the string "output", correct?

If so, you need to do two things:

  1. change your label definition to ttk.Label(mainframe, textvariable=output1). Notice that I use the attribute textvariable rather than text, and I use the variable name rather than a string that contains the variable name. This ties the label text to the value of that variable; when the variable changes, so does the label.

  2. Whenever you are done calculating your result, you need to do output.set(...), and replace ... with the result you want to have displayed. I don't know what variable contains the result you want to show, though, but presumably you have some variable somewhere that has the data you want to show.

An alternate suggestion is to do the following:

  1. throw away the output variable
  2. save a reference to your widget, eg: the_output_widget = Label(...). Note that you can not call .grid(...) on the same line, because that will set the_output_widget to None. It's generally a best practice to separate widget creation and widget layout anyway
  3. when you have a result you want displayed, reconfigure the widget with configure, eg: the_output_widget.configure(text=the_result) (where the_result is whatever variable you have that contains the result).
Sign up to request clarification or add additional context in comments.

Comments

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.