473,225 Members | 1,220 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,225 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 5895
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.