PyGtk:在窗口的 main() 方法之后更改图像?
我正在使用 gtk.Image 小部件在 gtk 窗口中显示图片。我可以在调用 window.main() 之前设置要显示的图像,但完成后图像将不再改变。基本上:
import pygtk
pygtk.require('2.0')
import gtk
(...)
window= Window()
window.canvas= gtk.Image()
window.window.add(sprite.window.canvas)
window.canvas.show()
window.canvas.set_from_file("pic1.gif")
window.main()
window.canvas.set_from_file("pic2.gif")
将显示 pic1.gif。有没有正确的方法来更改图像(我不在乎是否必须使用 gtk.Image 之外的小部件)?我所能想到的就是毁掉窗户并创建一扇新窗户。
编辑: 我意识到我的错误...我为每个窗口调用了 window.main() ,并为任何窗口的销毁事件调用了 gtk.main_quit() 。必须稍作调整,但现在可以了。即使在调用 window.main() 之后:)
I'm using a gtk.Image widget to display a picture in a gtk window. I can set the image to be displayed before I call window.main(), but after I've done that the image won't change any more. Basically:
import pygtk
pygtk.require('2.0')
import gtk
(...)
window= Window()
window.canvas= gtk.Image()
window.window.add(sprite.window.canvas)
window.canvas.show()
window.canvas.set_from_file("pic1.gif")
window.main()
window.canvas.set_from_file("pic2.gif")
pic1.gif will be displayed. Is there a proper way of changing the image (I don't care if I have to use a widget other than gtk.Image)? All I can think of is destroying the window and creating a new one.
Edit:
I realized my mistake... I called window.main() for every window and any window's destroy event called gtk.main_quit(). Had to make slight adjustments, but it works now. Even after calling window.main() :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Rawing 尚未接受他自己的答案,因此我会将其发布到未回答问题页面的顶部,并通过提供全面的答案来帮助任何人通过搜索引擎点击浏览此内容。 (无论如何,请随意发布您的答案。)
在您的代码中,您将 window 声明为 Window(),而不是 gtk.Window。如果您在一个窗口中进行构建,则不需要每次都执行此操作。创建窗口一次,然后向其中添加您需要的内容。如果您需要其他窗口,请在此模块中单独声明它们,并从代码(而不是从主窗口)调用它们。
此外,不要用“窗口”来命名您的对象。一开始......这只是变得过于混乱。给它一个简单的名称,将其添加到您需要的地方。 Python 将完成剩下的工作。
上面代码的清理版本可能如下所示:
现在,只需更改事件或另一个“def”中的图像,如下所示:
Canvas 应该自动更新图片。
As Rawing hasn't yet accepted his own answer, I'll post it to get this off the top of the unanswered questions page, and to help out anyone skimming this from a search engine clickthrough by providing a comprehensive answer. (Rawing, feel free to post your answer yourself, all the same.)
In your code, you're declaring window as Window(), as opposed to gtk.Window. If you're building in one window, you should not need to do this every time. Create the window once, add what you need to it. If you need additional windows, declare them separately in this module, and call them from code (instead of from main).
Furthermore, don't name your objects with a "window." at the beginning...that just gets overly confusing. Give it a simple name, add it where you need it. Python will do the rest.
A cleaned up version of your code above would probably look like this:
Now, just change the image in an event or another "def", like this:
Canvas should update the picture automatically.