Python GTK3+使用按钮加载图像
我正在创建一个可以使用按钮加载和显示图像的应用程序。我不明白这将如何与Python Gtk3+一起工作 我想将下一个图像加载到第一个图像所在的GUI位置。。。一个简单的替代品Python GTK3+使用按钮加载图像,python,user-interface,gtk3,Python,User Interface,Gtk3,我正在创建一个可以使用按钮加载和显示图像的应用程序。我不明白这将如何与Python Gtk3+一起工作 我想将下一个图像加载到第一个图像所在的GUI位置。。。一个简单的替代品 image = Gtk.Image() image.set_from_file(self.image) grid.attach(image, 0, 2, 1, 1) #grid location button = Gtk.Button("Load next image") button.connect("clicked"
image = Gtk.Image()
image.set_from_file(self.image)
grid.attach(image, 0, 2, 1, 1) #grid location
button = Gtk.Button("Load next image")
button.connect("clicked", self.load_image)
grid.attach(button, 2, 1, 1, 1) #grid location
button1 = Gtk.Button("Load next image")
button1.connect("clicked", self.load_new_image)
grid.attach(button1, 2, 2, 1, 1) #grid location
def load_image(self, widget):
self.image = 'image_path'
def load_new_image:
self.image = 'image_path'
我想到了活动箱或类似的东西,但我有点不知所措。映像部分只在实例化时运行一次,所以我不明白它应该如何使用事件进行更新。如果self.image路径名在另一个类方法中发生更改,我希望图像发生更改。有什么想法吗?也许我误解了这个问题,但问题不应该那么简单吗 我将用@DanD来解释答案。他指出 您只需要在load_image方法上设置图像路径self.image.set_from_fileimg,该方法与所需图像的按钮单击信号相连接 当前Gtk.Image将自动显示新加载的映像
import gi
import os
import sys
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class GridWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Grid Example")
grid = Gtk.Grid()
self.add(grid)
self.button = Gtk.Button(label="Button 1")
self.image = Gtk.Image()
grid.add(self.button)
grid.add(self.image)
self.button.connect("clicked", self.load_image)
self.count = 0
for root, _, files in os.walk(sys.argv[1]):
self.images = [os.path.join(root, f) for f in files]
def load_image(self, event):
img = self.images[self.count]
print(img)
self.image.set_from_file(img)
self.count = self.count + 1
win = GridWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
我想就是这样。但我建议从以下开始回答:在分配给self.image后,必须从_fileself.image调用image.set_来加载图像。@dad。你完全正确,对不起。刚刚编辑了答案,谢谢!出于某种原因,我从未想过再次从_文件调用set_,我认为它是作为订阅事件连接到类变量的。现在这样做更有意义了!