h4kr
h4kr

Reputation: 248

How to use Entry values for tkinter python

I'm self-learning python and currently am at beginners level. 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. Here is my code:

from tkinter import *

win = Tk()
    
L1 = Label(win, text = 'Principle amount:').pack()
L2 = Label(win, text = 'Interest rate:').grid(row = 1, column = 0).pack()
L3 = Label(win, text = 'Total time in years:').grid(row = 2, column = 0).pack()
L4 = Label(win, text = 'No of times the compound interest has to be applied per year:').grid(row = 3, column = 0).pack()
E1 = Entry(win).pack()
E2 = Entry(win).pack()
E3 = Entry(win).pack()
E4 = Entry(win).pack()
Res = Label(win, text = 'Result: ' + str(float(L1.get())*(1+(float(L2.get())/100*float(L3.get()))**(float(L3.get())*float(L4.get())))))

I'm attempting to perform the calculations under the Label: "Res". I had seen in another code that .get() is used to use the Entry values to do the calculations(that was a simpler one involving a calculator and different from this).

However, it is showing the following error:
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

I don't above any idea what the above error statement means, so please explain what is wrong with my code. Any help is appreciated.

Upvotes: 0

Views: 65

Answers (2)

toyota Supra
toyota Supra

Reputation: 4537

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:

enter image description here

Upvotes: 0

Ismail Hafeez
Ismail Hafeez

Reputation: 740

What the error basically means is that you the grid() function to place your widgets in your windows which already has widgets using pack()

Your label L1 uses Pack while L2 uses grid. To get rid of this error you have to use either grid() or pack() for all widgets in a window.

Also, why are you doing grid().pack()? Those are separate functions that place the widgets in different ways.

grid() is used to lay out widgets in a grid. Another answer says it "overlays a graph" which is a bit of a misnomer. It doesn't overlay anything, it merely arranges widgets along row and column boundaries. It is great for creating tables and other structured types of layouts.

pack() lays things out along the sides of a box. It excels at doing layouts where everything is on a single row or in a single column (think rows of buttons in a toolbar or dialog box). It's also useful for very simple layouts such as a navigator on the left and a main work area on the right. It can be used to create very complex layouts but it gets tricky until you fully understand the packing algorithm.

Upvotes: 2

Related Questions