基于wxpython实现的windows GUI程序实例

yipeiwu_com5年前Python基础

本文实例讲述了基于wxpython实现的windows GUI程序。分享给大家供大家参考。具体如下:

# using a wx.Frame, wx.MenuBar, wx.Menu, wx.Panel, wx.StaticText, wx.Button, 
# and a wx.BoxSizer to show a rudimentary wxPython Windows GUI application
# wxPython package from: http://prdownloads.sourceforge.net/wxpython/
# I downloaded: wxPython2.5-win32-ansi-2.5.3.1-py23.exe
# if you have not already done so install the Python compiler first
# I used Python-2.3.4.exe (the Windows installer package for Python23) 
# from http://www.python.org/2.3.4/
# tested with Python23   vegaseat   24jan2005
import wx
class Frame1(wx.Frame):
  # create a simple windows frame (sometimes called form)
  # pos=(ulcX,ulcY) size=(width,height) in pixels
  def __init__(self, parent, title):
    wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 250))
    # create a menubar at the top of the user frame
    menuBar = wx.MenuBar()
    # create a menu ... 
    menu = wx.Menu()
    # ... add an item to the menu
    # \tAlt-X creates an accelerator for Exit (Alt + x keys)
    # the third parameter is an optional hint that shows up in 
    # the statusbar when the cursor moves across this menu item
    menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit the program")
    # bind the menu event to an event handler, share QuitBtn event
    self.Bind(wx.EVT_MENU, self.OnQuitButton, id=wx.ID_EXIT)
    # put the menu on the menubar
    menuBar.Append(menu, "&File")
    self.SetMenuBar(menuBar)
    # create a status bar at the bottom of the frame
    self.CreateStatusBar()
    # now create a panel (between menubar and statusbar) ...
    panel = wx.Panel(self)
    # ... put some controls on the panel
    text = wx.StaticText(panel, -1, "Hello World!")
    text.SetFont(wx.Font(24, wx.SCRIPT, wx.NORMAL, wx.BOLD))
    text.SetSize(text.GetBestSize())
    quitBtn = wx.Button(panel, -1, "Quit")
    messBtn = wx.Button(panel, -1, "Message")
    # bind the button events to event handlers
    self.Bind(wx.EVT_BUTTON, self.OnQuitButton, quitBtn)
    self.Bind(wx.EVT_BUTTON, self.OnMessButton, messBtn)
    # use a sizer to layout the controls, stacked vertically
    # with a 10 pixel border around each
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(text, 0, wx.ALL, 10)
    sizer.Add(quitBtn, 0, wx.ALL, 10)
    sizer.Add(messBtn, 0, wx.ALL, 10)
    panel.SetSizer(sizer)
    panel.Layout()
  def OnQuitButton(self, evt):
    # event handler for the Quit button click or Exit menu item
    print "See you later alligator! (goes to stdout window)"
    wx.Sleep(1)  # 1 second to look at message
    self.Close()
  def OnMessButton(self, evt):
    # event handler for the Message button click
    self.SetStatusText('101 Different Ways to Spell "Spam"')
class wxPyApp(wx.App):
  def OnInit(self):
    # set the title too
    frame = Frame1(None, "wxPython GUI 2")
    self.SetTopWindow(frame)
    frame.Show(True)
    return True
# get it going ...
app = wxPyApp(redirect=True)
app.MainLoop()

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

相关文章

Python学习小技巧之利用字典的默认行为

本文介绍的是关于Python利用字典的默认行为的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 典型代码1: from collections import defaul...

Python对字符串实现去重操作的方法示例

Python对字符串实现去重操作的方法示例

前言 最近在工作经常会碰到对字符串进行去重操作,下面就给大家列出用Python如何处理的,话不多说了,来一起看看详细的介绍吧。 比如说,要拿下面的字符传去掉重复的AA, A(B,C)...

python按时间排序目录下的文件实现方法

废话不多说,直接上代码: python文件夹遍历,文件操作,获取文件修改创建时间可以去网上参考其他文章。 如: os.path.getmtime() 函数是获取文件最后修改时间 o...

Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

本文实例讲述了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)。分享给大家供大家参考,具体如下: demo.py(类方法,@classm...

使用python进行广告点击率的预测的实现

使用python进行广告点击率的预测的实现

当前在线广告服务中,广告的点击率(CTR)是评估广告效果的一个非常重要的指标。 因此,点击率预测系统是必不可少的,并广泛用于赞助搜索和实时出价。那么如何计算广告的点击率呢? 广告的点击率...