Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Tkinter父子数据交换_Python 2.7_Tkinter - Fatal编程技术网

Python 2.7 Tkinter父子数据交换

Python 2.7 Tkinter父子数据交换,python-2.7,tkinter,Python 2.7,Tkinter,我希望这不是一个重复的问题,因为已经提出了一个解决方案,我很难让它起作用。(或者,我可以在同一讨论中发布此内容) 目标: from Tkinter import * class trackApp(Frame): def __init__(self, master): Frame.__init__(self, master) self.mainVar1 = IntVar() self.mainVar2 = IntVar()

我希望这不是一个重复的问题,因为已经提出了一个解决方案,我很难让它起作用。(或者,我可以在同一讨论中发布此内容)

目标:

from Tkinter import *

class trackApp(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)

        self.mainVar1 = IntVar()
        self.mainVar2 = IntVar()

        self.mainVar1.set(100)
        self.mainVar2.set(100)
        self.master = master
        self.master.geometry('400x200+100+200')
        self.master.title('MAIN WINDOW')

        self.label1=Label(self.master,text='Variable1 : ').grid(row=1,column=1)
        self.mainEntry1=Entry(self.master,textvariable=self.mainVar1).grid(row=1,column=2)

        self.label2=Label(self.master,text='Variable2 : ').grid(row=2,column=1)
        self.mainEntry2=Entry(self.master,textvariable=self.mainVar2).grid(row=2,column=2)

        self.button1=Button(self.master,text='Child',command=self.dialogWindow).grid(row=7,column=2)


    def new_data(self, data):
        self.mainEntry1.delete(0,END)
        self.mainEntry1.insert(0,self.data['var1'])
        self.mainEntry2.delete(0,END)
        self.mainEntry2.insert(0,self.data['var2'])


    def dialogWindow(self):
        # Build a list from control variables used in the main window text entry boxes
        mainList = [self.mainVar1.get(),self.mainVar2.get()]

        top=Toplevel(self.master)
        childDialog=childWindow(top,mainList, self.master)


class childWindow(Frame):

    # Pass data (list) to the child
    def __init__(self, master, list, app):

        self.list = list

        self.app = app
        self.master = master
        self.master.geometry('300x100+150+250')
        self.master.title('CHILD WINDOW')

        # Define control variabels to be used with child window text entry widgets
        self.childvar1 = IntVar()
        self.childvar2 = IntVar()

        # Fill child window text entry widgets with inf. from parent window
        self.childvar1.set(list[0])
        self.childvar2.set(list[1])

        # Text entry widgets
        self.label1=Label(self.master,text='Enter New value 1').grid(row=0,column=1)
        self.childEntry1=Entry(self.master,textvariable=self.childvar1).grid(row=0,column=2)

        self.label2=Label(self.master,text='Enter New value 2').grid(row=1,column=1)
        self.childEntry2=Entry(self.master,textvariable=self.childvar2).grid(row=1,column=2)

        self.button1=Button(self.master,text='OK',command=self.childDestroy).grid(row=3,column=1)


    def childDestroy(self):

        self.data = {}
        self.data['var1'] = self.childvar1.get()
        self.data['var2'] = self.childvar2.get()

        trackApp.app.new_data(self, self.data)  # <<<<<<<<< How to call the parent new_data method

        self.master.destroy()


def main():
    root = Tk()
    app = trackApp(root)
    root.mainloop()

if __name__ == "__main__":
    main()
/usr/bin/python2.7 /<path>/child-dialog.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/<path>/child-dialog.py", line 77, in childDestroy
    trackApp.app.new_data(self, self.data)  # <<<<<<<<< How to call the parent new_data method
AttributeError: class trackApp has no attribute 'app'
在主窗口中,用户输入一些数据,如果单击,将出现一个新的子窗口,其中包含父数据,最终对其进行操作,然后将结果发送回父窗口小部件

使用的代码:

from Tkinter import *

class trackApp(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)

        self.mainVar1 = IntVar()
        self.mainVar2 = IntVar()

        self.mainVar1.set(100)
        self.mainVar2.set(100)
        self.master = master
        self.master.geometry('400x200+100+200')
        self.master.title('MAIN WINDOW')

        self.label1=Label(self.master,text='Variable1 : ').grid(row=1,column=1)
        self.mainEntry1=Entry(self.master,textvariable=self.mainVar1).grid(row=1,column=2)

        self.label2=Label(self.master,text='Variable2 : ').grid(row=2,column=1)
        self.mainEntry2=Entry(self.master,textvariable=self.mainVar2).grid(row=2,column=2)

        self.button1=Button(self.master,text='Child',command=self.dialogWindow).grid(row=7,column=2)


    def new_data(self, data):
        self.mainEntry1.delete(0,END)
        self.mainEntry1.insert(0,self.data['var1'])
        self.mainEntry2.delete(0,END)
        self.mainEntry2.insert(0,self.data['var2'])


    def dialogWindow(self):
        # Build a list from control variables used in the main window text entry boxes
        mainList = [self.mainVar1.get(),self.mainVar2.get()]

        top=Toplevel(self.master)
        childDialog=childWindow(top,mainList, self.master)


