471,349 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Tkinter Subclass Tutorial

Hello,

I'm still learning how to call class methods from other classes. I'm trying to figure out what is the best approach to executing a Tkinter UI from another class. It tells me that it expects two aurguments but received only 1. I expect it may have something to do with "app = App(root)" from class1.py.
I also tried defining another procedure that would execute both but I'm still confused.
Thanks for the help!

Frank


For example:

Expand|Select|Wrap|Line Numbers
  1. Class1.py
  2. -----------------------------------------------
  3. import Tkinter
  4.  
  5.  
  6. class App(Frame):
  7.     def __init__(self , master):
  8.         Frame.__init__(self, master)
  9.  
  10.     def makeFrames(self):
  11.          code
  12.     def makeRest(self):
  13.          code
  14.     def gerChars(self):
  15.          code
  16.  
  17.     def createUI(self):
  18.         #creates the UI
  19.         self.makeFrames()
  20.         self.makeRest()
  21.         self.gerChars()
  22.  
  23. root = Tk()
  24. app = App(root)
  25. app.pack()
  26. app.createUI()
  27. root.mainloop()
  28.  
  29. -------------------------------------------------------
  30. Class2.py calling class1.py to execute UI
  31.  
  32.  
  33. import class1
  34.  
  35. class ImportChar:
  36.     def __init__(self):
  37.         app = App(root)
  38.  
  39.     def AppendChar(self):
  40.          code
  41.  
  42.     def RunItAll(self):
  43.          self.ApppendChar()
  44.          self.app.createUI()
  45.  
  46. importchar = ImportChar()
  47. importchar.RunItAll()
  48.  
  49.  
  50. -------------------------------------------------
  51.  
  52.  
Jun 2 '07 #1
4 5566
bartonc
6,596 Expert 4TB
I like to teach OOP (especially GUI OOP) in terms of "it" and "has a".
An application has a main frame.
An application has a main loop.

The main frame has a user interface (many widgets).
The main frame has a worker class.

In those terms, the modules would lay out like this

Class1.py
Expand|Select|Wrap|Line Numbers
  1. # Don't need much from Tkinter to make an app
  2. from Tkinter import Tk
  3.  
  4. # in the module that defines App, import the frames that belong to it
  5. import Class2
  6.  
  7. class App: # not sure if there's a good super class in Tkinter
  8.     def __init__(self, root):
  9.         self.mainFrame = Class2.MainFrame(root)
  10.         self.mainFrame.pack()
  11.  
  12.  
  13.  
  14. if __name__ == "__main__":
  15.     root = Tk()  # This is the actual Tkinter app level object
  16.     app = App(root)
  17.     root.mainloop()
  18.  
Class2.py
Expand|Select|Wrap|Line Numbers
  1. # I don't like import *, but...
  2. from Tkinter import *
  3.  
  4. # Support Classes # Even better in another module
  5.  
  6. class Worker:
  7.  
  8.     def AppendChar(self, data):
  9.         # code
  10.         print "Appending Chars", data
  11.  
  12.  
  13.  
  14. # The main frame is in charge of all object creation #
  15.  
  16. class MainFrame(Frame):
  17.     def __init__(self , master):
  18.         Frame.__init__(self, master)
  19.         self.createUI
  20.  
  21.     def createUI(self):
  22.         #creates the UI
  23.         self.makeFrames()
  24.         self.makeRest()
  25.         self.gerChars()
  26.  
  27.     def makeFrames(self):
  28.         # code
  29.         print "Making Frames"
  30.  
  31.     def makeRest(self):
  32.         self.woker = Worker()
  33.         print "Making Rest"
  34.  
  35.     # event handlers #
  36.  
  37.     def gerChars(self):
  38.         # code
  39.         print "Getting Chars"
  40.  
  41.     def OnReasonToAppendChars(self):
  42.         self.worker.AppendChars("abcd")
  43.  
Then, simply running the Class1.py module creates the whole app.
Jun 2 '07 #2
bartonc
6,596 Expert 4TB
If you really want an application object, it would look like this:
Expand|Select|Wrap|Line Numbers
  1. # Don't need much from Tkinter to make an app
  2. from Tkinter import Tk
  3.  
  4. # in the module that defines App, import the frames that belong to it
  5. import Class2
  6.  
  7. class App(Tk):
  8.     def __init__(self, **kwargs):
  9.         Tk.__init__(self, **kwargs)
  10.         self.mainFrame = Class2.MainFrame(self)
  11.         self.mainFrame.pack()
  12.  
  13.  
  14.  
  15. if __name__ == "__main__":
  16.     app = App(className='MyAppClass')
  17.     app.mainloop()
  18.  
Jun 2 '07 #3
If you really want an application object, it would look like this:
Expand|Select|Wrap|Line Numbers
  1. # Don't need much from Tkinter to make an app
  2. from Tkinter import Tk
  3.  
  4. # in the module that defines App, import the frames that belong to it
  5. import Class2
  6.  
  7. class App(Tk):
  8.     def __init__(self, **kwargs):
  9.         Tk.__init__(self, **kwargs)
  10.         self.mainFrame = Class2.MainFrame(self)
  11.         self.mainFrame.pack()
  12.  
  13.  
  14.  
  15. if __name__ == "__main__":
  16.     app = App(className='MyAppClass')
  17.     app.mainloop()
  18.  

Hello bartonc,

Thanks for the input.. This is exactly what I was looking for. :)

Regards,

=F
Jun 4 '07 #4
bartonc
6,596 Expert 4TB
Hello bartonc,

Thanks for the input.. This is exactly what I was looking for. :)

Regards,

=F
You don't know how much your word warm my heart.

Thanks for dropping back in.
Keep posting,
Barton
Jun 4 '07 #5

Post your reply

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

Similar topics

3 posts views Thread by srijit | last post: by
2 posts views Thread by Paul A. Wilson | last post: by
2 posts views Thread by Zhang Le | last post: by
2 posts views Thread by codecraig | last post: by
11 posts views Thread by William Gill | last post: by
2 posts views Thread by import newbie | last post: by
2 posts views Thread by BartlebyScrivener | last post: by
2 posts views Thread by W. Watson | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.