1.第一个程序: "Hello, World"

A First Application: "Hello, World"

As is traditional, we are first going to write a Small "Hello, world" application. Here is the code:

按传统:我们将首先编写一个小的“Hello,world”应用程序。这里的代码是

import wx
app = wx.App(False)
frame = wx.Frame(None,wx.ID_ANY,"Hello World")
frame.Show()
app.MainLoop()

 

解释:

app = wx.App(False)

Every wxPython app is an instance of wx.App. For most simple applications you can usewx.App as is. When you get to more complex applications you may need to extend thewx.App class. The "False" parameter means "don't redirect stdout and stderr to a window".

每一个wxPython是一个wx.App实例。对于大多数简单的应用程序,您可以使用wxApp。当你进行复杂应用程序时你需要从wx.App继承。

"False"参数的意思是"重定向<标准输出><标准错误>到一个窗口"

wx.Frame(None,wx.ID_ANY,"Hello")

A wx.Frame is a top-level window. The syntax is x.Frame(Parent, Id, Title). Most of the constructors have this shape (a parent object, followed by an Id). In this example, we useNone for "no parent" and wx.ID_ANY to have wxWidgets pick an id for us.

一个wx.Frame是一个顶级窗口。语法是:x.Frame(Parent,Id,Title)。大部分的构造方法都是(一个parent,跟着Id)。

这个例子里,我们用None给"no parent"和wx.ID_ANY方法来拥有wxWidgets

frame.Show(True)

We make a frame visible by "showing" it. 我们使用frame可见

app.MainLoop()

Finally, we start the application's MainLoop whose role is to handle the events. 最后,我们开始应用程序的MainLoop语句,其角色是处理事件。

注意:你通常应该使用wx.Widgets提供的wx.ID_ANY或者其它标准的ID,你也可以创建自己的ID,但理由却不多。

 

运行程序应该是这个画面:

 

你可能感兴趣的:(1.第一个程序: "Hello, World")