ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# Busy Cursors 本节介绍在应用程序的长时间运行部分中如何更改光标。 更改为繁忙的光标会向应用程序用户提供一些反馈,否则他们可能会认为该应用程序已挂起,而不执行任何操作。其中一个在支持模块的模块级别包含以下代码: ~~~ # 添加了允许将默认光标更改为繁忙光标的代码。 # 格雷格·沃尔特斯(Greg Walters)在python编程示例中基于代码手动添加了变量以指示较长的过程。 # 这些例程也可以在Greyson的pg页中看到。158。 busyCursor = 'watch' preBusyCursors = None def busyStart(newcursor=None): '''我们首先检查是否将值传递给newcursor。 如果没有,我们默认使用busyCursor。 然后,我们遍历busyWidgets元组,并将光标设置为所需的任何内容。''' global preBusyCursors if not newcursor: newcursor = busyCursor newPreBusyCursors = {} for component in busyWidgets: newPreBusyCursors[component] = component['cursor'] component.configure(cursor=newcursor) component.update_idletasks() preBusyCursors = (newPreBusyCursors, preBusyCursors) def busyEnd(): '''在这个例程中,我们基本上将busyWidget元组中的小部件的游标重置为默认游标''' global preBusyCursors if not preBusyCursors: return oldPreBusyCursors = preBusyCursors[0] preBusyCursors = preBusyCursors[1] for component in busyWidgets: try: component.configure(cursor=oldPreBusyCursors[component]) except KeyError: pass component.update_idletasks() # 繁忙游标代码的结尾。 ~~~ 并将以下代码行插入支持模块的“ init”中: ~~~ global busyWidgets busyWidgets = (top, ) ~~~ 第一行在函数顶部附近,并且在创建根对象之后插入对busyWidgets的分配。 在我的一个应用程序中,函数“ init”如下所示: ~~~ def init(top, gui): ''' 创建用于定期更新GUI的线程的功能。''' global t global w, top_level global busyWidgets w = gui top_level = top t = threading.Thread(target=fill_window,) t.start() busyWidgets = (top, w.Scrolledtext1) ~~~ 上面的最后一行将全局变量busyWidgets设置为我希望显示busy光标的那些小部件的元组。 这是来自示例wcpe的示例,在该示例中,我希望忙碌的光标出现在顶级窗口以及Scrolledtextbox中。当启动一段可能长时间运行的代码段(繁忙部分)时,请在开始处插入以下内容: ~~~ busyStart() ~~~ 当离开繁忙的部分时,请在最后一个statement()中进行以下操作: ~~~ busyEnd() ~~~ 显然,一个应用程序可能有许多忙碌的部分,它们可能与特定功能一致或不一致。 可以通用使用此代码以使用任何Tkinter游标。