2011-04-12 78 views
2

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

+0

你真的应该保留所有的import语句的顶部,除非你'动态导入模块。 – 2011-04-18 13:04:49

回答

1

如果我理解你是正确的,你正在调用一个静态方法,因此你不能访问自己。 正如我假设你有你的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/

+0

是的人,它是__REALLY__我想要什么,但是当我尝试把桂。self =自我在我的gui模块_ init _函数中的行它给我下面的错误__NameError:名称'self'没有被定义___ – 2011-04-20 17:52:05

+0

我很抱歉它没有给我那个错误,set_progress函数给我__TypeError:set_progress( )只需要2个参数(1给出)__,并确定这意味着它不接受自我。 – 2011-04-20 18:08:53

+0

\ __ init__和set_progress()的方法签名/争论名称的外观如何? – rocksportrocker 2011-04-21 08:22:22

3

我会有点不同的攻击。我会去pyDispatcher,你可以定义什么样的qt调用“插槽和信号”,或者你可能只知道“信号”,而不是信号的os类型。这些信号在“发射”或执行一系列或一组功能时,已附加到信号上。插槽是执行的函数,调度程序保存对插槽的弱引用的字典,并使用您的信号发出的参数调用它们。

查看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)这将调用update_progress与kwargs progress=progress。通过这种方式,您可以从静态方法更改更新进度并直接更新gui。

+0

但我认为这不能解决我的问题,即将加载屏幕的实例发送到gui模块中的update_loading_screen函数,让我访问进度栏并更改其值 – 2011-04-18 12:50:24

+0

@Menopia这是一种完全不同的方式攻击的问题比你现在拥有的还要多。问题是更新gui中的进度条,而你的“后端”完成这项工作。这就是为什么我以“我会以不同方式攻击这个问题”开始信息。 – 2011-04-18 18:32:29

+0

我不能在游戏中使用pyDispatcher,而我的引擎现在无法提供事件处理! :( – 2011-04-20 20:47:45

4

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

这很有帮助,谢谢! – Drewdin 2011-04-21 15:35:50