Windows的Python开发工具 python开发winform_Windows的Python开发工具


在Revit的插件中,我们能看到很多人性化的用户交互界面,上期我们介绍了Dynamo中如何使用TaskDialog,本期我们将介绍winform窗体应用虽然data shape有类似功能,奈何我的电脑用不了,所以就自己动手做了,比如下面这样用Dynamo实现在revit中玩玩俄罗斯方块:


Windows的Python开发工具 python开发winform_python窗体设置italic_02


也可以创建如下的交互窗体:


Windows的Python开发工具 python开发winform_属性设置_03


下面开始我们的第一个窗体应用:


Windows的Python开发工具 python开发winform_python窗体设置italic_04


我们使用Python创建winform窗体由于实际创建窗体控件等会有很多函数,所以推荐在类下面再定义函数。最开始的import段和我之前公开课讲dyanmo&Python是同样的道理,不再赘述,图片和图标的路径自行设置


import clr, System
##########______Author______彩虹直至黑白L__点星阁
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("System")
from System.Windows.Forms import *
from System.Drawing import *
from System.Drawing.Drawing2D import *
from System.ComponentModel import *
from cStringIO import StringIO
#以上固定格式直接套用即可
class IForm(Form):
    def __init__(self):
        self.Text = '牛侃BIM____彩虹直至黑白'
        self.Width = 340#定义窗体宽度
        self.Height = 340#定义窗体高度
        #设置窗体图标
        self.Icon = Icon("C:UsersAdministratorDesktopfavicon.ico")
        #设置窗体背景
        self.BackgroundImage =Image.FromFile("C:UsersAdministratorDesktop11111.jpg")
        #设置背景图片填充样式详细介绍在下面
        self.BackgroundImageLayout=ImageLayout.None
        #定义窗体下方状态
        self.statusbar = StatusBar()
        self.statusbar.Parent = self
        self.statusbar.Text = 'Dynamo&Winform'
        #让窗体的位置在屏幕中间
        self.CenterToScreen()  
            
Application.Run(IForm())


以下是图片填充样式的详解:

BackgroundImageLayout属性值

背景图片重复:BackgroundImageLayout

属性设置为Tile(默认)

背景图片左边显示:BackgroundImageLayout

属性设置为None

背景图片右边显示:BackgroundImageLayout

属性设置为None,同时RightToLeft属性设置为Yes

背景图片居中显示:BackgroundImageLayout

属性设置为Center

背景图片拉伸:BackgroundImageLayout

属性设置为Stretch

背景图片按比例放大到合适大小

接下来介绍如何创建下拉列表并将选择的值使用python节点输出:


Windows的Python开发工具 python开发winform_背景图片_05


import clr, System
##########______Author______彩虹直至黑白L__点星阁
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("System")

from System.Windows.Forms import *
from System.Drawing import *
from System.Drawing.Drawing2D import *#LineCap
from System.ComponentModel import *#Container
from cStringIO import StringIO
class IForm(Form):
    def __init__(self):
        self.Text = '点星阁BIM____彩虹直至黑白'
        self.Width = 340#定义窗体宽度
        self.Height = 340#定义窗体高度
        #设置窗体背景色
        self.BackColor = Color.FromArgb(236,139,183)
        #设置窗体图标
        self.Icon = Icon("C:UsersAdministratorDesktopfavicon.ico")
        #设置窗体背景
        self.BackgroundImage =Image.FromFile("C:UsersAdministratorDesktop图片3.png")
        #设置背景图片填充样式
        self.BackgroundImageLayout=ImageLayout.None
        #定义窗体下方状态
        self.statusbar = StatusBar()
        self.statusbar.Parent = self
        self.statusbar.Text = 'BIM捕鱼达人'
        #让窗体的位置在屏幕中间
        self.CenterToScreen()    
  
        #定义下拉控件
        self.label=Label(Text="下拉选择")
        self.label.Font=Font("宋体",10)
        self.label.Location=Point(40,120)
        self.label.Height=30
        self.label.Width=300
        self.Controls.Add(self.label)
        self.combo=ComboBox()
        self.combo.Size=Size(200,100)
        self.combo.Location=Point(40,150)
        for i in IN[0]:
            self.combo.Items.Add(i);
            self.Controls.Add(self.combo) 
#实例化            
b=IForm()          
Application.Run(b)

#输出选择的内容
OUT=[b.combo.SelectedItem]


如果想给窗体以及控件添加颜色代码如下


#设置窗体背景色
self.BackColor = Color.FromArgb(236,139,183)
#设置label颜色
self.label.ForeColor=Color.FromArgb(55,247,182)
#设置下拉控件颜色
self.combo.BackColor=Color.FromArgb(119,201,213)


Windows的Python开发工具 python开发winform_Windows的Python开发工具_06