-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gtk.Builder().get_object(id
)???
#205
Comments
Pretty sure this isn't a place to ask for help.
Gtk.Builder() is documented here https://python-gtk-3-tutorial.readthedocs.io/en/latest/builder.html builder = Gtk.Builder()
builder.add_from_file("example.glade")
window = builder.get_object("window1") |
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.
I think this should be explicitly written in the docs. As far as I'm aware, Also I vaguely remember seeing the example with 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: