我有一个带有文本和按钮的父窗口。在父窗口中点击按钮后,会打开一个子窗口,其中包含输入框和一个按钮。现在,当我在子窗口的输入框中输入一些内容并单击提交按钮时,子窗口中输入的数据应该出现在父窗口的文本框中,我该怎么做?我的代码如下。
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()`
Application
实例本身而不是Text
小部件,像这样:self.appc=Demo(self.top, self)
。如果没有'object',我会得到一个TypeError: must be type, not classobj
,但我使用的是Python 2.7。不知道这有什么区别。 - VRage