Python 3 Tkinter Frame interaction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    Python 3 Tkinter Frame interaction

    I am trying to have a form talk to another one as per exemple below. Basically, a child form alter a widget on a parent form. I cannot find a way to make this to work. What am I doing wrong or missing? The method I am trying to solve is SetParentValue.
    Code:
    from tkinter import *
    from tkinter import ttk
    
    class FirstFrame():
        def __init__(self,root):
            self.root=root
            self.frame=Frame(self.root)
            self.PlaceLeftButton()
            self.PlaceLabel()
            
        def PlaceLeftButton(self):
            self.btn=ttk.Button(self.root,text='Open Child Form',command=self.OpenChild)
            self.btn.grid(column=0,row=0)
        def PlaceLabel(self):
            self.lbl=ttk.Label(self.root, text='At Start')
            self.lbl.grid(column=1,row=0)
    
        def OpenChild(self):
            top=Toplevel(self.root)
            self.child=ChildFrame(top)
    
    class ChildFrame():
        def  __init__(self, root):
            self.root=root
            self.childframe=Frame(self.root)
            self.PlaceLeftButton()
            self.PlaceEntryField()
        def PlaceLeftButton(self):
            self.btn=Button(self.root,text='Reset parent form', command=self.SetParentValue)
            self.btn.grid(column=0,row=0)
        def PlaceEntryField(self):
            self.entry=Entry(self.root)
            self.entry.grid(column=0, row=1)
        def SetParentValue(self):
            self.root.lbl(text='Parent has been reset')
            
    if  __name__=='__main__':
        root = Tk()
        root.option_add('*font', ('verdana', 12, 'bold'))
        root.title("Class Interaction")
        display = FirstFrame(root)
        root.mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You need to pass the instance to ChildFrame() as well as the Toplevel object. Since I am in Python 2.X, I modified the code accordingly. Also, use widget method config() to set the text value.
    Code:
    from Tkinter import *
    #from tkinter import *
    #from tkinter import ttk
     
    class FirstFrame(object):
        def __init__(self,root):
            self.root=root
            self.frame=Frame(self.root)
            self.PlaceLeftButton()
            self.PlaceLabel()
     
        def PlaceLeftButton(self):
            self.btn=Button(self.root,text='Open Child Form',command=self.OpenChild)
            self.btn.grid(column=0,row=0)
        def PlaceLabel(self):
            self.lbl=Label(self.root, text='At Start')
            self.lbl.grid(column=1,row=0)
     
        def OpenChild(self):
            top=Toplevel(self.root)
            self.child=ChildFrame(top, self)
     
    class ChildFrame(object):
        def  __init__(self, root, obj):
            self.root=root
            self.obj=obj
            self.childframe=Frame(self.root)
            self.PlaceLeftButton()
            self.PlaceEntryField()
        def PlaceLeftButton(self):
            self.btn=Button(self.root,text='Reset parent form', command=self.SetParentValue)
            self.btn.grid(column=0,row=0)
        def PlaceEntryField(self):
            self.entry=Entry(self.root)
            self.entry.grid(column=0, row=1)
        def SetParentValue(self):
            self.obj.lbl.config(text='Parent has been reset')
     
    if  __name__=='__main__':
        root = Tk()
        root.option_add('*font', ('verdana', 12, 'bold'))
        root.title("Class Interaction")
        display = FirstFrame(root)
        root.mainloop()

    Comment

    • Mariostg
      Contributor
      • Sep 2010
      • 332

      #3
      Outstanding! You have no idea how much browsing I did during week-end about this problem...
      Thanks.
      Works fine on python 3.2 by the way.

      Comment

      Working...