Custom widget with some stuff

Hi,

I would like a pixels canvas to simulate gardens.
The idea began to be clear, but I must level-up in programming to be autonomous.

Scaling with this code zoom but unzoom not.
Any suggestion please ?

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import GObject, Gtk, Gdk, Gio, Graphene, Gsk

import numpy


def slider_changed(scale_btn):
    global width, height 
    width = int(scale_btn.get_value())
    height = scale_btn.get_value()

    # Ajoute du contenu dans la grille
    i = 0
    j = 0
    c = []
#        cj = []
#        c = numpy.array([[],[]])
    while i < 5:
        while j < 10:
            c.append(f"custom{i}{j}")
            c[(i*j)] = Custom1(width, height)
            c[(i*j)].queue_resize()
            GridWindow.attach(c[(i*j)], i, j, 1, 1)     
            j += 1
        j = 0
        i += 1



class Custom1(Gtk.Widget):
    def __init__(self, width, height):
        super().__init__()
        self.width = width
        self.height = height
        self.set_size_request(self.width, self.height)


    def do_snapshot(self, s):
        colour = Gdk.RGBA()
        colour.parse("rgb(159, 222, 42)") # another way of parsing
        rect = Graphene.Rect().init(0, 0, (2 * self.width)/3, (2 * self.height)/3)
        s.append_color(colour, rect)


class AccueilWindow(Gtk.ApplicationWindow):
    def __init__(self, application):
        Gtk.Window.__init__(self, application=application)
        self.set_title("GBioDyn")
        self.set_default_size(400, 600)

        # Ajoute un conteneur 
        cargo = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        cargo.set_vexpand(True)
        cargo.set_spacing(5)
        self.set_child(cargo)


        # Ajoute une grille avec défilement
        Window = Gtk.ScrolledWindow()
        Window.props.hexpand = True
        Window.props.vexpand = True
        Window.props.margin_top = 6
        Window.props.margin_bottom = 6
        Window.props.margin_start = 6
        Window.props.margin_end = 6
        cargo.append(Window)

#        da = Gtk.DrawingArea()
#        self.set_child(da)
        global GridWindow

        GridWindow = Gtk.Grid()
        Window.set_child(GridWindow)

        # Ajoute une échelle
        scale_btn = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0.1, 50, 1)
        scale_btn.set_value(3)
        width = scale_btn.get_value()
        height = scale_btn.get_value()
        scale_btn.set_draw_value(True)

        cargo.append(scale_btn)

        # Ajoute du contenu dans la grille
        i = 0
        j = 0
        c = []
#        cj = []
#        c = numpy.array([[],[]])
        while i < 5:
            while j < 10:
                c.append(f"custom{i}{j}")
                c[(i*j)] = Custom1(width, height)
                GridWindow.attach(c[(i*j)], i, j, 1, 1)               
                j += 1
            j = 0
            i += 1

        scale_btn.connect('value-changed', slider_changed)


class GBioDyn(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self, application_id='fr.my.GBioDyn', flags=Gio.ApplicationFlags.DEFAULT_FLAGS )

    def do_activate(self):
        window = AccueilWindow(self)
        window.present()
      
if __name__ == '__main__':
    app = GBioDyn()
    app.run(None)

Thanks for all documentations and shares about python and glib in the www.
Particularly for the code above :

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.