I'm creating an app that can use a button to load and display an image. I don't understand how this would work with Python Gtk3+.
I want to load the next image into the GUI location where the first image is... a simple replacement.
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'
I thought of Event Boxes, or something similar, but I'm kind of at a loss. The image section is only run once on instantiation, so I don't understand how it should get updated with events. I want the image to change if the self.image
path name changes in another class method. Any ideas?
Maybe I am misunderstanding the question, but should not it be that simple?
I will explain the answer as @DanD. pointed out.
You just need to set the image path (self.image.set_from_file(img)) on the load_image method (connected with the button clicked signal) with the desired image.
Current Gtk.Image will display automatically the new loaded 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()