Onecore

  • Home
  • About
  • Advertise
  • Archives
  • Privacy Policy
  • Contact

IronPython Windows Forms – Event Handling

software templates

Building windows forms using ironpython is very easy. If you want to make you windows forms perform certain task then you need to add event handlers into it. Working with event handlers is a bit tricky but not difficult if you know what you’re doing. In this article we’ll see how to write ironpython code to create a windows form with clickable button and label text which changes text.

If you’re new to ironpythion then i suggest you to check the following tutorials before working on this tutorial.

Simple Ironpython Hello World Windows Form – This tutorial will show you simple code to display hello world graphical window.

Ironpython Labels on Windows forms – In this tutorial you’ll learn how to use label control on windows form.

Ironpython Textbox Widget – This is very simple tutorial that shows you how to add textbox tutorial in your ironpython windows tutorial.

Let’s cut some code for our Counter demo program.

Step 1 : In this step we’ll write code to import Sys, CLR and will add references to System.Drawing and System.Windows.Forms. You’ll get the following code after this :

import sys

import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")

Step 2: In this step we’ll declare the controls that are going to be used in the program.

from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label

Step 3: Now you need to write the class and the controls in your program. You’ll get code something like this :

class CountForm(Form):

    def __init__(self):
        self.Text = 'Counter Demo'

//code goes here

form = CountForm()
Application.Run(form)

Step 4: You have initial empty windows form in previous step. You need to add the controls in the program, so let’s start with label control.

self.label = Label()
        self.label.Text = "Counter 0"
        self.label.Location = Point(100, 150)
        self.label.Height = 50
        self.label.Width = 250

This code will add label ‘counter 0’ to your control. This control is fixed at location (100,150) with height (50) and width (250).

Step 5: Now it’s time to add the Button widget box to our program.

button = Button()
        button.Text = "Start Count"
        button.Location = Point(100, 200)

This code will add button widget box to our program at location (100,200) with label text ‘start count’. Our button has event of pressing in order to work this example. So let’s add the buttonPressed function to button widget.

button.Click += self.buttonPressed

You also need to add a counter that counts the value for buttonPressed. Add the counter variable ‘count’ and add the controls to windows form.

self.count = 0

self.Controls.Add(self.label)
        self.Controls.Add(button)

Now you’ve windows form with these control but there is no button pressed event working on the control. In order for button control to work you need to define the buttonPressed function.

def buttonPressed(self, sender, args):
        self.count += 1
        self.label.Text = "Count %s" % self.count

After putting this code you can compile and run this program. Your program will allow you to click on button and after clicking it label text counter will increase it’s value. The complete source code of the program is as follows :

import sys
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")

from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label

class CountForm(Form):

    def __init__(self):
        self.Text = 'Counter Demo'

        self.label = Label()
        self.label.Text = "Counter 0"
        self.label.Location = Point(100, 150)
        self.label.Height = 50
        self.label.Width = 250

        self.count = 0

        button = Button()
        button.Text = "Start Counter"
        button.Location = Point(100, 200)

        button.Click += self.buttonPressed

        self.Controls.Add(self.label)
        self.Controls.Add(button)

    def buttonPressed(self, sender, args):
        self.count += 1
        self.label.Text = "Count %s" % self.count

form = CountForm()
Application.Run(form)

Note : You need to pay attention to indentation or else there is likely to be error thrown while compilation of this program.

software templates

Featured Articles

  • Sample Test Cases for Calendar App
  • Sample Test Cases for Dialog Box
  • Test Cases for Amazon Login Screen Page
  • Sample Test Cases for Online Editor
  • Sample Test Cases for Online Book Shopping

WPForms

ENDORSEMENT DISCLOSURE: In order for us to support our website activities, we may receive monetary compensation or other types of remuneration for our endorsement, recommendation, testimonial and/or link to any products or services from this website.

Copyright © 2022 . This website uses cookies to ensure you get the best experience on our website.