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,但理由卻不多。

 

運行程序應該是這個畫面:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章