pythongtkpygobjectgobjectgtkbuilder

Can I add element in code so that I could get it later through Gtk.Builder()?


I use Python 3+PyGObjects+Gtk.Builder I have some window, what I create from glade:

builder = Gtk.Builder()
builder.add_from_file("main.ui")

I create some widgets and add them(i see it in window):

switch = Gtk.Switch()
switch.set_name('test')

hBox = Gtk.HBox()
hBox.pack_start(switch, True, True, 10)

window = builder.get_object("MainWindow")
window.add(hBox)

At another part of program I do not have access to "switch", but have to "builder". I try:

switch = builder.get_object('test')
switch_active = switch.get_active()

But receive error:

AttributeError: 'NoneType' object has no attribute 'get_active'

How I can get it from "builder"?


Solution

  • You can't. Builder can only give you objects defined in the ui file.

    You should modify your program so that you do have access to switch in the part that needs to get the active value.