calling a class method from a menu in a different class

C

Chris Hare

What I am trying to do is call a class function from a menu, for example

displaySubMenu.add_radiobutton(label="Medium",
variable=radarPanelSize, command=radarWidgets.refresh)

class radarWidgets:
def __init__(self,root):
self.window = root
def refresh(self):
global radar
global refreshTimer
self.refreshTimer.cancel()
logging.debug("handlesRadarRefreshByTimer()")
#
# destroy the existing frame the radar is in
#
self.w.destroy()
self.lblLastUpdate.destroy()
self.btnRefresh.destroy()
#
# rebuild the radarWidgets
#
self.createImage()
self.refreshTimer = threading.Timer( 900, self.refresh)
self.refreshTimer.start()

This doesn't work because the instance of radarWidgets was defined in a different class ......

class mainDisplay:
def __init__(self,root):
self.window = root
def show(self):
#
# Configure the base frame
#
base=Frame(self.window,bg=backColor,width=1200, height=800)
base.grid()
frameRadar = Frame(base, bd=0, bg=backColor)
frameRadar.grid(row=2,column=1,sticky=N+E+S+W)
#
radar = radarWidgets(frameRadar)
radar.show()

so the actual instance is called "radar". If I put the instance name in the menu item, I get an error that "radar" is unknown. If I use RadarWidgets.refresh ins the command for the menu, I get the error:

TypeError: unbound method refresh() must be called with radarWidgets instance as first argument (got nothing instead)

Any ideas?

Thanks!
 
R

rantingrick

Chris,

It looks as if you are calling a class object and not an instance
object. However i cannot be for sure because you are using improper
Python style. All classes should be capwords. But here again you have
used camelcase for the class identifiers "radarWidgets" and
"mainDisplay", which is bad bad bad!!

You been asking multiple questions about this code for the last couple
of days (and thats fine). However, maybe you would do everyone a favor
by posting the entire code so we can offer suggestions. Just seeing
seeing a snipit here and a snipit there is not helping because we
don't know where else you may be screwing up that we cannot see.

It seems you're committing many faux pas with not only your coding
style but also your coding practices. I've seen overuse of globals and
bad styles and on and on. So post the entire code so myself and others
can go through this mess and get it sorted out. If you keep on with
your bad habits you're never going to become proficient with Python.

I am sorry if this post seems condescending because that is not the
case.
 
C

Chris Hare

No offense taken. I'll get getting the Google Python Style Guide today. I'll package up the code tonight and it to the group. Fortunately ( or unfortunately), it is all in one file right now.
 
C

Chris Hare

Oh and Risk,
I know I was calling the class object.

class 1 creates the instance object
class 2 tries to use the instance object

so the problem is how to make class 2 knowledgable of instance object? I guess I could pass the instance object into the class, since class1 creates the instance and also calls class 2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top