所有的程序设计语言的第一课基本上都会是Hello World这个简单的小程序,相同的是Hello World;不同的也是Hello World!
程序元代码:
以下的Hello World程序使用wxPython展示了wxPython的Logo详细参看以下代码:
逐行说明(与wx入门相同的内容不做讲解):
1. Frame多了一个显示image的控件
def __init__(self, image, parent = None, id= -1, pos = wx.DefaultPosition, title = "Hello, wxPython!"):
"""Create a Frame instance and display image."""
temp = image.ConvertToBitmap()
size = temp.GetWidth(), temp.GetHeight()
wx.Frame.__init__(self, parent, id, title, pos, size)
self.bmp = wx.StaticBitmap(parent = self, bitmap = temp)
传入一个image对象,
将这个对象转换为Bitmap,
获得image的初始大小,设置为Frame的初始大小
使用StaticBitmap空间显示这个图片
2. App的OnInit方法
def OnInit(self):
image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
self.frame = Frame(image)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
获得一个image对象,将它传递给Frame
3. 定义了一个main方法,参看以下代码:
def main():
app = App()
app.MainLoop()
4. 使用常规的方法调用main方法
if __name__ == '__main__':
main()
运行效果参看以下图示: