Re: [Builder] Challenges Binding Buttons to Python Code



On 06/14/2019 05:16 PM, David Strauss wrote:
Hi Builders,

As good opportunities arise, I'd like to share my challenges as a developer new to using Builder for a Python + GTK GUIĀ + Flatpak app. In this case, I think I'm past the problem, but I suspect others might benefit from me sharing how I got past it. I'm also not sure my solution is the best practice, so maybe I still need guidance, too.

Am I on the right track? One of the things I really liked from my
(long ago) Microsoft Access/Visual Basic years was being able to
right-click on a button, select something like "Go to code..." on the
context menu, and land in the right method. If the method didn't
already exist, it got created.

Like anything in programming, there are multiple ways to do what you
want. And the way you've chosen is fine if you don't run into any
road-blocks.

Gtk.ApplicationWindow is a Gio.ActionMap which allows adding actions too
(just like with Gtk.Application). So you can add actions to that and use
"win.action-name" instead of "app.action-name". That allows for a bit
more control.

You can also create your own action groups (using Gio.SimpleActionGroup)
and insert those onto a given Gtk.Widget. Then any widget that is a
descendant can activate the action from there.

For example, lets say you had a series of text formatting actions in a
new action group called "text".

  group = Gio.SimpleActionGroup()
  group.add_action(...)

  my_view.insert_action_group('text', group)

Then your widgets would use something like 'text.bold' as the action-
name.

You can also connect signal callbacks from the UI designer. We don't yet
have the "double-click to jump to code" implemented, simply because of
time constraints and there being more important projects for me to
finish implementing. (So somebody please step up to work on this).

But you can use the signals view in the bottom panel to add signal
handlers. Select the widget you care about, and add a signal handler for
the appropriate signal. Something like "on_button_clicked", then go to
the Python code for that class and add it to the class like:

  @Gtk.Template.Callback('on_button_clicked')
  def on_button_clicked(self, *args):
      print(args)

The args should match the documentation for the signal, but the above is
a quick example to see what those arguments will be.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]