wxpython打包成exe_WxPython 做界面以及生成EXE文件

看到以下三个窗口了么?

一个MDI多窗口、一个命令行、一个Frame,如果用MFC做你会觉得如何呢?

这些全部是Python+WxPython做的,代码量极少,很快很好用。

WxPython做界面真的是太轻松了。。这简直就是4GL的语言。。快速开发的绝好助手。

有人说Python只适合开发人员,不便于给客户演示,要是要用的话还得装Python、WxPython。。

其实只要用Py2exe这个东西就可以了,Python可以很方便的变成一个可执行文件,当然体积稍微大点。。

MFC写并静态库连接可能2-3M,Wx写可能2-3M,WxPython+Py2exe就是16M左右了。当然只要能用多点硬盘空间又如何呢.Python的快速开发能力真的是非常惊人。

因此我决定做界面快速开发用Python,实际开发用wxwidgets 和C++

其实核心模块我们可以用C++写成DLL给Python调用。

Python和C++典型的快速开发模型

1、需求分析

2、核心模块设计

3、快速开发界面,连接核心模块

4、客户体验、修改

5、实际开发过程

6、交付验收。

但是有一点要注意Python在数据库应用的快速开发上有些不足,没有PB和C#的快速。因此我们要扬长避短。数据库快速开发用PB,C#,界面较多样式复杂的用Python+WxPython。

还有一点要注意,Python既然可以调用C++那么就可以对C++模块进行黑盒测试。利用Python强大的快速开发能力,我们可以很快的做出相关模块的测试框架。

代码如下

import wx

import wx.aui

#########MDI窗口的代码 开始

class ParentFrame(wx.aui.AuiMDIParentFrame):

def

__init__(self, parent):

wx.aui.AuiMDIParentFrame.__init__(self, parent, -1,

title="AuiMDIParentFrame",

size=(640,480),

style=wx.DEFAULT_FRAME_STYLE)

self.count = 0

mb = self.MakeMenuBar()

self.SetMenuBar(mb)

self.CreateStatusBar()

def

MakeMenuBar(self):

mb = wx.MenuBar()

menu = wx.Menu()

item = menu.Append(-1, "New child window\tCtrl-N")

self.Bind(wx.EVT_MENU, self.OnNewChild, item)

item = menu.Append(-1, "Close parent")

self.Bind(wx.EVT_MENU, self.OnDoClose, item)

mb.Append(menu, "&File")

return mb

def

OnNewChild(self, evt):

self.count += 1

child = ChildFrame(self, self.count)

child.Show()

def

OnDoClose(self, evt):

self.Close()

#----------------------------------------------------------------------

class ChildFrame(wx.aui.AuiMDIChildFrame):

def

__init__(self, parent, count):

wx.aui.AuiMDIChildFrame.__init__(self, parent, -1,

title="Child: %d" % count)

mb = parent.MakeMenuBar()

menu = wx.Menu()

item = menu.Append(-1, "This is child %d's menu" % count)

mb.Append(menu, "&Child")

self.SetMenuBar(mb)

p = wx.Panel(self)

wx.StaticText(p, -1, "This is child %d" % count, (10,10))

p.SetBackgroundColour('light blue')

sizer = wx.BoxSizer()

sizer.Add(p, 1, wx.EXPAND)

self.SetSizer(sizer)

wx.CallAfter(self.Layout)

#########MDI窗口的代码 结束

#########Frame的代码 开始

class MyFrame(wx.Frame):

"""

This is

MyFrame. It just shows a few controls on a

wxPanel,

and has a

simple menu.

"""

def

__init__(self, parent, title):

wx.Frame.__init__(self, parent, -1, title,

pos=(150, 150), size=(350, 200))

# Create the menubar

menuBar = wx.MenuBar()

# and a menu

menu = wx.Menu()

# add an item to the menu, using \tKeyName automatically

# creates an accelerator, the third param is some help text

# that will show up in the statusbar

menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this

simple sample")

# bind the menu event to an event handler

self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)

# and put the menu on the menubar

menuBar.Append(menu, "&File")

self.SetMenuBar(menuBar)

self.CreateStatusBar()

# Now create the Panel to put the other controls on.

panel = wx.Panel(self)

# and a few controls

text = wx.StaticText(panel, -1, "Hello World!")

text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))

text.SetSize(text.GetBestSize())

btn = wx.Button(panel, -1, "Close")

funbtn = wx.Button(panel, -1, "Just for fun...")

# bind the button events to handlers

self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)

self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)

# Use a sizer to layout the controls, stacked vertically and

with

# a 10 pixel border around each

sizer = wx.BoxSizer(wx.VERTICAL)

sizer.Add(text, 0, wx.ALL, 10)

sizer.Add(btn, 0, wx.ALL, 10)

sizer.Add(funbtn, 0, wx.ALL, 10)

panel.SetSizer(sizer)

panel.Layout()

def

OnTimeToClose(self, evt):

"""Event handler for the button click."""

print "See ya later!"

self.Close()

def

OnFunButton(self, evt):

"""Event handler for the button click."""

pf=ParentFrame(self)

pf.Show()

#########Frame的代码 结束

#########仿照CMD的代码 开始

class MyApp(wx.App):

def

OnInit(self):

frame = MyFrame(None, "Simple wxPython App")

self.SetTopWindow(frame)

print "Print statements go to this stdout window by default."

frame.Show(True)

return True

#########仿照CMD的代码 结束

#########Python程序的入口代码

app = MyApp(redirect=True)

app.MainLoop()

相信如果你有C++、Java、C#、PHP。。。。任何一种语言的基础你就可以看懂以上代码了。多简单,多实用的Python。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值