So, I'm trying to create a program that takes user input, runs functions against parameters to determine what percentage is output, and then combines all the earned percentages together before multiplying them against the total profit (first box they input info into) to give a number. Ex: 15000 profit, qualifies for 10% after the parameters are met, output $1500. In short, its a commission calculator for my organization that I'm building in my down time to make life easier.
I've got most of it built, GUI runs, but when I input numbers and click calculate I get this error and I'm unsure how to resolve it.
**Edited to add code for GUI and button
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\word2\AppData\Local\Programs\Python\Python38-32\lib\tkinter__init__.py", line 1883, in call return self.func(*args) File "C:/Users/word2/PycharmProjects/Commission2/main.py", line 75, in calculatetotal return grossc() + epc() + sprint_elite() * grossp.get() File "C:/Users/word2/PycharmProjects/Commission2/main.py", line 55, in grossc if gross < 5500: TypeError: '<' not supported between instances of 'function' and 'int'
Code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Commission Calculator")
# Defining String variables to call user input
grossp = IntVar()
emergingp = IntVar()
pp = IntVar()
sc = IntVar()
pay = IntVar()
# Function to call user input and display it in the "Payout" Column
def calculategp():
try:
value1 = int(grossp.get())
return value1
except ValueError:
return "error"
def calculateep():
try:
value2 = int(emergingp.get())
return value2
except ValueError:
return "error"
def calculatese():
try:
value3 = sc.get()
value4 = pp.get()
value5 = True
value6 = 0
if value3 >= 65:
return True
if value4 >= 70:
return True
if value3 and value4 is True:
return value5
if value3 and value4 is False:
return value6
except ValueError:
return value6
def grossc():
if gross < 5500:
return 0.0
if gross > 5500 < 10000:
return 0.01
if gross > 10000 < 15000:
return 0.03
if gross > 15000:
return 0.05
def epc():
if emerging <= 6:
return 0.01
def sprint_elite():
if se is True:
return 0.03
def calculatetotal():
return grossc() + epc() + sprint_elite() * grossp.get()
gross = calculategp
emerging = calculateep
se = calculatese()
# Defining Labels
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=N)
# Labels for input boxes
GP = Label(root, padx=5, pady=5, text="Gross Profit")
EP = Label(root, padx=5, pady=5, text="Emerging Products")
Plus_premium = Label(root, padx=5, pady=5, text="Plus/Premium")
Sprint_complete = Label(root, padx=5, pady=5, text="Sprint Complete")
Pay = Label(root, padx=5, pady=5, text="Total Payout")
# Defining input box
grossp_entry = Entry(root, textvariable=grossp, width=20)
emergingp_entry = Entry(root, textvariable=emergingp, width=20)
pp_entry = Entry(root, textvariable=pp, width=20)
sc_entry = Entry(root, textvariable=sc, width=20)
pay_entry = Entry(root, width=20, textvariable=pay)
# Placing input boxes on grid
grossp_entry.grid(row=0, column=1)
emergingp_entry.grid(row=1, column=1)
pp_entry.grid(row=2, column=1)
sc_entry.grid(row=3, column=1)
pay_entry.grid(row=4, column=1)
# Putting labels on grid
GP.grid(row=0, column=0)
EP.grid(row=1, column=0)
Plus_premium.grid(row=2, column=0)
Sprint_complete.grid(row=3, column=0)
Pay.grid(row=4, column=0,)
# Creating button
btn = Button(root, width=10, height=2, text="Calculate", command=calculatetotal)
# Placing button on Grid
btn.grid(row=5, column=0)
root.mainloop()
If you use
print(grossc(), epc(), sprint_elite(), grossp.get())in yourcalculatetotalfunction you will see where the problem is. Results0.0 0.01 None 0. This is one reason you need to take care how you write your if statements when returning data.if se is Trueis not happening here so nothing is returned and thus the value is None forsprint_elite(). That said you are using return in a function attached to a button. This will basically result in nothing happening as your button cannot do anything with a return.You have if statements that return a value but if the condition is not met they don't return a value. Sometimes this is ok but in this case with your math a number must be returned or else the math will error out due to None.
Use
import tkinter as tkinstead of*this will help prevent overwriting other imports or functions/variables you write.You have 2 problems with these 3 lines.
The first 2 lines are missing the
(). That said these should be in the functions as it stands now they are run at the instant of the code being ran and are only ever going to have one value. So if you place them in the functions they belong to then they will return values at the time you press the button.Based on all we talked about in the comments I have cleaned up your code a bit and it should be working how you expected now. Let me know if you have any questions.