I'm trying to make a basic interest income calculator using GUI tkinker in Python. However, after entering all the values, the label at the end doesn't update.
Please note that calculations is just printing the variables for now
import tkinter as tk
frame = tk.Tk()
frame.title("Interest Income Calculator")
frame.geometry('800x450')
label = tk.Label(text="Enter Balance (number, no symbols)")
entry = tk.Entry(fg="Black", bg="White", width=50)
label.pack()
entry.pack()
def update1():
global balance
balance = entry.get()
buttonupdate1 = tk.Button(
text = "Save")
buttonupdate1.pack()
buttonupdate1.config(command = update1())
label2 = tk.Label(text="Enter Standard Interest (percentage but no symbol)")
entry2 = tk.Entry(fg="Black", bg="White", width=50)
label2.pack()
entry2.pack()
def update2():
global sinterest
sinterest = entry2.get()
buttonupdate2 = tk.Button(
text = "Save")
buttonupdate2.pack()
buttonupdate2.config(command = update2())
label3 = tk.Label(text="Enter Bonus Interest (percentage but no symbol)")
entry3 = tk.Entry(fg="Black", bg="White", width=50)
label3.pack()
entry3.pack()
def update3():
global binterest
binterest = entry3.get()
buttonupdate3 = tk.Button(
text = "Save")
buttonupdate3.pack()
buttonupdate3.config(command = update3())
button = tk.Button(
text="Calculate",
width=25,
height=1,
background="white",
foreground="black",
)
button.pack()
calculations = (balance, sinterest, binterest)
label4 = tk.Label(
text = (calculations),
foreground = "black",
background="white",
width=25,
height=10
)
def cont():
label4.pack()
button.config(command=cont())
frame.mainloop()
I've tried so many things, like moving around the functions etc. with no luck. please help
CodePudding user response:
To understand why it doesn't work when we use command = func()
we have to understand how passing function as an argument works
When we pass function with parentheses as an argument, we are first calling the function and then giving the returned value as an argument for the method or function we are using. For example
def sum():
return 1 1
example1 = sum
example2 = sum()
# -> example1 is a function object
# -> example2 is 2
This is why in your code you have to give your button a function object rather than the return value of the function so it can run the function object when you press the button.
I ran your code with these fixes and noticed it doesn't run. This is because in your code you are defining your variables sinterest
, balance
and binterest
inside your functions and it throws an error because now you don't run the functions when the code runs.
Also you are trying to change values inside tuple, which is impossible since tuples are immutable
calculations = (balance, sinterest, binterest)
You should change it to a list, because you can change the value inside list
calculations = [balance, sinterest, binterest]
CodePudding user response:
(command = function) because you are passing the function not calling the function()