- 积分
- 194
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-3-13
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
刚刚接触python,总找不到合适做界面的工具。今天发现pywin is very cool and suitable, so blog it here to help a programer who is confuse with it.
Python运行后目标界面如下( WindowsXp系统):
一 准备工作:
1) 安装了python3.2 or python2.7,
2) pywin 程序:pywin32-216.1.win32-py3.2.exe
3) 用vs2008 新建一个MFC dialog程序 ,在vs2008 Resource View 中画出目标MFC界面。产生如下代码:
[cpp] view plaincopy
- IDD_MFCDIALOG_DIALOG DIALOGEX 0, 0, 467, 334
- STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
- EXSTYLE WS_EX_APPWINDOW
- CAPTION "Picture Demos"
- FONT 8, "MS Shell Dlg", 0, 0, 0x1
- BEGIN
- DEFPUSHBUTTON "OK",IDOK,327,300,50,14
- PUSHBUTTON "Cancel",IDCANCEL,391,299,50,14
- LTEXT "datatype:",IDC_STATIC,15,17,42,14
- COMBOBOX IDC_CBX_DATATYPE,68,15,105,68,CBS_DROPDOWN | CBS_SORT | WS_VSCROLL | WS_TABSTOP
- LISTBOX IDC_LIST_FILE,15,41,157,145,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
- LTEXT "element:",IDC_STATIC,18,199,34,8
- LTEXT "level:",IDC_STATIC,121,200,24,8
- LISTBOX IDC_LIST_ELEMENT,17,216,67,99,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
- LISTBOX IDC_LIST_LEVEL,105,216,67,99,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
- END
二 根据第一步产生的MFC对话框信息,写Python代码:
[python] view plaincopy
- # -*- coding: utf-8 -*-
- #sqlserver to handle chapter 13
- import dbi
- from printscreen import IDC_COMBOBOX_1
-
- IDC_COMBOX_DATATYPE = 2000
- IDC_LIST_FILE = 2001
- IDC_LIST_ELEMENT = 2002
- IDC_LIST_LEVEL = 2003
-
- import win32ui
- import win32api
- import win32con
- from pywin.mfc import dialog
-
- def MakePictureServerDlgTemplate(title):
- style = win32con.DS_SETFONT | win32con.DS_MODALFRAME | win32con.DS_FIXEDSYS | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU
- cs = win32con.WS_CHILD | win32con.WS_VISIBLE
- listCs= cs | win32con.LBS_NOINTEGRALHEIGHT | win32con.WS_VSCROLL | win32con.WS_TABSTOP
- # Window frame and title
- dlg = [ [title, (0, 0, 467, 334), style, None, (8, "MS Sans Serif")], ]
-
- # ID label and text box
- dlg.append([130, "数据类型:", -1, (15,17,42,14), cs | win32con.SS_LEFT])
- s = cs | win32con.CBS_DROPDOWN | win32con.WS_VSCROLL | win32con.WS_TABSTOP
- dlg.append(['COMBOBOX', None, IDC_COMBOX_DATATYPE, (68,15,105,68), s])
- dlg.append(['LISTBOX', None, IDC_LIST_FILE, (15,41,157,145), listCs])
-
-
- dlg.append([130, "元素:", -1, (18,199,34,8), cs | win32con.SS_LEFT])
- dlg.append(['LISTBOX', None, IDC_LIST_ELEMENT, (17,216,67,99), listCs])
- dlg.append([130, "层次:", -1, (121,200,24,8), cs | win32con.SS_LEFT])
- dlg.append(['LISTBOX', None, IDC_LIST_LEVEL, (105,216,67,99), listCs])
-
- # OK/Cancel Buttons
- s = cs | win32con.WS_TABSTOP
- dlg.append([128, "OK", win32con.IDOK, (327,300,50,14), s | win32con.BS_DEFPUSHBUTTON])
- # dlg.append(["BUTTON","upload",IDC_UPLOAD ,(197, 202,50, 14), s | win32con.BS_DEFPUSHBUTTON])
- s = win32con.BS_PUSHBUTTON | s
- dlg.append([128, "Cancel", win32con.IDCANCEL, (391,299,50,14), s])
- return dlg
-
-
- class PictureServerDlg(dialog.Dialog):
-
- def __init__(self,title):
- dialog.Dialog.__init__(self,MakePictureServerDlgTemplate(title))
- self.case_name = ''
- self.case_no = ''
- self.file_name = ''
- self.window_name = ''
-
- def OnInitDialog(self):
- rc = dialog.Dialog.OnInitDialog(self)
- #init the element of the window
- self.cbxFileType = self.GetDlgItem(IDC_COMBOX_DATATYPE)
- self.cbxFileType.AddString("case 01")
- self.cbxFileType.AddString("case 02")
- self.cbxFileType.AddString("case 03")
-
-
- self.lbxFile = self.GetDlgItem(IDC_LIST_FILE)
- for i in range(1,40):
- self.lbxFile.AddString("(case_" + str(i) +")")
-
- self.lbxElement = self.GetDlgItem(IDC_LIST_ELEMENT)
- self.lbxElement.AddString("_Input")
- self.lbxElement.AddString("_Ounput")
-
- self.lbxLevel = self.GetDlgItem(IDC_LIST_LEVEL)
- self.lbxLevel.AddString('Window name')
- self.lbxLevel.AddString('Window name2')
- #print(self.combol1.GetCount())
- return rc
-
-
- if __name__ == "__main__":
- import sys
- title = 'Login'
- picDlg = PictureServerDlg("")
- picDlg.DoModal()
友情提示:
pywin32安装目录\pythonwin\pywin\Demos 下有些做win32界面程序的一些例子,有兴趣有读者可以多尝试尝试。
|
|