class childWindow(Frame):

    # Pass data (list) to the child
    def __init__(self, master, list, app):

        self.list = list

        self.app = app
        self.master = master
        self.master.geometry('300x100+150+250')
        self.master.title('CHILD WINDOW')

        # Define control variabels to be used with child window text entry widgets
        self.childvar1 = IntVar()
        self.childvar2 = IntVar()

        # Fill child window text entry widgets with inf. from parent window
        self.childvar1.set(list[0])
        self.childvar2.set(list[1])

        # Text entry widgets
        self.label1=Label(self.master,text='Enter New value 1').grid(row=0,column=1)
        self.childEntry1=Entry(self.master,textvariable=self.childvar1).grid(row=0,column=2)

        self.label2=Label(self.master,text='Enter New value 2').grid(row=1,column=1)
        self.childEntry2=Entry(self.master,textvariable=self.childvar2).grid(row=1,column=2)

        self.button1=Button(self.master,text='OK',command=self.childDestroy).grid(row=3,column=1)


    def childDestroy(self):

        self.data = {}
        self.data['var1'] = self.childvar1.get()
        self.data['var2'] = self.childvar2.get()

        trackApp.app.new_data(self, self.data)  # <<<<<<<<< How to call the parent new_data method

        self.master.destroy()


def main():
    root = Tk()
    app = trackApp(root)
    root.mainloop()

if __name__ == "__main__":
    main()
/usr/bin/python2.7 /<path>/child-dialog.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/<path>/child-dialog.py", line 77, in childDestroy
    trackApp.app.new_data(self, self.data)  # <<<<<<<<< How to call the parent new_data method
AttributeError: class trackApp has no attribute 'app'
从Tkinter导入*
类trackApp(框架):
定义初始(自我,主):
帧。\uuuu初始化(自,主)
self.mainVar1=IntVar()
self.mainVar2=IntVar()
自维护1.套(100)
自维护2.套(100)
self.master=master
自主几何(“400x200+100+200”)
self.master.title('主窗口')
self.label1=Label(self.master,text='Variable1:').grid(行=1,列=1)
self.mainEntry1=条目(self.master,textvariable=self.mainVari1).grid(行=1,列=2)
self.label2=Label(self.master,text='Variable2:').grid(行=2,列=1)
self.maintry2=条目(self.master,textvariable=self.mainVar2).grid(行=2,列=2)
self.button1=按钮(self.master,text='Child',command=self.dialogWindow).grid(行=7,列=2)
def新_数据(自身、数据):
self.maintry1.delete(0,结束)
self.maintry1.insert(0,self.data['var1'])
self.maintry2.delete(0,结束)
self.maintry2.insert(0,self.data['var2'])
def对话框窗口(自身):
#从主窗口文本输入框中使用的控制变量生成列表
mainList=[self.mainVar1.get(),self.mainVar2.get()]
顶层=顶层(self.master)
childDialog=childWindow(顶部、主列表、self.master)
类子窗口(框架):
#将数据(列表)传递给子级
定义初始化(自、主、列表、应用):
self.list=list
self.app=app
self.master=master
自主几何(“300x100+150+250”)
self.master.title('子窗口')
#定义要与子窗口文本输入小部件一起使用的控件变量
self.childvar1=IntVar()
self.childvar2=IntVar()
#用父窗口的inf.填充子窗口文本输入小部件
self.childvar1.set(列表[0])
self.childvar2.set(列表[1])
#文本输入小部件
self.label1=Label(self.master,text='Enter New value 1').grid(行=0,列=1)
self.childEntry1=Entry(self.master,textvariable=self.childvar1).grid(行=0,列=2)
self.label2=Label(self.master,text='Enter New value 2').grid(行=1,列=1)
self.childEntry2=Entry(self.master,textvariable=self.childvar2).grid(行=1,列=2)
self.button1=按钮(self.master,text='OK',command=self.childDestroy).grid(行=3,列=1)
def(自我):
self.data={}
self.data['var1']=self.childvar1.get()
self.data['var2']=self.childvar2.get()

trackApp.app.new_data(self,self.data)#错误消息准确地告诉您问题所在:它说
trackApp
没有属性
'app'
,查看该类的定义,我没有看到属性
'app'

为了让您的子窗口知道它是父窗口,您需要给它一个对父窗口的引用。因此,在创建窗口时,传递引用,并在子窗口中使用引用

看起来您正在尝试这样做,但是您传入了错误的值,然后您没有使用该值。您需要修改代码,使其如下所示:

class trackApp(Frame):
    ...
def dialogWindow(self):
    ...
    childDialog=childWindow(top,mainList, self)

class childWindow(Frame):
    def childDestroy(self):
        ...
        self.app.new_data(self, self.data) 
        ... 

谢谢布莱恩的回答。以下是我所做的相应修改:

将父窗口(自身)传递给子窗口

def dialogWindow(self):
    childDialog=childWindow(top,mainList, self)
使用父实例(self)调用父方法

以及以下内容来更新主窗口小部件:

def new_data(self, data):
    self.mainVar1.set(data['var1'])
    self.mainVar2.set(data['var2'])
谢谢你的帮助