4

I have a parent window with a Text and button. On clicking button in parent window child window opens which contains entry box and a button. Now when I enter something in entry box of child window and click submit of child window the data entered in entry box of child window should appear in text box of parent window, how can I do this? My code is as below.

from tkinter import *


class Application(Frame):

    def __init__(self,  master):
        super(Application,self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.t1=Text(self,width=10,height=2)
        self.t1.grid(row=1,column=1)
        self.b1=Button(self,text="create",command=self.onClick)
        self.b1.grid(row=2,column=1)

    def onClick(self):
        self.top = Toplevel()
        self.top.title("title")
        self.top.geometry("300x150+30+30")
        self.top.transient(self)
        self.appc=Demo(self.top)

class Demo:

    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)
        self.widget()

    def widget(self):
        self.e1=Entry(self.master)
        self.e1.grid(row=1,column=1)
        self.b1=Button(self.master,text="submit",command=self.onSubmit)
        self.b1.grid(row=2,column=1)

    def onSubmit(self):
        self.value=self.e1.get()
        print(self.value)
    root=Tk()
    app=Application(root)
    app.mainloop()`

2 Answers 2

1

You have to pass a reference of your Text widget to your child window via constructor. After that you have full control of the widget in your Demo class. So in your onSubmit method just use the insert method:

from tkinter import *
class Application(Frame, object):
    def __init__(self,  master):
        super(Application, self).\
            __init__(master)
        self.grid()
        self.create_widgets()


    def create_widgets(self):
        self.t1=Text(self,width=10,height=2)
        self.t1.grid(row=1,column=1)
        self.b1=Button(self,text="create",command=self.onClick)
        self.b1.grid(row=2,column=1)

    def onClick(self):
        self.top = Toplevel()
        self.top.title("title")
        self.top.geometry("300x150+30+30")
        self.top.transient(self)
        self.appc=Demo(self.top, self.t1)

class Demo(object):
    def __init__(self, master, t1):
        self.master = master
        self.frame = Frame(self.master)
        self.t1 = t1
        self.widget()

    def widget(self):
        self.e1=Entry(self.master)
        self.e1.grid(row=1,column=1)
        self.b1=Button(self.master,text="submit",command=self.onSubmit)
        self.b1.grid(row=2,column=1)

    def onSubmit(self):
        self.t1.insert(INSERT, self.e1.get())


root=Tk()
app=Application(root)
app.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

He can pass the Application instance itself instead of the Text widget, like this: self.appc=Demo(self.top, self). Withouth the 'object' i get an TypeError: must be type, not classobj, but i use Python 2.7. Don't know what difference it makes.
1

Ultimately, your Demo class needs to have access to your t1 Text widget.

In order to accomplish this we need to pass our instance of Application into Demo.


This bit of code shows how we pass in our instance of Application into Demo for reference later on.

    #Create Demo object with VVVVV
    self.appc=Demo(self.top, self)

Now in class Demo, look at the changes to our __init__ :

#notice how we are including VVV something new? This is our instance of Application
def __init__(self, master, parent):
    self.master = master
    self.frame = Frame(self.master)

    #Dont forget to reassign it! VVVV
    self.parent = parent
    self.widget()

Now we finally need to solve your problem!

Now when i enter something in entry box of child window and click submit of child window the data entered in entry box of child window should appear in text box of parent window how can i do this?

def onSubmit(self):

    #within our Application instance, look at the t1 widget, 
    #run the insert function with our new input!
    self.parent.t1.insert(INSERT, self.e1.get())

Problem Solved


Here's the full/complete code:

from tkinter import *
class Application(Frame):
def __init__(self,  master):
    super(Application, self).\
        __init__(master)
    self.grid()
    self.create_widgets()


def create_widgets(self):
    self.t1=Text(self,width=10,height=2)
    self.t1.grid(row=1,column=1)
    self.b1=Button(self,text="create",command=self.onClick)
    self.b1.grid(row=2,column=1)

def onClick(self):
    self.top = Toplevel()
    self.top.title("title")
    self.top.geometry("300x150+30+30")
    self.top.transient(self)
    self.appc=Demo(self.top, self)

class Demo():
def __init__(self, master, parent):
    self.master = master
    self.frame = Frame(self.master)
    self.parent = parent
    self.widget()

def widget(self):
    self.e1=Entry(self.master)
    self.e1.grid(row=1,column=1)
    self.b1=Button(self.master,text="submit",command=self.onSubmit)
    self.b1.grid(row=2,column=1)

def onSubmit(self):
    self.parent.t1.insert(INSERT, self.e1.get())


root=Tk()
app=Application(root)
app.mainloop()

Hope this helps! ~Gunner

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.