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: -
Class1.py
-
-----------------------------------------------
-
import Tkinter
-
-
-
class App(Frame):
-
def __init__(self , master):
-
Frame.__init__(self, master)
-
-
def makeFrames(self):
-
code
-
def makeRest(self):
-
code
-
def gerChars(self):
-
code
-
-
def createUI(self):
-
#creates the UI
-
self.makeFrames()
-
self.makeRest()
-
self.gerChars()
-
-
root = Tk()
-
app = App(root)
-
app.pack()
-
app.createUI()
-
root.mainloop()
-
-
-------------------------------------------------------
-
Class2.py calling class1.py to execute UI
-
-
-
import class1
-
-
class ImportChar:
-
def __init__(self):
-
app = App(root)
-
-
def AppendChar(self):
-
code
-
-
def RunItAll(self):
-
self.ApppendChar()
-
self.app.createUI()
-
-
importchar = ImportChar()
-
importchar.RunItAll()
-
-
-
-------------------------------------------------
-
-
4 5914
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 - # Don't need much from Tkinter to make an app
-
from Tkinter import Tk
-
-
# in the module that defines App, import the frames that belong to it
-
import Class2
-
-
class App: # not sure if there's a good super class in Tkinter
-
def __init__(self, root):
-
self.mainFrame = Class2.MainFrame(root)
-
self.mainFrame.pack()
-
-
-
-
if __name__ == "__main__":
-
root = Tk() # This is the actual Tkinter app level object
-
app = App(root)
-
root.mainloop()
-
Class2.py - # I don't like import *, but...
-
from Tkinter import *
-
-
# Support Classes # Even better in another module
-
-
class Worker:
-
-
def AppendChar(self, data):
-
# code
-
print "Appending Chars", data
-
-
-
-
# The main frame is in charge of all object creation #
-
-
class MainFrame(Frame):
-
def __init__(self , master):
-
Frame.__init__(self, master)
-
self.createUI
-
-
def createUI(self):
-
#creates the UI
-
self.makeFrames()
-
self.makeRest()
-
self.gerChars()
-
-
def makeFrames(self):
-
# code
-
print "Making Frames"
-
-
def makeRest(self):
-
self.woker = Worker()
-
print "Making Rest"
-
-
# event handlers #
-
-
def gerChars(self):
-
# code
-
print "Getting Chars"
-
-
def OnReasonToAppendChars(self):
-
self.worker.AppendChars("abcd")
-
Then, simply running the Class1.py module creates the whole app.
If you really want an application object, it would look like this: - # Don't need much from Tkinter to make an app
-
from Tkinter import Tk
-
-
# in the module that defines App, import the frames that belong to it
-
import Class2
-
-
class App(Tk):
-
def __init__(self, **kwargs):
-
Tk.__init__(self, **kwargs)
-
self.mainFrame = Class2.MainFrame(self)
-
self.mainFrame.pack()
-
-
-
-
if __name__ == "__main__":
-
app = App(className='MyAppClass')
-
app.mainloop()
-
If you really want an application object, it would look like this: - # Don't need much from Tkinter to make an app
-
from Tkinter import Tk
-
-
# in the module that defines App, import the frames that belong to it
-
import Class2
-
-
class App(Tk):
-
def __init__(self, **kwargs):
-
Tk.__init__(self, **kwargs)
-
self.mainFrame = Class2.MainFrame(self)
-
self.mainFrame.pack()
-
-
-
-
if __name__ == "__main__":
-
app = App(className='MyAppClass')
-
app.mainloop()
-
Hello bartonc,
Thanks for the input.. This is exactly what I was looking for. :)
Regards,
=F
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |