由於主迴路被鎖定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
運行示例的結果。
來源
2013-08-24 11:11:30
eri
@eri很多感謝爲您的答案!我對python不是很有經驗。我在代碼中添加了代碼,但它似乎不起作用:(也許我做錯了什麼。這是代碼:https://dl.dropboxusercontent.com/u/76369534/Threading當我運行代碼並按下rotate_button旋轉器不出現,圖像不旋轉 – mnrl
當我點擊rotate_button'顯示 內 線程 '出現在終端,如果我關閉主窗口'Gtk-CRITICAL **:gtk_label_set_text:斷言'GTK_IS_LABEL (標籤)」失敗 回調 無 隱藏 '出現在終端,謝謝。 – mnrl
@mnrl你的主循環是'gobject.mainloop'?它需要一些魔術。'gobject.threads_init()''之前=主循環gobject.MainLoop ()' – eri