0

I'm working on small program to work with excel data.But i cant seem to return the value from combo box so findTotal() doesn't work as intended

Excel file here: https://drive.google.com/open?id=1NYYzNvm_QlD2LbP0RLCwG8uuKf7chAC1

import tkinter as tk
from tkinter import ttk
from tkinter import *
import pandas as pd
df = pd.read_excel("some excel file")


def findTotal():
    df["Total"] = (df["Oct"] + df["Nov"] + df["Dec"])*df["$ value"]
    a = df.loc[(df.Country == combo.get()) & (df.incoterm == combo1.get())]
    print(a.Total.sum()/len(a))


root = tk.Tk()
root.title("Data calculator")
root.configure(background="black")
root.resizable(0, 0)

canvas1 = Canvas(root, width=250, height=250, bg='lightsteelblue')
canvas1.pack()

combo = ttk.Combobox(root,
                        values=["USA", "Japan", "China", "Russia"])
combo1 = ttk.Combobox(root,
                        values=["EXW", "FOB", "DAT"])

But = Button(root, fg="red", text="Calculate", command=findTotal())

combo.current(1)
combo1.current(1)

canvas1.create_window(125, 50, window=combo)
canvas1.create_window(125, 110, window=combo1)
canvas1.create_window(125, 200, window=But)

root.mainloop()

When I run the program I get nan, actual output is somewhere around 2******

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Gaaki
  • 53
  • 2
  • 8
  • 2
    Try removing "()" from "command=findTotal" – Himal Jun 10 '19 at 05:40
  • Dupe: [https://stackoverflow.com/questions/8269096/why-is-button-parameter-command-executed-when-declared](https://stackoverflow.com/questions/8269096/why-is-button-parameter-command-executed-when-declared) - close voted already – Patrick Artner Jun 10 '19 at 07:24

1 Answers1

0

If you assign a handler to a button press you need to provide the function name - not call that function. The calling is done when the button is pressed:

But = Button(root, fg="red", text="Calculate", command=findTotal) # just the name here

Suggested as comment by Himal

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69