Marco Scannadinari
Marco Scannadinari

Reputation: 1884

Passing Gtk.Window to a class?

I'm learning interface design in Python 3 and Glade with GObject introspection, and I don't understand how you can pass another variable (or class?) to another class, like this:

from gi.repository import Gtk

class DemoWindow(Gtk.Window):

And what is the difference between class() and class - Does class() initiate the __init__function, and class is used for referencing other functions, like class.function()?

And is it possible to pass a variable to a class and use it in other functions?

Upvotes: 0

Views: 209

Answers (1)

sepp2k
sepp2k

Reputation: 370465

The syntax class MyClassName(SomeOtherClassName): defines a class called MyClassName which inherits from the (previously existing) class SomeOtherClassName. So in your example DemoWindow inherits from the class Gtk.Window.

And what is the difference between class() and class

MyClassName(args) creates a new object of the MyClassName class and calls its __init__ method with the arguments args. MyClassName.my_function() calls my_function without going through an object, i.e. no self argument will be passed implicitly (unless my_function is defined as a class method).

And is it possible to pass a variable to a class and use it in other functions?

Sure. Just let the __init__ function take it as an argument and then set it as an attribute on self. Other methods of the class can then use it through self.

Upvotes: 1

Related Questions