【问题标题】:Sending progress value to progress bar in python在python中将进度值发送到进度条
【发布时间】:2011-04-12 22:45:47
【问题描述】:

在我的游戏中,我有两个模块,island.py 将岛屿加载到我的游戏中,第二个模块是 gui.py,它在游戏前处理 gui 小部件开始。我的问题是如何将 island.py 模块中的进度值发送到在 gui.py 模块中创建的进度条 编辑:还使用加载屏幕的实例来访问进度条并更改其值。

在模块 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

在模块 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)

如何实现这个 update_loading_screen 功能?

【问题讨论】:

  • 你真的应该把所有的导入语句放在最前面,除非你动态导入模块。

标签: python user-interface loading progress


【解决方案1】:

扩展 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()

【讨论】:

    【解决方案2】:

    我会以不同的方式来解决这个问题。我会去获取 pyDispatcher,通过它您可以定义 qt 所谓的“插槽和信号”,或者您可能只知道“信号”,而不是 os 类型的 SIGNAL。这些信号在“发出”或执行一系列或一组功能时,您已附加到信号上。插槽是被执行的函数,调度器保留一个对插槽的弱引用的字典,并使用您通过信号发出的参数调用它们。

    查看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 progress=progress 的update_progress。这样您就可以将更新进度从静态方法更改为直接更新 gui。

    【讨论】:

    • 但是我觉得这并不能解决我的问题,就是将加载屏幕的实例发送到 gui 模块中的 update_loading_screen 函数,让我访问进度条并更改它的值跨度>
    • @Menopia 解决问题的方法与您现在的方法完全不同。问题是在 gui 中更新进度条,而您的“后端”完成了工作。这就是为什么我以“我会以不同的方式解决这个问题”开头的消息。
    • 我不能在游戏中使用 pyDispatcher 并且我的引擎现在不提供事件处理! :(
    【解决方案3】:

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

     GUI.self = self
    

    在 GUI.__init__ 中

    然后静态方法可以访问 GUI.self。

    如需进一步阅读,请查看http://en.wikipedia.org/wiki/Singleton_patternhttp://code.activestate.com/recipes/52558-the-singleton-pattern-implemented-with-python/

    【讨论】:

    • 是的,这是我想要的 REALLY,但是当我尝试将 Gui.self = self 行放在我的 gui 模块的 _ init _ 函数中时,它给了我以下信息错误 NameError: name 'self' is not defined
    • 很抱歉,它没有给我那个错误,set_progress 函数给了我 TypeError: set_progress() 恰好需要 2 个参数(给定 1 个) 并确保这意味着它没有'不接受自我。
    • _init_ 和 set_progress() 的方法签名/参数名称如何?
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多