在python中将进度值发送到进度条

发布于 10-31 21:35 字数 1465 浏览 2 评论 0原文

在我的游戏中,我有两个模块,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 功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶底世界2024-11-07 21:35:44

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

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()
熊抱啵儿2024-11-07 21:35:44

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

请参阅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 调用 update_progress =进度。这样您就可以将更新进度从静态方法更改为直接更新 GUI。

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.

风流物2024-11-07 21:35:44

如果我理解正确的话,您正在调用静态方法,因此您无法访问 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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文