Merging instance with GtkBuilder object



Hey,

what I'm trying to do (and failing doing it) it to create a python class
derived from Gtk.Window and then loading this instance from a GtkBuilder
file.

The BtkBuilder fiel is some really basic stuff:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Hopefully never
shown!</property>
      </object>
    </child>
  </object>
</interface>


This is my code:

from gi.repository import Gtk

class Test(Gtk.Window):
	
	def __init__(self):
		
		builder = Gtk.Builder()
		builder.add_from_file("ui.glade")
		
		self = builder.get_object("window1")
		self.connect("delete-event", Gtk.main_quit)
		self.msg = builder.get_object("label1")
		
		# attribute not found exception
		self.set_test()
		self.show_all()
		
	def set_test(self):
		self.msg.set_text("Epic Win")
		

w = Test()
Gtk.main()

What I'm trying is to merge my funcionality in the class with the UI
description in the GtkBuilder file. Obviously replacing self doesn't
work but I didn't get it to work with the __init__ function.

My question is if it is possible to do what I want or if there are
better pratices to do this. In the project I hope to use this the
GtkBuilder file will be quite bigger and it feels more natural for me to
create a class for each Window object which then includes all the
functionality. (However I'm coming from Java so if that's common
practice in Python please tell me!)

Thanks for any hints!

With best regards,
Paul



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