Home  >  Article  >  Backend Development  >  python之wxPython应用实例

python之wxPython应用实例

WBOY
WBOYOriginal
2016-06-16 08:41:423205browse

本文实例讲述了python之wxPython的使用方法,分享给大家供大家参考。具体方法如下:

先来看看效果,这里加载一张图片:

代码如下:

#!/usr/bin/env python 
"""hello wxPython program""" 
 
import wx 
 
class Frame(wx.Frame): #wxPrame subclass 
  """Frame class that display a image""" 
  def __init__(self, image, parent=None, id=-1, 
         pos=wx.DefaultPosition, 
         title="Hello ,wxPython"):  #3 
  #create a Frame instance and display a 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) 
    pass 
   
class App(wx.App): #5 wx.App subclass 
  """Application class""" 
   
  def OnInit(self): 
    image = wx.Image('wxPython.jpg',wx.BITMAP_TYPE_JPEG) 
    self.frame = Frame(image) 
     
    self.frame.Show(True) 
    self.SetTopWindow(self.frame) 
    return True 
   
def main(): 
  app = App() 
  app.MainLoop() 
   
if __name__ == "__main__": 
  main() 

总结使用wxPython的4个步骤:

1. 导入wxPython包
2. 子类化应用程序类
3. 定义一个应用程序的初始化方法
4. 创建应用程序类(第2步中创建的子类的)的实例
5. 进入第4步中创建的这个应用程序类实例的主事件循环

注:wxPython.jpg就是这张阿甘同学的图片,要和程序放在同一目录下.

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn