2

In my game I have two modules, island.py which loads the islands into my game and the second module is gui.py which handles the gui widgets before game starting. My problem is how to send the progress values from the island.py module to the progress bar created in gui.py module EDIT: also with an instance of the loading screen to access the progress bar in it and change its value.

In the module island.py

def __iter__(self):
        total = float(len(self.ground_map))
        import game.gui
        for i in self.get_coordinates():
            yield i
            global count
            count+=1
            progress = (count/total) * 100 
            game.gui.Gui.set_progress(progress)
        global count
        count = 0

In the module gui.py

def show_loading_screen(self):
    self._switch_current_widget('loadingscreen', center=True, show=True) # Creates the loading screen and its associated widgets, except the progress bar.

@staticmethod
def set_progress(progress):
    # Now I have the progress values, and it will be updated automatically... how can I pass it to the progress bar widget?
    # I need to create the progress bar widget here, but to do that I need to have the self instance to give me the current screen that I will create the progress bar for **AND HERE IS THE PROBLEM!!**
    def update_loading_screen(progress):
        """updates the widget."""
        **self,current**.findChild(name="progressBar")._set_progress(progress)
    update_loading_screen(progress)

How I can make this update_loading_screen function?

1
  • You should really keep all of your import statements at the top, unless you're dynamically importing modules. Commented Apr 18, 2011 at 13:04

3 Answers 3

4
+50

Expanding on rocksport's answer...this is how I did it

class GUI:
    def __init__(self):
        GUI.self = self


    @staticmethod
    def set_progressbar():
        print "set progress bar"
        print GUI.self


g = GUI()
g.set_progressbar()
Sign up to request clarification or add additional context in comments.

Comments

3

I would attack this a bit differently. I would go get pyDispatcher, with this you can define what qt calls "slots and signals" or you might know as just "signals", not the os kind of SIGNAL. These signals, when 'emitted' or execute a series or set of functions, you have attached to the signal. The slots are functions that are executed, the dispatcher keeps a dictionary of weak references to the slots and calls them with the arguments you emit with your signal.

See the examples for pydispatch to see how it all goes together.

but you would do something like: dispatcher.connect(reciever, signal, sender) or connect(game.gui.Gui.set_progress, 'update_progress', island.Class) then in __iter__ you would send a signal like send('update_progress', sender=island.Class, progress=progress) this will call update_progress with the kwargs progress=progress. This way you can change update progress from being a static method and update the gui directly.

3 Comments

But I think this don't solve my problem, which is to send an instance of the loading screen to the update_loading_screen function in the gui module to let me access the progress bar and change its value
@Menopia It's a completely different way to attack the problem than what you have now. The problem being updating the progress bar in the gui, while your "back end" does the work. That's why I started the message with "I would attack this problem differently."
I can't use pyDispatcher in the game and my engine doesn't provide anyway for event handling right now!! :(
1

If I understand you right you are calling a static method and thus you have no access to self. As I suppose that you have only one instance of your GUI class you can set

 GUI.self = self

in GUI.__init__

The static method then can access GUI.self.

For further reading look at http://en.wikipedia.org/wiki/Singleton_pattern and http://code.activestate.com/recipes/52558-the-singleton-pattern-implemented-with-python/

3 Comments

Yes man it's REALLY what I want, but when I try to put Gui.self = self line in the _ init _ function in my gui module it gives me the following error NameError: name 'self' is not defined
I am sorry it is not giving me that error, the set_progress function gives me TypeError: set_progress() takes exactly 2 arguments (1 given) and sure that means it didn't accept self.
How do your methods signature / argumennames of _init_ and set_progress() look like ?

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.