2011-04-12 79 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