You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this question belongs in this repo...
I just want to be able to get an object from the window in python 😭
Prelude
So, as the title says, you can use the Builder object to get an object by its unique ID. Here's the thing tho, in order to do that you need a previously existing Builder object to which all the widgets are added.
If you execute print(Gtk.Builder().get_object('some_ID_that_exists')) it outputs [] (an empty array), because with Builder() you are creating a new empty object without widgets.
Example
If you create a GTK-Builder project with Python, you might notice that there is no "Builder" object anywhere, the widgets are added from the template (the 'window.ui' file) directly. I leave here a link to my project in case you don't believe me, but trust me: there is none.
The objects Gtk.ApplicationWindow and Gtk.Application do not have a builder attribute as some sources suggest
is that there is no PyGTK resource that actually gives the correct solution!!
The correct solution is to create a variable with the same name as the ID of the object and call Gtk.Template.Child() and magically, it gives you the correct object.
Example (if the ID is 'ok_button'):
ok_button = Gtk.Template.Child()
It's that easy!
PS
I've been searching this question for more than 5h now 😅 we all know this is one of those things I could have missed but... If so, it needs more visibility.
Gtk.Builder() and Gtk.Template are two different ways to handle the same thing. You can't mix these. Templates are more object oriented and declarative. Gtk.Template.Child() only works for classes decorated with Gtk.Template
I was not asking for help, I was saying that I had a hard time making this work as a beginner. In other words, I think this needs clarification in the docs.
I was trying to make a Gtk application with Gnome Builder. I can't remember exactly what the code was because it has been a year, but I do remember being confused with the template project.
Gtk.Builder() and Gtk.Template are two different ways to handle the same thing. You can't mix these. Templates are more object oriented and declarative. Gtk.Template.Child() only works for classes decorated with Gtk.Template
I think this should be explicitly written in the docs. As far as I'm aware, Gtk.Template.Child() is nowhere to be found in the docs, and this is exactly what I was confused about.
Also I vaguely remember seeing the example with builder.add_from_file("example.glade") and being unable to do this because with Gnome Builder you can define the interface elsewhere than in a .glade file.
So the only option I had was to use the Gtk.Template.Child().
As I said, I am a beginner in this framework and I found this a bit confusing when reading the documentation.
I'm not sure if this question belongs in this repo...
I just want to be able to get an object from the window in python 😭
Prelude
So, as the title says, you can use the
Builder
object to get an object by its unique ID. Here's the thing tho, in order to do that you need a previously existing Builder object to which all the widgets are added.If you execute
print(Gtk.Builder().get_object('some_ID_that_exists'))
it outputs[]
(an empty array), because withBuilder()
you are creating a new empty object without widgets.Example
If you create a GTK-Builder project with Python, you might notice that there is no "Builder" object anywhere, the widgets are added from the template (the 'window.ui' file) directly. I leave here a link to my project in case you don't believe me, but trust me: there is none.
The objects
Gtk.ApplicationWindow
andGtk.Application
do not have abuilder
attribute as some sources suggestConfusion
Some sources redirect to the pygobject documentation, but the page does not exist.
What I'm getting at
is that there is no PyGTK resource that actually gives the correct solution!!
The correct solution is to create a variable with the same name as the ID of the object and call
Gtk.Template.Child()
and magically, it gives you the correct object.Example (if the ID is 'ok_button'):
It's that easy!
PS
I've been searching this question for more than 5h now 😅 we all know this is one of those things I could have missed but... If so, it needs more visibility.
This is where I found the solution: https://stackoverflow.com/questions/56010352/i-need-example-code-for-a-simple-python-gui-script-with-gtk. A simple answer like that, which only has 1 extra line of code, was enough to understand.
References
The text was updated successfully, but these errors were encountered: