There is simple example of code (python 3, linux, gtk4)
#!/usr/bin/python3
import sys
import gi
import os, fnmatch
import configparser
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk,Gio,Gdk
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.app_ = self.get_application()
self.set_default_size(800, 600)
for i in range(1, 11):
self.button = Gtk.Button.new()
self.button.set_label('test'+str(i))
self.button.connect("clicked", self.onclick)
self.set_child(self.button)
def onclick(self, button):
print(button)
self.button.set_label('NEW LABEL')
self.ANOTHER_BUTTONS.set_label('ANOTHER LABELS')
self.ANOTHER_BUTTON.do_something_from_gtk()
class MyApp(Gtk.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def do_activate(self):
active_window = self.props.active_window
if active_window:
active_window.present()
else:
self.win = MainWindow(application=self)
self.win.present()
app = MyApp(application_id="com.github.yucefsourani.myapplicationexample1",flags= Gio.ApplicationFlags.FLAGS_NONE)
app.run(sys.argv)
This code - must create 10 buttons on window, click on what, calling function "onclick", and code inside of function should have access to properties of all buttons by some identifier.
Also want to mention, that instead of Gtk.Button there could be any another object, so my question if better about Python, not about Button properties.
Is there a possibility to use some index to call objects? Something like "self.button[i] = Gtk.Button.new()" ?
Thanks !
I tried to add indexes or any another IDs - not working, always returns errors like
self.names[i] = Gtk.Button() ~~~~~~~~^^^ IndexError: list assignment index out of range