Home > Software design >  I cannot get the value of my Entry in Tkinter | Python
I cannot get the value of my Entry in Tkinter | Python

Time:11-18

So I am making this GUI app with tkinter in which we can signup and login and save our data. I am having problem with getting the value from the Entry widget in my signup part.

from tkinter import *

##### CONSTANTS ############
m = Tk()
m.title("Info storage")
m.geometry("500x500")
############################

def signup():
    sw = Toplevel()
    sw.title("Sign Up")


    signup_head = Label(sw, text="Sign Up over here!", font=font1, padx=180).grid(row=0, column=0)

    username_text = Label(sw, text="Username", font=font2, pady=40).grid(row=1, column=0)
    username = Entry(sw, width=40)
    password_text = Label(sw, text="Password", font=font2, pady=10).grid(row=3, column=0)
    password = Entry(sw, width=40)
    re_password_text = Label(sw, text="Re-type your password", font=font2, pady=10).grid(row=5, column=0)
    re_password = Entry(sw, width=40)
    useless = Label(sw, text=" ", pady=50).grid(row=7, column=0)


    username.grid(row=2, column=0)
    password.grid(row=4, column=0)
    re_password.grid(row=6, column=0)
    

    def signup_work():
        p = password.get()
        my_p = Label(sw, text=p).pack()


    go_btn = Button(sw, text="GO", width=20, command=signup_work).grid(row=8, column=0)



title = Label(m, text="Login or Signup to start storing your info!", width=50, height=3, font=font1).grid(row=0, column=0)
signup_button = Button(m, text="Sign Up", command=signup).grid(row=1, column=0)

m.mainloop()

This is the error that I am getting

Exception in Tkinter callback
Traceback (most recent call last):                                                                                        
File "F:\software\Python 3.9.7\lib\tkinter\__init__.py", line 1892, in __call__                                           
return self.func(*args)                                                                                               
File "D:\Programming\Python\Tkinter\info-storage\main.py", line 38, in signup_work                                        
my_p = Label(sw, text=p).pack()                                                                                       
File "F:\software\Python 3.9.7\lib\tkinter\__init__.py", line 2396, in pack_configure                                     
self.tk.call(                                                                                                       
_tkinter.TclError: cannot use geometry manager pack inside .!toplevel which already has slaves 
managed by grid            

I didn't even put a windows geometry function on my second window but i still get the same error.

CodePudding user response:

The first problem with your code is that you are packing/gridding the widgets in the same line that their object is initialized, and then you save them in a variable. Like so -:

my_p = Label(sw, text=p).pack()

This would not give the desired results, as what is actually being saved in variable for example here my_p is of None type, as it is the return value of the function pack and not the Label object itself. The fix to this is to pack the Label in a separate line -:

my_p = Label(sw, text=p)
my_p.pack()

Secondly, pack and grid are two tkinter geometry managers that cannot be used on the same parent's widgets. For example in your code the problem is that to the same parent sw, the Label my_p is being packed and many other widgets are being gridded. And as explained tkinter cannot handle the packing and gridding of widgets of the same parent, thus it throws the error -:

_tkinter.TclError: cannot use geometry manager pack inside .!toplevel which already has slaves managed by grid

By changing the geometry manager for the label my_p to grid instead like so -:

my_p = Label(sw, text=p)
my_p.grid(row = some_row, column = some_column)

The problem can be fixed.

  • Related