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):
#开发者_Go百科 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?
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()
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.
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/
精彩评论