Whenever object-oriented programming is done in Python, we mostly come across the __init__ method. This article explains the main concept of __init__ method in Python when used for Windows app development of graphical user interfaces (GUIs).
“__init__” is a reserved method in python classes. It is known as a constructor in object-oriented concepts. This method is called when an object is created from the class and it allows the class to initialize the attributes of a class.
The __init__ method is similar to constructors in C++ and Java. Constructors are used to initialize the object’s state. The task of constructors is to initialize (assign values) to the data members of the class when an object of a class is created. Like methods, a constructor also contains a collection of statements (i.e. instructions) that are executed at the time of Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
Here is the basic example of how we can use __init__:
1 2 3 4 5 6 7 8 9 10 11 12 |
# A Sample class with __init__ method class Person: # init method or constructor def __init__(self, name): self.name = name # Sample Method def say_hi(self): print('Hello, my name is', self.name) p = Person('Jim') p.say_hi() |
The output in PyScripter IDE:
In the above example, a person named Jim is created. While creating a person, “Jim” is passed as an argument, and this argument will be passed to the __init__ method to initialize the object. The keyword self represents the instance of a class and binds the attributes with the given arguments. Similarly, many objects of the Person class can be created by passing different names as arguments. Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# A Sample class with init method class Person: # init method or constructor def __init__(self, name): self.name = name # Sample Method def say_hi(self): print('Hello, my name is', self.name) # Creating different objects p1 = Person('Jim') p2 = Person('Eli') p3 = Person('Marco') p1.say_hi() p2.say_hi() p3.say_hi() |
The output in PyScripter IDE:
The following code is the working example of using __init__ method in the context of creating GUI using DelphiVCL library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from DelphiVCL import * class MainForm(Form): def __init__(self, Owner): self.Caption = "A VCL Form..." self.SetBounds(10, 10, 340, 410) self.lblHello = Label(self) self.lblHello.SetProps(Parent=self, Caption="Hello Python") self.lblHello.SetBounds(10,10,100,24) self.edit1 = Edit(self) self.edit1.SetProps(Parent=self, Top=30, Left=10, Width=200, Height=24) self.button1 = Button(self) self.button1.SetProps(Parent=self, Caption="Add", OnClick=self.Button1Click) self.button1.SetBounds(220,29,90,24) self.lb1 = ListBox(self) self.lb1.SetProps(Parent=self) self.lb1.SetBounds(10,60,300,300) def Button1Click(self, Sender): self.lb1.Items.Add(self.edit1.Text) def main(): Application.Initialize() Application.Title = "My DelphiVCL App" f = MainForm(Application) f.Show() FreeConsole() Application.Run() main() |
And here is the GUI result:
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.