Wxpython 简明教程

wxPython - Hello World

使用以下步骤构建一个显示“Hello World”消息的简单 GUI 应用程序:

A simple GUI application displaying Hello World message is built using the following steps −

  1. Import wx module.

  2. Define an object of Application class.

  3. Create a top level window as object of wx.Frame class. Caption and size parameters are given in constructor.

  4. Although other controls can be added in Frame object, their layout cannot be managed. Hence, put a Panel object into the Frame.

  5. Add a StaticText object to display ‘Hello World’ at a desired position inside the window.

  6. Activate the frame window by show() method.

  7. Enter the main event loop of Application object.

import wx

app = wx.App()
window = wx.Frame(None, title = "wxPython Frame", size = (300,200))
panel = wx.Panel(window)
label = wx.StaticText(panel, label = "Hello World", pos = (100,50))
window.Show(True)
app.MainLoop()

上述代码生成以下输出 -

The above code produces the following output −

hello world

wxFrame object 是最常用的顶级窗口。它派生自 wxWindow class 。框架是一个窗口,其大小和位置可以由用户更改。它具有标题栏和控制按钮。如果需要,还可以启用其他组件(如菜单栏、工具栏和状态栏)。wxFrame 窗口可以包含除对话框或其他框架外的任何框架。

wxFrame object is the most commonly employed top level window. It is derived from wxWindow class. A frame is a window whose size and position can be changed by the user. It has a title bar and control buttons. If required, other components like menu bar, toolbar and status bar can be enabled. A wxFrame window can contain any frame that is not a dialog or another frame.