简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM