第一范文网 - 专业文章范例文档资料分享平台

PythonWindowsTutorial

来源:用户分享 时间:2025/6/1 6:42:13 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Using Python To Harness Windows

Advanced ? ? ?

map from one accounts structure to another analyse and trace cash flows Multidimensional analysis

5.10 What we’d like…

Page 21 of 71

6795.doc

Using Python To Harness Windows

5.11 Design Patterns for the COM Server

COM servers and Python apps handle some arg types differently… ? Unicode String Handling – Gotcha Number One! (hopefully goes in 1.6) # our ordinary save method for use from Python def save(self, filename): f = open(filename,'wb') cPickle.dump(self.__journal,f) f.close() # what we would need for use from COM def save(self, unicode_filename): # convert it to a python string: python_filename = str(unicode_filename) f = open(python_filename,'wb') cPickle.dump(self.__journal,f) f.close() ?

…so a single class not the best design for real apps. Others options: ? ? ?

We go for option 3: Delegate. Keeps our Python package pure and portable.

Startup Code: # comservers.py – to be expanded class COMBookSet: _reg_clsid_ = '{38CB8241-D698-11D2-B806-0060974AB8A9}' _reg_progid_ = 'Doubletalk.BookServer' _public_methods_ = ['double'] def __init__(self): self.__BookSet = doubletalk.bookset.BookSet() def double(self, arg): # trivial test function to check it is alive return arg * 2 if __name__ == '__main__': win32com.server.register.UseCommandLine(COMBookSet)

COM Base Class, Python Server Pure Python Base Class, COM Subclass COM interface, Python Delegate Wrap/Unwrap subobjects

Page 22 of 71

6795.doc

Using Python To Harness Windows

5.12 Visual Basic GUI Startup Code

Public BookServer As Object Private Sub MDIForm_Load() InitCOMServer frmJournal.Show End Sub Private Sub MDIForm_Unload(Cancel As Integer) CloseCOMServer End Sub Sub InitCOMServer() 'called when the program starts On Error GoTo InitCOMServer_error Set BookServer = CreateObject(\ Exit Sub InitCOMServer_error: Dim msg As String msg = \BookServer.\ \the Python \ \ MsgBox msg End End Sub Sub CloseCOMServer() Set BookServer = Nothing End Sub Sub TestCOMServer() 'just to check it is alive Dim hopefully_four As Integer hopefully_four = BookServer.Double(2) MsgBox \alive\End Sub Private Sub mnuToolsTestServer_Click() 'this helps establish if the COM server is alive 'using a minimal diagnostic function in the modMain module TestCOMServer End Sub With a little luck…

Page 23 of 71

6795.doc

Using Python To Harness Windows

5.13 Our first view – The Journal

Goal:

Date-Ordered List of Transactions

Python Code Needed:

# more methods for COMBookSet – must be named in _public_methods_ def load(self, filename): self.__BookSet.load(str(filename)) def count(self): # return number of transactions return len(self.__BookSet) def getTransactionString(self, index): return self.__BookSet[index].asString() Visual Basic Code – File / Open handler

Private Sub mnuFileOpen_Click() Dim sFile As String With dlgCommonDialog .DialogTitle = \ .CancelError = False 'ToDo: set the flags and attributes of the common dialog control .Filter = \ .ShowOpen If Len(.FileName) = 0 Then Exit Sub End If sFile = .FileName End With BookServer.Load sFile 'display something helpful in the Journal caption frmJournal.Caption = sFile & \Transactions\End Sub

Page 24 of 71

6795.doc

搜索更多关于: PythonWindowsTutorial 的文档
PythonWindowsTutorial.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8kzf631ezc5v45r563hb_6.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top