天天看點

在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   ,如需轉載請自行聯系原作者