Skip to content
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

Open
margual56 opened this issue Jul 27, 2021 · 2 comments
Open

Gtk.Builder().get_object(id)??? #205

margual56 opened this issue Jul 27, 2021 · 2 comments

Comments

@margual56
Copy link

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

Confusion

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'):

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.

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

@friday
Copy link

friday commented May 21, 2022

Pretty sure this isn't a place to ask for help.

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

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")

@margual56
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants