Home > Net >  why does tkinter Entry widget returns empty sting while using multiple windows but seems to work in
why does tkinter Entry widget returns empty sting while using multiple windows but seems to work in

Time:11-21

I am building a project in python using tkinter but when using multiple windows in tkinter, I have encountered a problem, In my Code Which you can see below, In my first window ,by pressing Open Entry Box button, I can open my second window from there i want to take value using Entry box to be store in local database but whenever I try to do so each time it returns an empty string. but it works in a single window though From This code I have given a Brief idea what i want to achieve

from tkinter import *
import pymysql
class Sample():
    def __init__(self,root):
        self.root = root
        self.root.geometry("200x200")
        S_Button = Button(root,text="Open entry Box",command=self.add).pack()
        

    def add(self):
        self.second = Tk()
        self.second.geometry("500x200")

        self.SRN = StringVar()
        S_entry = Entry(self.second,textvariable=self.SRN).pack()

        S_Button2 = Button(self.second,text="Store in Database",command=self.data).pack()

        self.second.mainloop()


    def data(self):
        print(self.SRN.get())
        #con = pymysql.connect(host="localhost",user="root",password="",database="stm")
        # cur = con.cursor()
        # cur.execute("insert into StudentsData value (%s),(self.SRN.get()))
        # con.commit()
        # con.close()'''

CodePudding user response:

Your issue is you are using two instances of Tk(). Doing so means you have two seperate instances of Tk() running that essentially can't interact with each other (a great explanation of this can be found here). Hence why you aren't able to store the text from the Entry box in StringVar and it only returns an empty string. Instead, use Toplevel(). It does what you are wanting to do while only using one Tk(). Working example using Toplevel:

import tkinter as tk

class Sample():
    def __init__(self,root):
        self.root = root
        self.root.geometry("200x200")
        S_Button = tk.Button(root,text="Open entry Box",command=self.add)
        S_Button.pack()
        

    def add(self):
        self.second = tk.Toplevel(self.root)
        self.second.geometry("500x200")

        # Widgets, StringVar
        self.SRN = tk.StringVar()
        S_entry = tk.Entry(self.second,textvariable=self.SRN)
        S_Button2 = tk.Button(self.second,text="Store in Database",command=self.data)

        # Packing widgets
        S_entry.pack()
        S_Button2.pack()



    def data(self):
        self.second.destroy()
        print(self.SRN.get())


if __name__ == "__main__":
    root = tk.Tk()
    app = Sample(root)
    root.mainloop()

Don't use from tkinter import *, it doesn't follow PEP8. Instead use import tkinter as tk or similar. Don't use button = tk.Button().pack(). Seperate the button creation from the packing, this makes it much easier to order your widgets properly when there are more of them.

  • Related