2013-08-24 49 views
1

我只想在我的函數運行時顯示Gtk微調控件微件。例如:僅在函數運行時顯示Gtk微調器

[...] 

self.spinner.hide() # hide spinner at startup 

self.my_function(input) # run function 

def my_function(self, input) 
    self.spinner.show() # show spinner when function running 
    # do something here that takes long time 
    self.spinner.hide() # hide spinner when the process is complete 
    return output 

我用這一點,但同時創建my_function運行,在它上面的窗口天黑作爲響應窗口微調不會出現。 我應該如何使用微調並防止無響應的窗口? 謝謝

回答

1

由於主迴路被鎖定my_function,窗口變暗。

嘗試調用您的方法異步。它可以用gobject或gdk線程來實現。這個例子用python線程。

許多印刷品來描述的功能的順序:

background 
wrapper 
showed 
inner 
thread 
>>> 
gogo 
callback 
gogo 
hidden 

>>>意味着主線程變爲空閒時,輸出後它是背景

import threading 

class Thread(threading.Thread): 
    def __init__(self,callback,*args,**kwargs): 
     self.__callback = callback 
     threading.Thread.__init__(self,*args,**kwargs) 
    def run(self): 
     try: 
      if self.__target: 
       print('thread') 
       _self = self.__kwargs.get('self',self.__args[0]) 
       self.__callback(_self, self.__target(*self.__args, **self.__kwargs)) 
     finally: 
      # Avoid a refcycle if the thread is running a function with 
      # an argument that has a member that points to the thread. 
      del self.__target, self.__args, self.__kwargs   

def background(callback): 
    print('background') 
    def wrapper(fun): 
     print('wrapper') 
     def inner(*args,**kwargs): 
      print('inner') 
      Thread(callback=callback,target=fun,args=args,kwargs=kwargs).start() 
     return inner 
    return wrapper 

def spinner(fun): 
    def inner(self,*args,**kwargs): 
     self.show() 
     result = fun(self,*args,**kwargs) 
     self.hide() 
     return result 
    return inner 


def spinner_hide(fun): 
    def inner(self,*args,**kwargs): 
     result = fun(self,*args,**kwargs) 
     self.hide() 
     return result 
    return inner 

def spinner_show(fun): 
    def inner(self,*args,**kwargs): 
     self.show() 
     result = fun(self,*args,**kwargs) 
     return result 
    return inner 


class A(object): 
    @spinner_hide 
    def my_function_callback(self,data): 
     print('callback') 
     print(data) 
    @spinner_show 
    @background(my_function_callback) 
    def my_function(self, input): 
     # do something here that takes long time 
     print(input) 
     output=input 
     return output 

    def show(self): print('showed') 
    def hide(self): print('hidden') 

a=A() 
a.my_function('gogo') 

與IDLE

運行示例的結果。

+0

@eri很多感謝爲您的答案!我對python不是很有經驗。我在代碼中添加了代碼,但它似乎不起作用:(也許我做錯了什麼。這是代碼:https://dl.dropboxusercontent.com/u/76369534/Threading當我運行代碼並按下rotate_button旋轉器不出現,圖像不旋轉 – mnrl

+0

當我點擊rotate_button'顯示 內 線程 '出現在終端,如果我關閉主窗口'Gtk-CRITICAL **:gtk_label_set_text:斷言'GTK_IS_LABEL (標籤)」失敗 回調 無 隱藏 '出現在終端,謝謝。 – mnrl

+0

@mnrl你的主循環是'gobject.mainloop'?它需要一些魔術。'gobject.threads_init()''之前=主循環gobject.MainLoop ()' – eri

1

如果mainloop是gobject.MainLoop()是使用gobject線程的更好方法。 如果Qt - 使用Qt。

這個裝飾用gobject.idle_add進行函數異步,但沒有回調

def async(func): 
    """Make a function mainloop friendly. the function will be called at the 
    next mainloop idle state.""" 
    def new_function(*args, **kwargs): 
     def async_function(): 
      func(*args, **kwargs) 
      return False 
     gobject.idle_add(async_function) 
    return new_function 

實現你的函數,而不是調用return回調或將它傳遞給裝飾器爲在def background(callback):...