天天看点

在Python脚本中使用Delphi控件

   Python设计GUI界面的时候,有强大的WxPython,PyQt等,在Windows下也有一个PyWin32的,不过那个是纯SDK模式开发的,用起来不太方便,这两天通过P4D研究了一下Delphi和Python交互的模式,于是通过P4D写了一个模块,通过本模块,就能实现在Python脚本中使用Delphi的控件。目前转化了几个基本的控件支持,增加了几个常规函数,用来作为一般的需求。

比如使用本模块创建一个简单脚本的记事本功能:

<a></a>

#-*-coding: gbk-*-

from DxVcl import *

class MainForm(Form):

def __init__(self, Owner):

self.Caption = "A Delphi Form..." 

self.SetBounds(10, 10, 500, 400)

self.TopPanel = Panel(self)

self.TopPanel.Parent = self

self.TopPanel.Align = 1

self.Btn = Button(self)

self.Btn.SetProps(Parent=self.TopPanel, Caption="打开")

self.Btn.SetBounds(0, 3, 100, 25) 

self.OnClose = self.MainFormClose

self.Btn.OnClick = self.BtnClick

self.BtnSave = Button(self)

self.BtnSave.SetProps(Parent=self.TopPanel, Caption="保存")

self.BtnSave.SetBounds(110, 3, 100, 25) 

self.BtnSave.OnClick = self.BtnSaveClick

self.Mo = Memo(self)

self.Mo.Parent = self

self.Mo.Align = 5 

def BtnClick(self,Sender):

#self.Caption = "测试不得闲"

#ShowMessage(StrToHex(self.Caption))

#ShowMessage(''.join(['16进制转换回来:',HexToStr(StrToHex(self.Caption))]))

#ShowMessage(IntToStr(234234)) 

fileName = OpenDialog('文本文件|*.txt','打开文档')

if fileName:

self.Caption = '已经打开'+fileName.encode('gbk')

self.Mo.Lines.LoadFromFile(fileName.encode('gbk'))

def BtnSaveClick(self,Sender):

if Ask(self.Handle,'确定要保存'):

fileName = SaveDialog('文本文件|*.txt','保存文档')

self.Mo.Lines.SaveToFile(fileName.encode('gbk'))

def MainFormClose(self, Sender, Action):

Action.Value = caFree

class DxLogForm(Form):

def __init__(self,Owner):

self.Caption = '不得闲测试窗体'

self.Position = 5

self.BorderStyle = 3

self.Width = 234

self.Height = 150

self.lb1 = Label(self)

self.lb1.SetProps(Parent = self,Caption = '用户')

self.lb1.SetBounds(40,16,28,13)

self.EdtUser = Edit(self)

self.EdtUser.SetProps(Parent = self,Text = 'dxsoft')

self.EdtUser.SetBounds(77,13,121,21)

self.lb2 = Label(self)

self.lb2.SetProps(Parent = self,Caption = '密码')

self.lb2.SetBounds(40,48,28,13)

self.EdtPwd = Edit(self)

self.EdtPwd.SetProps(Parent = self,Text = 'dxsoft',PasswordChar='*')

self.EdtPwd.SetBounds(77,45,121,21)

self.BtnLog = Button(self)

self.BtnLog.SetProps(Parent = self,Caption = '登录')

self.BtnLog.SetBounds(25,80,75,25)

self.BtnLog.OnClick = self.BtnClick

self.BtnCancel = Button(self)

self.BtnCancel.SetProps(Parent = self,Caption = '取消')

self.BtnCancel.SetBounds(141,80,75,25)

self.BtnCancel.OnClick = self.BtnCancelClick

if self.EdtPwd.Text.decode('gbk') == '123':

ShowMessage('登录成功,打开主窗体') 

#self.visible = false 

#打开另一个窗体

self.ModalResult = mrok

else: 

ShowMessage('密码为123')

def BtnCancelClick(self,Sender):

self.Close()

def dolog():

LogFrm = DxLogForm(None)

m = LogFrm.ShowModal() == mrok

LogFrm.Free() 

return m

def main():

FreeConsole()

if dolog():

Application.Initialize()

Application.Title = 'TestApp'

f = MainForm(Application)

f.Show() 

Application.Run()

else:

ShowMessage('用户取消了登录')

if __name__=='__main__':

main()

程序运行这个脚本双击运行之后的效果就是

本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2011/04/03/2004490.html   ,如需转载请自行联系原作者