Home > Software engineering >  Display the output of a function on a Python GUI Calculator
Display the output of a function on a Python GUI Calculator

Time:04-27

I have created a GUI calculator and would like to include an additional function "probate". The normal standard calculator portion of the GUI works fine however when I attempted to include the special function and click on the probate button in the GUI nothing happens. The code for part of the calculator is shown below. I have excluded many of the digits and operators of the calculator.

from tkinter import *
def click(num):
    global op
    op=op str(num)
    iptext.set(op)
def evaluate():
    global op
    output = str(eval(op))
    iptext.set(output)

def clearDisplay():
    global op
    op=""
    iptext.set(op)

def probate(op):

    # global probate_fee
    VALUE_CATEGORY1 = 10000
    VALUE_CATEGORY2 = 250000
    RATE_CATEGORY1 = 0.05
    RATE_CATEGORY2 = 0.03
    RATE_CATEGORY3 = 0.01
    estate_worth=op

    if (estate_worth) < 0:
        # is_on = False
        print("Invalid Enrty")
    else:
        if (estate_worth <= VALUE_CATEGORY1):
            probate_fee = 500
        elif ((estate_worth > VALUE_CATEGORY1) & (estate_worth <= VALUE_CATEGORY2)):
            probate_fee = (estate_worth - VALUE_CATEGORY1) * RATE_CATEGORY2   VALUE_CATEGORY1 * RATE_CATEGORY1
        elif (estate_worth > VALUE_CATEGORY2):
            probate_fee = (estate_worth - VALUE_CATEGORY2) * RATE_CATEGORY3   (
                        (VALUE_CATEGORY2 - VALUE_CATEGORY1) * RATE_CATEGORY2)   VALUE_CATEGORY1 * RATE_CATEGORY1
        else:
            print("Invalid Entry")
    return str(probate_fee)

calc=Tk()
calc.title("GUI Calculator")
op=""
iptext=StringVar()
iparea=Entry(calc,font=('large,_font',15,'bold'),bd=10,justify="right",insertwidth=4,textvariable=iptext).grid(columnspan=10)
bt7=Button(calc,font=('arial',15,'bold'),command=lambda:click(7),bg="lavender",text="7",bd=5,padx=15,pady=10).grid(row=1,column=0)
bt8=Button(calc,font=('arial',15,'bold'),command=lambda:click(8),bg="lavender",text="8",bd=5,padx=15,pady=10).grid(row=1,column=1)
bt9=Button(calc,font=('arial',15,'bold'),command=lambda:click(9),bg="lavender",text="9",bd=5,padx=15,pady=10).grid(row=1,column=2)
add=Button(calc,font=('arial',15,'bold'),command=lambda:click(' '),bg="lavender",text=" ",bd=5,padx=15,pady=10).grid(row=1,column=3)
eql=Button(calc,font=('arial',15,'bold'),command=evaluate,bg="lavender",text="=",bd=5,padx=15,pady=10).grid(row=4,column=2)
div=Button(calc,font=('arial',15,'bold'),command=lambda:click('/'),bg="lavender",text="/",bd=5,padx=15,pady=10).grid(row=4,column=3)
btC=Button(calc,font=('arial',15,'bold'),command=clearDisplay,bg="lavender",text="C",bd=5,padx=15,pady=10).grid(row=4,column=1)
btprobate = Button(calc, font=('arial', 15, 'bold'), command=lambda: evaluate, bg="lavender", text="probate", bd=5, padx=0,
                 pady=10).grid(columnspan=10)

calc.mainloop(),

CodePudding user response:

The reason nothing happens is that you need to include "()" after the function name for btprobate. E.g.:

btprobate = Button(calc, font=('arial', 15, 'bold'), command=lambda: evaluate(), bg="lavender", text="probate", bd=5, padx=0,
                 pady=10).grid(columnspan=10)

I'm not sure what you're trying to do, but here's a stab at code that functions as requested in the comments (it runs, the probate button does stuff):

from tkinter import *
def click(num):
    global op
    op=op str(num)
    iptext.set(op)
def evaluate():
    global op
    output = str(eval(op))
    iptext.set(output)

def clearDisplay():
    global op
    op=""
    iptext.set(op)

def probate(op):

    # global probate_fee
    VALUE_CATEGORY1 = 10000
    VALUE_CATEGORY2 = 250000
    RATE_CATEGORY1 = 0.05
    RATE_CATEGORY2 = 0.03
    RATE_CATEGORY3 = 0.01
    estate_worth=eval(op)

    if (estate_worth) < 0:
        # is_on = False
        print("Invalid Enrty")
    else:
        if (estate_worth <= VALUE_CATEGORY1):
            probate_fee = 500
        elif ((estate_worth > VALUE_CATEGORY1) & (estate_worth <= VALUE_CATEGORY2)):
            probate_fee = (estate_worth - VALUE_CATEGORY1) * RATE_CATEGORY2   VALUE_CATEGORY1 * RATE_CATEGORY1
        elif (estate_worth > VALUE_CATEGORY2):
            probate_fee = (estate_worth - VALUE_CATEGORY2) * RATE_CATEGORY3   (
                        (VALUE_CATEGORY2 - VALUE_CATEGORY1) * RATE_CATEGORY2)   VALUE_CATEGORY1 * RATE_CATEGORY1
        else:
            print("Invalid Entry")
        iptext.set(str(probate_fee))
        
    # return str(probate_fee)

calc=Tk()
calc.title("GUI Calculator")
op=""
iptext=StringVar()
iparea=Entry(calc,font=('large,_font',15,'bold'),bd=10,justify="right",insertwidth=4,textvariable=iptext).grid(columnspan=10)
bt7=Button(calc,font=('arial',15,'bold'),command=lambda:click(7),bg="lavender",text="7",bd=5,padx=15,pady=10).grid(row=1,column=0)
bt8=Button(calc,font=('arial',15,'bold'),command=lambda:click(8),bg="lavender",text="8",bd=5,padx=15,pady=10).grid(row=1,column=1)
bt9=Button(calc,font=('arial',15,'bold'),command=lambda:click(9),bg="lavender",text="9",bd=5,padx=15,pady=10).grid(row=1,column=2)
add=Button(calc,font=('arial',15,'bold'),command=lambda:click(' '),bg="lavender",text=" ",bd=5,padx=15,pady=10).grid(row=1,column=3)
eql=Button(calc,font=('arial',15,'bold'),command=evaluate,bg="lavender",text="=",bd=5,padx=15,pady=10).grid(row=4,column=2)
div=Button(calc,font=('arial',15,'bold'),command=lambda:click('/'),bg="lavender",text="/",bd=5,padx=15,pady=10).grid(row=4,column=3)
btC=Button(calc,font=('arial',15,'bold'),command=clearDisplay,bg="lavender",text="C",bd=5,padx=15,pady=10).grid(row=4,column=1)
btP=Button(calc,font=('arial',15,'bold'),command=lambda: probate(op), bg="lavender", text="probate", bd=5, padx=0, pady=10).grid(columnspan=10)

calc.mainloop()
  • Related