1

我只想在我的函数运行时显示 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 运行时微调器没有出现,在它上面,窗口变暗,因为窗口没有响应。我应该如何使用微调器并防止无响应的窗口?谢谢

4

2 回答 2

1

由于主循环被锁定,窗口变暗my_function

尝试异步调用您的方法。它可以用 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')

使用 IDLE 运行示例的结果

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

>>>表示主线程变为IDLE,进入后台后输出。

于 2013-08-24T11:11:30.490 回答
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):...

于 2013-08-26T14:41:30.730 回答