问题描述
def OnSelectCategory(self,listBox,data=None):
try:
# Get the text of the selected category.
selected = listBox.get_selected_row()
label = selected.get_child()
itemText = label.get_text()
# Get the tags for the selected category.
tagtext = self.categoryTags.get(itemText)
self.updateStatusbar("Collecting videos...")
# Start a thread to scan for videos.
self.threadEvent = threading.Event()
self.videoscanThread = threading.Thread(target=self.ScanForVideos,args=(self.threadEvent,tagtext,))
self.videoscanThread.daemon = True
self.videoscanThread.start()
self.threadEvent.set()
except Exception as e:
print("Exception from 'OnSelectCategory':",str(e))
一开始,我无法让状态栏立即更新文本。我最初调用该函数来直接更新文本。在 ScanForVideos 功能完成之前,状态栏文本不会更新。所以,我将 ScanForVideos 代码移到了一个线程中。线程等待事件开始。
线程 (ScanForVideos) 运行多个“for”循环以寻找匹配条件。找到条件后,代码将附加到 IconView 的列表存储中。在线程函数结束时,代码将 IconView 模型设置为列表存储。 IconView 似乎更新了一些项目,但并非所有项目都应该在那里。此外,代码似乎“挂起”,因为我无法关闭包含 IconView 的对话框。我必须停止在 Visual Studio Code 中进行调试。
我觉得我违反了 Python 编码中我不知道的内容。或者,我更新 IconView 的设计不正确。谁能解释一下我可能做错了什么?
解决方法
我意识到我试图从我的后台线程中更新一些 UI 小部件。然后我偶然发现了GLib.idle_add。我编写了一个单独的函数来更新 UI 小部件并调用 idle_add 传递函数的名称。这允许我从后台线程更新 GUI。