473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 5914
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

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
2
by: Zhang Le | last post by:
Hello, Is there a quick way to replace the content of a single item in tkinter's listbox? Currently my solution is to first delete the item, then insert a new item at the same position. I think...
2
by: codecraig | last post by:
Hi, I was reading through the Tkinter tutorial at http://www.pythonware.com/library/tkinter/introduction/index.htm ...and it mentions that by doing, from Tkinter import * you have access to...
11
by: William Gill | last post by:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep references to them in a 2 dimensional list ( rBtns ). It works fine, and I can even make it so only one button per column can be...
2
by: import newbie | last post by:
Hi all, I'm a programming dabbler trying learn Python, and I've got a few questions. Mainly: Where can I find a good open-source library or tutorial (preferably free) that explains how to...
2
by: BartlebyScrivener | last post by:
Finally started trying to build a simple gui form for inserting text data into a mysql db of quotations. I found this nice Tkinter tutorial, http://www.ibiblio.org/obp/py4fun/gui/tkPhone.html...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.