Function won't run and not sure how to convert string

49 views Asked by At

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()
1

There are 1 answers

2
Mike - SMT On BEST ANSWER
  1. If you use print(grossc(), epc(), sprint_elite(), grossp.get()) in your calculatetotal function you will see where the problem is. Results 0.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 True is not happening here so nothing is returned and thus the value is None for sprint_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.

  2. 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.

  3. Use import tkinter as tk instead of * this will help prevent overwriting other imports or functions/variables you write.

  4. You have 2 problems with these 3 lines.

    gross = calculategp
    emerging = calculateep
    se = calculatese()
    

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.

  1. I did some general clean up on your code so let me know if something confuses you.

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.

import tkinter as tk
from tkinter import ttk


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
        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 0
    except ValueError:
        return 0


def grossc():
    gross = calculategp()
    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 calculateep() <= 6:
        return 0.01
    else:
        return 0


def sprint_elite():
    if calculatese() is True:
        print('True')
        return 0.03
    else:
        return 0


def calculatetotal():
    print(grossc(), epc(), sprint_elite(), grossp.get())
    pay.set(grossc() + epc() + sprint_elite() * grossp.get())


root = tk.Tk()
root.title("Commission Calculator")
emergingp = tk.IntVar()
grossp = tk.IntVar()
pay = tk.IntVar()
pp = tk.IntVar()
sc = tk.IntVar()

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky='n')

tk.Label(root, padx=5, pady=5, text="Gross Profit").grid(row=0, column=0)
tk.Label(root, padx=5, pady=5, text="Emerging Products").grid(row=1, column=0)
tk.Label(root, padx=5, pady=5, text="Plus/Premium").grid(row=2, column=0)
tk.Label(root, padx=5, pady=5, text="Sprint Complete").grid(row=3, column=0)
tk.Label(root, padx=5, pady=5, text="Total Payout").grid(row=4, column=0)
tk.Entry(root, textvariable=grossp, width=20).grid(row=0, column=1)
tk.Entry(root, textvariable=emergingp, width=20).grid(row=1, column=1)
tk.Entry(root, textvariable=pp, width=20).grid(row=2, column=1)
tk.Entry(root, textvariable=sc, width=20).grid(row=3, column=1)
tk.Entry(root, textvariable=pay, width=20).grid(row=4, column=1)

btn = tk.Button(root, width=10, height=2, text="Calculate", command=calculatetotal)
btn.grid(row=5, column=0)
root.mainloop()