Python 仅在函数运行时显示Gtk微调器
我只想在函数运行时显示Gtk微调器小部件。例如:Python 仅在函数运行时显示Gtk微调器,python,pygtk,python-multithreading,Python,Pygtk,Python Multithreading,我只想在函数运行时显示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 t
[...]
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\u功能锁定
尝试调用异步方法。它可以用gobject或gdk线程实现。此示例使用python线程
许多用于描述功能顺序的打印:
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')
怠速运行样本的结果
background
wrapper
showed
inner
thread
>>>
gogo
callback
gogo
hidden
>>
意味着主线程变为空闲,在它成为后台后输出。如果mainloop是gobject。mainloop()是使用gobject线程的更好方法。
如果是Qt-使用Qt
此装饰器使用gobject.idle\u 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(回调)一样将其传递给decorator:…
@eri非常感谢您的回答!我对python不是很有经验。我在您对我的代码的回答中添加了代码,但它似乎不起作用:(可能我做错了什么。这是代码:当我运行代码并按下旋转按钮时,微调器不会出现,图像也不会旋转当我单击旋转按钮时,显示的内螺纹会出现在终端中,如果我关闭主窗口,`Gtk CRITICAL**:Gtk_标签设置_文本:断言Gtk_是标签(标签)“未隐藏的失败回调”出现在终端中。谢谢。@mnrl您的mainloop是gobject.mainloop
?它需要一些魔力。gobject.threads_init()
在mainloop=gobject.mainloop()之前
@eri,不幸的是,我不知道我的主循环。我正在使用Canonical快速创建ubuntu应用程序模板。它在我的主文件夹中创建一个模板文件夹,有bin、data、help和turuncu(turuncu是我应用程序的名称),turuncu lib文件夹位于模板文件夹中。我将代码写入~/turuncu/turuncu window.py中。/bin/turuncu脚本启动应用程序。这是turuncu window.py:这是/bin/turuncu:我想,导入turuncu turuncu.main()
在/bin/turuncu启动应用程序。我应该在哪里添加gobject.threads\u init()
?请原谅我的无知。再次感谢。