GTK screen updating

My question

How does a person update the screen in gtk for a game or video updating?

notes

Glade was used to make the following code and I was wondering how I would update the screen (the ppm that is currently being used is just a placeholder)
this is the relevant image class that i wanted to update (about 60 times a second at 1920 by 1080 pixels)

<object class="GtkImage">
                    <property name="width-request">100</property>
                    <property name="height-request">80</property>
                    <property name="visible">True</property>
                    <property name="can-focus">False</property>
                    <property name="pixbuf">image.PPM</property>

how would I update the image?
This is the code I am using for the placeholder image

function GHW()
            nx = 1920 - 200
            ny = 1080 - 200
            open("image.PPM", "w") do image  
                        write(image,"P3\n" , string(nx) , " " , string(ny) , "\n" , string(255) , "\n")
                        for i in 1 : ny
                                    for j in 1 : nx
                                                r =  (nx - j) / nx
                                                g = i / ny 
                                                b = (i*j) / (nx*ny)
                                                ir = string(Integer(255 - trunc(255.99*r)))
                                                write(image, ir , " ")
                                                ig = string(Integer(255 - trunc(255.99*g)))
                                                write(image,ig , " ")
                                                ib = string(Integer(trunc(255.9*b)))
                                                write(image,ib , " ")
                                                write(image, "\n")
                                    
                                    end
                        end
            end

            nx = round(Int,nx / 3)
            ny = round(Int,ny / 3)
            open("mini_image.PPM", "w") do image  
                        write(image,"P3\n" , string(nx) , " " , string(ny) , "\n" , string(255) , "\n")
            
                        for i in 1 : ny
                                    for j in 1 : nx
                                                r =  (nx - j) / nx
                                                g = i / ny 
                                                b = (i*j) / (nx*ny)
                                                ir = string(Integer(255 - trunc(255.99*r)))
                                                write(image, ir , " ")
                                                ig = string(Integer(255 - trunc(255.99*g)))
                                                write(image,ig , " ")
                                                ib = string(Integer(trunc(255.9*b)))
                                                write(image,ib , " ")
                                                write(image, "\n")
                                    
                                    end
                        end
            end
end

If possible please help change the size of the image dependent on window size too that would be great

Hi, and welcome to the Julia community!


Are you using Gtk.jl? If so, this is deprecated and you should switch to Gtk4.jl.


With the caveat that I’m not an expert, and that this approach is presumably not optimal in any way, the following kind of works:

using Gtk4
using Images   # for RGB{N0f8} (could use a smaller dependency)
using Random   # for rand!

width = 1920
height = 1080

win = GtkWindow("Random pixels", width, height)
pic = GtkPicture()
win[] = pic  # or push!(win, pic)

img = rand(RGB{N0f8}, width, height)
pixbuf = Gtk4.GdkPixbuf(img)

g_timeout_add(33) do  # 33 ms: 30 fps
	rand!(img)  # updates img, hence pixbuf
	pic.paintable = Gtk4.GdkTexture(pixbuf)
	return true  # never stop
end

(You could also create pic = GtkPicture(pixbuf), the image data of which should update when we call rand!(img). But Gtk does not realise anything has changed, and thus does not redraw. Methods like show, reveal, and a (possibly incorrect)

ccall((:gtk_widget_queue_draw, Gtk4.libgtk4), Nothing, (Ptr{Gtk4.GObject},), pic)

seem unable to force an update. Setting pic.paintable to a newly created texture does.)


The GtkPicture automatically scales to fill the window size as much as possible, while preserving the aspect ratio. It seems to use basic linear interpolation. Therefore, you don’t necessarily have to correlate size(img) with the window size.