wxPython是python可视化编程中的一个很好的模块,一以下的代码主要讲述工具栏、状态栏、菜单、菜单事件的实现(可参考:http://www.czug.org/python/wxpythoninaction/):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
wx
import
wx.py.images
class
ToolbarFrame(wx.Frame):
def
__init__(
self
, parent,
id
):
wx.Frame.__init__(
self
, parent,
id
,
'Toolbars'
, size
=
(
600
,
400
))
panel
=
wx.Panel(
self
)
panel.SetBackgroundColour(
'White'
)
#创建状态栏
statusBar
=
self
.CreateStatusBar()
#创建工具栏
toolbar
=
self
.CreateToolBar()
#增加一个工具
toolbar.AddSimpleTool(wx.NewId(), wx.py.images.getPyBitmap(),
"New"
,
"Long help for 'New'"
)
toolbar.AddSimpleTool(wx.NewId(), wx.py.images.getPyBitmap(),
"Edit"
,
"Long help for 'Edit'"
)
#准备显示
toolbar.Realize()
#创建菜单
menuBar
=
wx.MenuBar()
menu1
=
wx.Menu()
menuBar.Append(menu1, u
"&文件"
)
#菜单项目1
self
.close
=
menu1.Append(wx.NewId(), u
"退出(&X)"
, "")
menu2
=
wx.Menu()
#菜单内容&表示随后的字符为热键,参数3为在状态栏上显示的菜单项说明
self
.Copy
=
menu2.Append(wx.NewId(),
"&Copy"
,
"Copy in status bar"
)
self
.Cut
=
menu2.Append(wx.NewId(),
"C&ut"
, "")
self
.Paste
=
menu2.Append(wx.NewId(),
"Paste"
, "")
menu2.AppendSeparator()
self
.Options
=
menu2.Append(wx.NewId(),
"&Options..."
,
"Display Options"
)
self
.Edit
=
menuBar.Append(menu2,
"&Edit"
)
self
.SetMenuBar(menuBar)
#调用菜单下拉的退出事件
self
.Bind(wx.EVT_MENU,
self
.OnClose,
self
.close)
def
OnClose(
self
,event):
#退出事件
self
.Close()
if
__name__
=
=
'__main__'
:
app
=
wx.PySimpleApp()
frame
=
ToolbarFrame(parent
=
None
,
id
=
-
1
)
frame.Show()
app.MainLoop()
|
本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1209303,如需转载请自行联系原作者