1

Yea, I know even the Title of this question is confusing. Sorry but that is the best I can describe what I am trying to accomplish.

So.... I am still learning to work with Python classes. Here is one that I need some help with. Basically I have a single Class that I create a stopwatch with. Then I have a bunch of functions outside of that class that do other tasks and I have my Main function. I need to be able to start and stop my stopwath class instance from one of the non-class functions. Here a slimmed down version so you can see my structure.

class StopWatch(Frame):
     ...
         def Start(self):
              ...              
         def Start(self):
              ...
def dosomething():
             # right here I want to call the sw1.Start function in StopWatch.  
def dosomethingelse();
             # right here I want to call the sw1.Stop function in StopWatch.
def main():
          sw1 = StopWatch(root)

I have tried to do something like this but it doesn't work.

class StopWatch(Frame):
     ...
         def Start(self):
              ...              
         def Start(self):
              ...
def dosomething():
             # right here I want to call the sw1.Start function in StopWatch.  
             tmp = StopWatch
             tmp.Start()   
def dosomethingelse();
             # right here I want to call the sw1.Stop function in StopWatch.
             tmp = StopWatch
             tmp.Start() 
def main():
          sw1 = StopWatch(root)

I am sure this doesn't work because I it is not the sw1 instance of the StopWatch class.

So how can I call the Start and Stop functions of the StopWatch class for instance sw1?

Husar
  • 413
  • 1
  • 6
  • 17

1 Answers1

3

It's pretty clear you don't yet have a handle on Python functions yet or the concept of Scope. So I'll walk you through it, but please read some more tutorials too!

You are defining the variable sw1, an instance of the StopWatch class, in the scope of main(). It won't be accessable anywhere outside of main(). Therefore, you need to pass the object between methods. That's done my making your dosomething() and dosomethingelse() methods take arguments, like so:

 def doSomething(stopwatch):
     # whatever comes first
     stopwatch.start()


 def doSomethingElse(stopwatch):
     # whatever comes first
     stopwatch.stop()

And then you simply call those methods and pass them sw1, like so:

def main():
    sw1 = StopWatch(root)
    doSomething(sw1)
    doSomethingElse(sw1)
Community
  • 1
  • 1
aruisdante
  • 8,875
  • 2
  • 30
  • 37
  • Thanks. I will look up more info on scope because this what is messing me up. Would having doSomething(sw1) inside of main actually execute the doSomething function instance of sw1? I don't want that to happen. It would need to happen as part of the flow in the doSomething. – Husar Feb 18 '15 at 22:19
  • Right now the doSomething function is called by a button. – Husar Feb 18 '15 at 22:26
  • If that's the case, then you should encapsulate `doSomething()` etc. inside another class, and make `sw1` a class member variable. It's kind of a bit outside the scope of this question though. You might want to ask a new question with the entire context of your problem to get a better answer. – aruisdante Feb 18 '15 at 22:42
  • Having said that, you can always do `dsl = lambda : doSomething(sw1)`, and pass `dsl` as the callback binding for your button. But that's getting probably a lot more advanced then you want, and the class solution is better, the `labda` solution should only be used as a last resort. – aruisdante Feb 18 '15 at 22:44
  • Part of me wants to put it all in one class. I am never going to create another instance of the StopWatch. – Husar Feb 19 '15 at 02:49
  • Is this widget just a display for the stopwatch? then absolutely it would be appropriate to put it in a single class that defines a stopwatch widget. – aruisdante Feb 19 '15 at 02:52
  • The StopWatch class just creates everything on the screen and runs it. It make the labels for the stopwatch, has a start, stop, reset, and exit functions. The main has my buttons that make the call to the StopWatch class. You can see the stopwatch code on this post prior to me adding my functions outside the class to do other tasks. Here is the link.... (http://stackoverflow.com/questions/28567530/python-stopwatch-example-starting-all-class-instances-at-the-same-time/28569304#28569304) – Husar Feb 19 '15 at 03:02