How to use Entry values for tkinter python.
I having a bit of trouble with the tkinter module and my idea about
functions is not very clear either.
I'm attempting to create a compound interest calculator using tkinter.
The problem can be fixed.
There are two options for layout manager.
Using grid layout is more advantage over pack().
Avoid wildcard import *. Use this import tkinter as tk then use prefix tk. for widgets.
Option 1: For grid geometry manager layout merely.
- You cannot use all three geometry manager on one line.
widget(...)`.grid().pack()
- Add four
StringVar() for each Entry widgets.
- Add the
Res.configure(text= ....) to the compound_interest
function.
- Add Button widget for updating result.
Snippet:
import tkinter as tk
win = tk.Tk()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
def compund_interest():
#print( f'Result: {E1.get()}* {1 + E2.get()/100*E3.get()**E3.get()*E4.get()}')
Res.configure(text='Result: ' + str(float(E1.get())*(1+(float(E2.get())/100*float(E3.get()))**(float(E3.get())*float(E4.get())))))
L1 = tk.Label(win, text = 'Principle amount:')
L1.grid(row=0, column=0, stick='w')
L2 = tk.Label(win, text = 'Interest rate:')
L2.grid(row=1, column=0, stick='w')
L3 = tk.Label(win, text = 'Total time in years:')
L3.grid(row=2, column=0, stick='w')
L4 = tk.Label(win, text = 'No of times the compound interest has to be applied per year:')
L4.grid(row=3, column=0, stick='w',columnspan=3)
E1 = tk.Entry(win, textvariable=ent_var)
E1.grid(row=0, columnspan=3)
E2 = tk.Entry(win)
E2.grid(row=1, columnspan=3)
E3 = tk.Entry(win)
E3.grid(row=2, columnspan=3)
E4 = tk.Entry(win)
E4.grid(row=3, column=3)
Res = tk.Label(win)
Res.grid(row=4, column=0, stick='w')
btn= tk.Button(win, text='Click', command=compund_interest)
btn.grid(row=5, column=1)
win.mainloop()
Option 2 for place() manager layout.
Snippet:
import tkinter as tk
win = tk.Tk()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
ent_var = tk.StringVar()
def compund_interest():
#print( f'Result: {E1.get()}* {1 + E2.get()/100*E3.get()**E3.get()*E4.get()}')
Res.configure(text='Result: ' + str(float(E1.get())*(1+(float(E2.get())/100*float(E3.get()))**(float(E3.get())*float(E4.get())))))
L1 = tk.Label(win, text = 'Principle amount:')
L1.place(x= 0, y= 0)
L2 = tk.Label(win, text = 'Interest rate:')
L2.place(x= 0, y= 20)
L3 = tk.Label(win, text = 'Total time in years:')
L3.place(x= 0, y= 40)
L4 = tk.Label(win, text = 'No of times the compound interest has to be applied per year:')
L4.place(x=0, y=60)
E1 = tk.Entry(win, textvariable=ent_var)
E1.place(x=105, y= 0)
E2 = tk.Entry(win)
E2.place(x=105, y=20)
E3 = tk.Entry(win)
E3.place(x=105, y= 40)
E4 = tk.Entry(win)
E4.place(x=335, y= 60)
Res = tk.Label(win)
Res.place(x=0, y=80)
btn= tk.Button(win, text='Click', command=compund_interest)
btn.place(x=100, y=100)
win.mainloop()
Screenshot:
