返回介绍

First steps in IronPython Mono Winforms

发布于 2025-02-22 22:19:48 字数 6938 浏览 0 评论 0 收藏 0

In this part of the IronPython Mono Winforms tutorial, we introduce some basic programs in Winforms programming library.

Simple

This is a simple Winforms application.

simple.py

#!/usr/bin/ipy

import clr

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

from System.Windows.Forms import Application, Form

class IForm(Form):

  def __init__(self):
    self.Text = 'Simple'
    self.Width = 250
    self.Height = 200
    self.CenterToScreen()

Application.Run(IForm())

This code example shows a small window on the screen.

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

We add reference for the Winforms library.

class IForm(Form):

In Winforms, any window or a dialog is a Form . This control is a basic container, whose purpose is to display other child controls. Our class, IForm , inherits from a form. This way it becomes a form itself.

self.Text = 'Simple'
self.Width = 250
self.Height = 200

Text , Width and Height are properties of a form. Changing these properties, we modify our form control. The first line displays text "Simple" in the titlebar of the form control. The other two lines set the size of the form to 250x200 px.

self.CenterToScreen()

This method centers our application on the screen.

Application.Run(IForm())

This line runs the example.

Simple
Figure: Simple

Icon

Mono means monkey in Spanish. If we do not provide an icon for our application, we have a head of a monkey by default. The next example shows how to change this.

icon.py

#!/usr/bin/ipy

import clr
import sys

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

from System.Windows.Forms import Application, Form
from System.Drawing import Icon

class IForm(Form):

  def __init__(self):
    self.Text = 'Icon'
    self.Width = 250
    self.Height = 200
    
    try:
      self.Icon = Icon("web.ico")
    except Exception, e:
      print e.msg
      sys.exit(1)     
    
    self.CenterToScreen()

Application.Run(IForm())

The code example shows an icon in the upper left corner of the form. A form's icon is the picture that represents the form in the taskbar as well as the icon that is displayed for the control box of the form.

clr.AddReference("System.Drawing")

The Icon object comes from System.Drawing module. So we have to add a reference.

try:
  self.Icon = Icon("web.ico")
except Exception, e:
  print e.msg
  sys.exit(1)  

It is a good practice to put all input output work between the try and except keywords. The web.ico file must be available in the current working directory. This is the directory from where we execute (ipy icon.py) our application.

Icon
Figure: Icon

Tooltips

A tooltip is a small rectangular pop-up window that displays a brief description of a control's purpose when the user rests the pointer on the control.

tooltips.py

#!/usr/bin/ipy

import clr

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

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

class IForm(Form):

  def __init__(self):
    self.Text = 'Tooltips'
    self.CenterToScreen()
    self.Size = Size(200, 150)

    tooltip = ToolTip()
    tooltip.SetToolTip(self, "This is a Form")

    button = Button()
    button.Parent = self
    button.Text = "Button"
    button.Location = Point(50, 70)

    tooltip.SetToolTip(button, "This is a Button")


Application.Run(IForm())

Our code example creates a tooltip for two controls. The Button control and the Form control.

tooltip = ToolTip()

Here we create the ToolTip control. This instance is used to provide tooltips for both controls.

tooltip.SetToolTip(self, "This is a Form")

Here we set a tooltip for a form.

tooltip.SetToolTip(button, "This is a Button")

And here for the button.

button = Button()
button.Parent = self
button.Text = "Button"
button.Location = Point(50, 70)

Notice the creation of the Button control. The Parent property determines the container, where the button will reside. The Text property is a label for the button. The Location property places the button on the form at x=30, y = 70 px coordinates.

Tooltips
Figure: Tooltips

Button

Our last code example shows a button control in action.

button.py

#!/usr/bin/ipy

import clr

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

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

class IForm(Form):

  def __init__(self):
    self.Text = 'Button'
    self.CenterToScreen()
    self.Size = Size(200, 150)

    btn = Button()
    btn.Parent = self
    btn.Text = "Quit"
    btn.Location = Point(50, 50)
    btn.Click += self.OnClick
    btn.MouseEnter += self.OnEnter


  def OnClick(self, sender, args):
    self.Close()

  def OnEnter(self, sender, args):
    print "button entered"


Application.Run(IForm())

All GUI programming is event driven programming. In our example, we show a button control on a form container. The button will listen to two events: The Click and the MouseEnter events.

btn.Click += self.OnClick

This code line plugs an event handler to the Click event. When we click on the button, the OnClick() method is called.

btn.MouseEnter += self.OnEnter

When we enter the button area with the mouse pointer, the MouseEnter event is triggered. In this case, our code calls the OnEnter() method.

def OnClick(self, sender, args):
  self.Close()

The method closes the application.

def OnEnter(self, sender, args):
  print "button entered"

When we enter the button control area with the mouse pointer, "button entered" text is displayed in the terminal.

This part of the IronPython Mono Winforms tutorial showed some introductory code examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文