Home > Software engineering >  Python entry widget inside a function [duplicate]
Python entry widget inside a function [duplicate]

Time:09-22

Hiii. I coundn't get the texts which is being entered in the entry variable by using this method. Using get method i always get 0 as the entered value. i tried partial, lambda functions too! But still coundn't get the entry values. someone please help me to solve this out!

from tkinter import *

a = Tk()

def reg():
    global b
    b = Tk()
    global ab
    ab = IntVar()
    E = Entry(b,textvariable=ab).pack()
    b2 = Button(b, text='submit',command=sub).pack()
    

def sub():
    l = ab.get()
    Lab = Label(b, text=l).pack()


b1 = Button(a, text='reg',command=lambda:reg()).pack()

CodePudding user response:

I think the way you implemented the second window b is causing the problem. There should always be only one root window in the tkinter application. All other windows should be the toplevel widgets for the root window.

This code should work.

from tkinter import *
# Root window
a = Tk() # All other windows should be the toplevel widgets for this root window

def reg():
    global b
    b = Toplevel(a)
    global ab
    ab = IntVar()
    E = Entry(b,textvariable=ab).pack()
    b2 = Button(b, text='submit',command=lambda:sub()).pack()
    

def sub():
    l = ab.get()
    Lab = Label(b, text=l).pack()


b1 = Button(a, text='reg',command=lambda:reg()).pack()

a.mainloop()

Watch this video. It has a better implementation and explanation for multiple window/frame gui with tkinter.

  • Related