I was trying to add tkinter graphics to a small part of my code for some reason and want the output(calculated sum) on window.How did I make this code working please help!
my code:
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get())
time = float(time.get())
interest_rate = float(ir.get())
# simple interest calculating engine
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:", simple_interest)
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
getting error:
C:UsersAnmolAppDataLocalProgramsPythonPython38python.exe "D:/Downloads/si test.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersAnmolAppDataLocalProgramsPythonPython38libtkinter__init__.py", line 1892, in __call__
return self.func(*args)
File "D:/Downloads/si test.py", line 22, in simple_interest
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
TypeError: simple_interest() got an unexpected keyword argument 'Simple_Interest'
Advertisement
Answer
I think you can use Simple_Interest=principal * (interest_rate / 100) * time at line 22.
And you should use the “if” statement in line 17-19
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get() if not isinstance(principal,float) else principal)
time = float(time.get()) if not isinstance(time,float) else time
interest_rate = float(ir.get()) if not isinstance(ir,float) else ir
# simple interest calculating engine
Simple_Interest=principal * (interest_rate / 100) * time
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:n"+str(Simple_Interest))
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
It was worked!
