在python中向进度条发送进度值

2024-09-29 01:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的游戏中,我有两个模块,岛屿.py将岛屿加载到我的游戏中,第二个模块是图形用户界面.py在游戏开始前处理gui小部件。我的问题是如何从岛屿.py在图形用户界面.py模块编辑:还可以使用加载屏幕的实例访问其中的进度条并更改其值。

在模块中岛屿.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

在模块中图形用户界面.py

^{pr2}$

如何使这个更新加载屏幕功能?在


Tags: 模块pyselfgame游戏编辑屏幕部件
3条回答

我会用一点不同的方式来处理这个问题。调度器可能不知道什么叫“Pyos”,或者“你不知道”什么信号。这些信号,当“发出”或执行一系列或一组函数时,您已经附加到该信号上。slot是执行的函数,dispatcher保存了对slot的弱引用字典,并使用信号发出的参数调用它们。在

请参阅examples for pydispatch以了解它们是如何组合在一起的。在

但是你可以做一些类似的事情:dispatcher.connect(reciever, signal, sender)或者connect(game.gui.Gui.set_progress, 'update_progress', island.Class),然后在__iter__中发送一个类似send('update_progress', sender=island.Class, progress=progress)的信号,这将使用kwargs来调用update_progress progress=progress。通过这种方式,您可以将更新进度从静态方法更改为直接更新gui。在

扩展rocksport的答案…我就是这样做的

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


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


g = GUI()
g.set_progressbar()

如果我对你的理解是正确的,你正在调用一个静态方法,因此你无法访问self。 我假设您只有一个可以设置的GUI类实例

 GUI.self = self

在图形用户界面中

然后静态方法可以访问图形用户界面. 在

进一步阅读请看http://en.wikipedia.org/wiki/Singleton_pattern和{a2}

相关问题 更多 >