如何使用Winforms在IronPython中绘制线条?

时间:2012-11-11 18:20:39

标签: python winforms ironpython sharpdevelop

我已经开始在#Develop中使用IronPython了,我喜欢与IronPython和Windows Forms的集成,它可以让你像创建Visual Basic或C#一样创建GUI

我的问题很简单,如何在点击时画线到PictureBox?我已经找到了关于绘制线条的代码,但我知道如何使它适应PictureBox。

这是我发现的代码: http://www.zetcode.com/tutorials/ironpythontutorial/painting/

那么,我应该把什么放入“def PictureBox1Click(self,sender,e):”?

任何帮助或指南都会得到很好的赞赏。

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子,在点击图片框时画一条线。

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
  def __init__(self):
    self.InitializeComponent()
    self.pen = System.Drawing.Pen(System.Drawing.Color.Black);

  def InitializeComponent(self):
    self._pictureBox1 = System.Windows.Forms.PictureBox()
    self._pictureBox1.BeginInit()
    self.SuspendLayout()
    # 
    # pictureBox1
    # 
    self._pictureBox1.Location = System.Drawing.Point(13, 13)
    self._pictureBox1.Name = "pictureBox1"
    self._pictureBox1.Size = System.Drawing.Size(259, 237)
    self._pictureBox1.TabIndex = 0
    self._pictureBox1.TabStop = False
    self._pictureBox1.Click += self.PictureBox1Click
    # 
    # MainForm
    # 
    self.ClientSize = System.Drawing.Size(284, 262)
    self.Controls.Add(self._pictureBox1)
    self.Name = "MainForm"
    self.Text = "PyWinForm"
    self._pictureBox1.EndInit()
    self.ResumeLayout(False)


  def PictureBox1Click(self, sender, e):
    g = self._pictureBox1.CreateGraphics()
    g.DrawLine(self.pen, 10, 10, 400, 200)