wxpython使用_python中wxpython用法

Hello World, Part 2

Okay, now let's put a little more flesh on the bones of that Hello World sample to give a little better idea of what creating a wxPython application is all about. The finished application looks like these screenshots when run:

HelloWorld2-osx.png

HelloWorld2-msw.png

And here is the source code. The docstrings and the comments in the code will help you understand what it is doing.

#!/bin/python

"""

Hello World, but with more meat.

"""

import wx

class HelloFrame(wx.Frame):

"""

A Frame that says Hello World

"""

def __init__(self, *args, **kw):

# ensure the parent's __init__ is called

super(HelloFrame, self).__init__(*args, **kw)

# create a panel in the frame

pnl = wx.Panel(self)

# and put some text with a larger bold font on it

st = wx.StaticText(pnl, label="Hello World!", pos=(25,25))

font = st.GetFont()

font.PointSize += 10

font = font.Bold()

st.SetFont(font)

# create a menu bar

self.makeMenuBar()

# and a status bar

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.

"""

# Make a file menu with Hello and Exit items

fileMenu = wx.Menu()

# The "\t..." syntax defines an accelerator key that also triggers

# the same event

helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",

"Help string shown in status bar for this menu item")

fileMenu.AppendSeparator()

# When using a stock ID we don't need to specify the menu item's

# label

exitItem = fileMenu.Append(wx.ID_EXIT)

# Now a help menu for the about item

helpMenu = wx.Menu()

aboutItem = helpMenu.Append(wx.ID_ABOUT)

# Make the menu bar and add the two menus to it. The '&' defines

# that the next letter is the "mnemonic" for the menu item. On the

# platforms that support it those letters are underlined and can be

# triggered from the keyboard.

menuBar = wx.MenuBar()

menuBar.Append(fileMenu, "&File")

menuBar.Append(helpMenu, "&Help")

# Give the menu bar to the frame

self.SetMenuBar(menuBar)

# Finally, associate a handler function with the EVT_MENU event for

# each of the menu items. That means that when that menu item is

# activated then the associated handler function will be called.

self.Bind(wx.EVT_MENU, self.OnHello, helloItem)

self.Bind(wx.EVT_MENU, self

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值