By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Python 3 Tkinter Frame interaction

100+
P: 332
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.
Expand|Select|Wrap|Line Numbers
  1. from tkinter import *
  2. from tkinter import ttk
  3.  
  4. class FirstFrame():
  5.     def __init__(self,root):
  6.         self.root=root
  7.         self.frame=Frame(self.root)
  8.         self.PlaceLeftButton()
  9.         self.PlaceLabel()
  10.  
  11.     def PlaceLeftButton(self):
  12.         self.btn=ttk.Button(self.root,text='Open Child Form',command=self.OpenChild)
  13.         self.btn.grid(column=0,row=0)
  14.     def PlaceLabel(self):
  15.         self.lbl=ttk.Label(self.root, text='At Start')
  16.         self.lbl.grid(column=1,row=0)
  17.  
  18.     def OpenChild(self):
  19.         top=Toplevel(self.root)
  20.         self.child=ChildFrame(top)
  21.  
  22. class ChildFrame():
  23.     def  __init__(self, root):
  24.         self.root=root
  25.         self.childframe=Frame(self.root)
  26.         self.PlaceLeftButton()
  27.         self.PlaceEntryField()
  28.     def PlaceLeftButton(self):
  29.         self.btn=Button(self.root,text='Reset parent form', command=self.SetParentValue)
  30.         self.btn.grid(column=0,row=0)
  31.     def PlaceEntryField(self):
  32.         self.entry=Entry(self.root)
  33.         self.entry.grid(column=0, row=1)
  34.     def SetParentValue(self):
  35.         self.root.lbl(text='Parent has been reset')
  36.  
  37. if  __name__=='__main__':
  38.     root = Tk()
  39.     root.option_add('*font', ('verdana', 12, 'bold'))
  40.     root.title("Class Interaction")
  41.     display = FirstFrame(root)
  42.     root.mainloop()
  43.  
Mar 14 '11 #1

✓ answered by bvdet

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.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. #from tkinter import *
  3. #from tkinter import ttk
  4.  
  5. class FirstFrame(object):
  6.     def __init__(self,root):
  7.         self.root=root
  8.         self.frame=Frame(self.root)
  9.         self.PlaceLeftButton()
  10.         self.PlaceLabel()
  11.  
  12.     def PlaceLeftButton(self):
  13.         self.btn=Button(self.root,text='Open Child Form',command=self.OpenChild)
  14.         self.btn.grid(column=0,row=0)
  15.     def PlaceLabel(self):
  16.         self.lbl=Label(self.root, text='At Start')
  17.         self.lbl.grid(column=1,row=0)
  18.  
  19.     def OpenChild(self):
  20.         top=Toplevel(self.root)
  21.         self.child=ChildFrame(top, self)
  22.  
  23. class ChildFrame(object):
  24.     def  __init__(self, root, obj):
  25.         self.root=root
  26.         self.obj=obj
  27.         self.childframe=Frame(self.root)
  28.         self.PlaceLeftButton()
  29.         self.PlaceEntryField()
  30.     def PlaceLeftButton(self):
  31.         self.btn=Button(self.root,text='Reset parent form', command=self.SetParentValue)
  32.         self.btn.grid(column=0,row=0)
  33.     def PlaceEntryField(self):
  34.         self.entry=Entry(self.root)
  35.         self.entry.grid(column=0, row=1)
  36.     def SetParentValue(self):
  37.         self.obj.lbl.config(text='Parent has been reset')
  38.  
  39. if  __name__=='__main__':
  40.     root = Tk()
  41.     root.option_add('*font', ('verdana', 12, 'bold'))
  42.     root.title("Class Interaction")
  43.     display = FirstFrame(root)
  44.     root.mainloop()

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. #from tkinter import *
  3. #from tkinter import ttk
  4.  
  5. class FirstFrame(object):
  6.     def __init__(self,root):
  7.         self.root=root
  8.         self.frame=Frame(self.root)
  9.         self.PlaceLeftButton()
  10.         self.PlaceLabel()
  11.  
  12.     def PlaceLeftButton(self):
  13.         self.btn=Button(self.root,text='Open Child Form',command=self.OpenChild)
  14.         self.btn.grid(column=0,row=0)
  15.     def PlaceLabel(self):
  16.         self.lbl=Label(self.root, text='At Start')
  17.         self.lbl.grid(column=1,row=0)
  18.  
  19.     def OpenChild(self):
  20.         top=Toplevel(self.root)
  21.         self.child=ChildFrame(top, self)
  22.  
  23. class ChildFrame(object):
  24.     def  __init__(self, root, obj):
  25.         self.root=root
  26.         self.obj=obj
  27.         self.childframe=Frame(self.root)
  28.         self.PlaceLeftButton()
  29.         self.PlaceEntryField()
  30.     def PlaceLeftButton(self):
  31.         self.btn=Button(self.root,text='Reset parent form', command=self.SetParentValue)
  32.         self.btn.grid(column=0,row=0)
  33.     def PlaceEntryField(self):
  34.         self.entry=Entry(self.root)
  35.         self.entry.grid(column=0, row=1)
  36.     def SetParentValue(self):
  37.         self.obj.lbl.config(text='Parent has been reset')
  38.  
  39. if  __name__=='__main__':
  40.     root = Tk()
  41.     root.option_add('*font', ('verdana', 12, 'bold'))
  42.     root.title("Class Interaction")
  43.     display = FirstFrame(root)
  44.     root.mainloop()
Mar 14 '11 #2

100+
P: 332
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.
Mar 14 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.