PyGTK 和线程

发布于 2024-10-07 13:17:57 字数 686 浏览 4 评论 0原文

我对 Python 还算陌生,我正在自学,我想说的是,它是一种很棒的编程语言。即使我是直接学习的,但现在我(作为初学者)知道如何使用 GTK、线程、urllib 和其他模块。

但我需要有经验的 PyGTK 用户的答案。看看图片中的这个小例子:

https://i.sstatic.net/y10nv.png< /a>

在此概念应用程序中(它没有工作代码),当用户从 Gtk.TreeView 选择一行时,我将显示一些从网络获取的特定于行的数据(每行的 URL 不同)。因此,当用户单击一行时,就会启动一个线程(以免冻结 GUI)从 URL 获取数据。成功获取数据后,将调用回调,显示列表下 Gtk.Label 中的内容。

问题是,如果用户快速选择一些不同的行,那么...将会出现一些混乱,因为某些请求可能比其他请求花费更长的时间,并且所选行和 Gtk 中的数据显示之间会出现“不同步”。标签。我知道正在运行的线程无法从外部停止(即使我找到了一个线程模块可以实现这一点),那么防止 Gtk.Label 与当前选择不同步的最佳方法是什么?

我为我糟糕的英语和我愚蠢的问题感到抱歉。目前,我通过将所选行中的文本与我期望从请求的 URL 中获得的文本进行比较来解决此问题,但我认为有更好的方法来管理此问题。

非常感谢, 奥维迪乌·尼坦

I'm kinda' new in Python, I'm learning it by myself and all I have to say is that it's a wonderful programming language. Even if I'm learning it in the straight way, for now I know (as a beginner) how to work with GTK, threads, urllib and other modules.

But I need an answer from experienced PyGTK users. Take a look to this little example in the image:

https://i.sstatic.net/y10nv.png

In this concept application (it doesn't have a working code) when the user will select a row from Gtk.TreeView I will show some row-specific data taken from the web (URL differ for each row). So when the user click on a row, a thread is started (to not froze the GUI) getting the data from an URL. When data was succesfully fetched, a callback is called displaying the content in the Gtk.Label placed under the list.

The problem is that if the user quickly selects some different rows, well... there will be a little mess because some requests may take longer than others and there will be a "desynchronization" between the selected row and the data displayer in Gtk.Label. I know that a running thread can't be stopped from outside (even that I found a thread module that make this possible), so what's the best way to prevent the Gtk.Label being desynchronized with the current selection?

I'm sorry for my bad english and for my silly question. Currently I managed to fix this by comparing the text in the selected row with what I expect to get from the requested URL, but I think there is a better way to manage this.

Thank you very much,
Ovidiu Nitan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-10-14 13:17:57

听起来你已经在做显而易见的事情了。在回调函数中,只需检查它来自的 URL 是否与当前选择的行匹配,如果不匹配,则忽略它。

It sounds like you're already doing the obvious thing. In your callback function, simply check that the URL it came from matches the row that's selected at present, and if it doesn't, ignore it.

雪落纷纷 2024-10-14 13:17:57

只有运行主循环的线程才应该更新 GUI,因此您的下载工作人员无论如何都必须进行通信,例如使用idle_add。这允许您在启动线程时传递一个令牌,例如简单的计数器或选定的 TreeIter,并将返回的任何结果与您发送的最后一个结果进行比较。仅在匹配时才设置标签。

Only the thread running the mainloop should update the GUI, so your download worker has to communicate back anyway, for example using idle_add. This allows you to pass a token, like a simple counter or the selected TreeIter, when starting the thread and compare any results that come back to the last one you sent off. Only on match do you set the label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文