如何在python tkinter中显示从子窗口到父窗口的值?

时间:2016-04-25 04:12:57

标签: python user-interface python-3.x tkinter

我有一个带有文本和按钮的父窗口。在父窗口中单击按钮时,将打开包含输入框和按钮的子窗口。现在当我在子窗口的输入框中输入内容并单击子窗口的提交时,在子窗口的输入框中输入的数据应该出现在父窗口的文本框中,我该怎么办?我的代码如下。

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 个答案:

答案 0 :(得分:1)

您必须通过构造函数将Text窗口小部件的引用传递给子窗口。之后,您可以完全控制Demo课程中的小部件。因此,在onSubmit方法中,只需使用insert方法:

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()

答案 1 :(得分:0)

最终,您的Demo课程需要有权访问您的t1文本小部件。

为了实现这一目标,我们需要将Application的实例传递给Demo

这段代码显示了我们如何将Application的实例传入Demo以供日后参考。

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

现在在课堂演示中,查看__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()

现在我们终于需要解决你的问题了!

  

现在当我在子窗口的输入框中输入内容并点击子窗口的提交时,在子窗口的输入框中输入的数据应该出现在父窗口的文本框中,我该怎么办?

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

这里是完整/完整的代码:

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()

希望这有帮助! 〜枪手