如何在Python Tkinter中将子窗口的值显示到父窗口?

4

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

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个回答

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

他可以传递Application实例本身而不是Text小部件,像这样:self.appc=Demo(self.top, self)。如果没有'object',我会得到一个TypeError: must be type, not classobj,但我使用的是Python 2.7。不知道这有什么区别。 - VRage

1

最终,您的Demo类需要访问您的t1文本小部件。

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


这段代码展示了我们如何将Application实例传递给Demo,以便稍后进行引用。
    #Create Demo object with VVVVV
    self.appc=Demo(self.top, self)

现在在Demo类中,看看我们__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()

希望这有所帮助!~Gunner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接