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?

2
  • Make sure they have access to sw1. Commented Feb 18, 2015 at 21:40
  • 1
    Well, that is pretty much what I am trying to figure out. Is there an easy way to test this? Commented Feb 18, 2015 at 21:42

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

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.
Right now the doSomething function is called by a button.
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.
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.
Part of me wants to put it all in one class. I am never going to create another instance of the StopWatch.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.