0 前言
不定期学习wxPython,随学随写,不定期更新,争取一周至少一更。 wxPython是一个经典的,基于Python的GUI界面。
1 安装
pip
install wxPython
2 Hello World
import wx
app
= wx
.App
()
frm
= wx
.Frame
(None, title
="Hello World")
frm
.Show
()
app
.MainLoop
()
import wx
; a
=wx
.App
(); wx
.Frame
(None, title
="Hello World").Show
(); a
.MainLoop
()
"""
Hello World, but with more meat.
"""
import wx
class HelloFrame(wx
.Frame
):
"""
A Frame that says Hello World
"""
def __init__(self
, *args
, **kw
):
super(HelloFrame
, self
).__init__
(*args
, **kw
)
pnl
= wx
.Panel
(self
)
st
= wx
.StaticText
(pnl
, label
="Hello World!")
font
= st
.GetFont
()
font
.PointSize
+= 10
font
= font
.Bold
()
st
.SetFont
(font
)
sizer
= wx
.BoxSizer
(wx
.VERTICAL
)
sizer
.Add
(st
, wx
.SizerFlags
().Border
(wx
.TOP
|wx
.LEFT
, 25))
pnl
.SetSizer
(sizer
)
self
.makeMenuBar
()
self
.CreateStatusBar
()
self
.SetStatusText
("Welcome to wxPython!")
def makeMenuBar(self
):
"""
A menu bar is composed of menus, which are composed of menu items.
This method builds a set of menus and binds handlers to be called
when the menu item is selected.
"""
fileMenu
= wx
.Menu
()
helloItem
= fileMenu
.Append
(-1, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item")
fileMenu
.AppendSeparator
()
exitItem
= fileMenu
.Append
(wx
.ID_EXIT
)
helpMenu
= wx
.Menu
()
aboutItem
= helpMenu
.Append
(wx
.ID_ABOUT
)
menuBar
= wx
.MenuBar
()
menuBar
.Append
(fileMenu
, "&File")
menuBar
.Append
(helpMenu
, "&Help")
self
.SetMenuBar
(menuBar
)
self
.Bind
(wx
.EVT_MENU
, self
.OnHello
, helloItem
)
self
.Bind
(wx
.EVT_MENU
, self
.OnExit
, exitItem
)
self
.Bind
(wx
.EVT_MENU
, self
.OnAbout
, aboutItem
)
def OnExit(self
, event
):
"""Close the frame, terminating the application."""
self
.Close
(True)
def OnHello(self
, event
):
"""Say hello to the user."""
wx
.MessageBox
("Hello again from wxPython")
def OnAbout(self
, event
):
"""Display an About Dialog"""
wx
.MessageBox
("This is a wxPython Hello World sample",
"About Hello World 2",
wx
.OK
|wx
.ICON_INFORMATION
)
if __name__
== '__main__':
app
= wx
.App
()
frm
= HelloFrame
(None, title
='Hello World 2')
frm
.Show
()
app
.MainLoop
()
3 参考
wxPython官方网站手册
转载请注明原文地址:https://ipadbbs.8miu.com/read-59429.html