如何从Tkinter组合框返回值

时间:2019-06-10 05:38:28

标签: python python-3.x tkinter

我正在使用小型程序来处理excel数据。但是我似乎无法从组合框中返回值,所以findTotal()不能按预期工作

此处为Excel文件: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()

当我运行程序时,我得到的是nan,实际输出约为2 ******

1 个答案:

答案 0 :(得分:0)

如果您为按钮按下分配了处理程序,则需要提供函数名称-而不是调用该函数。按下按钮后完成呼叫

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

Himal建议作为评论