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


Posted in Python onMay 30, 2015

本文实例讲述了基于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调用机器喇叭发出蜂鸣声(Beep)的方法
Mar 23 Python
Python遍历目录中的所有文件的方法
Jul 08 Python
Python 常用 PEP8 编码规范详解
Jan 22 Python
用十张图详解TensorFlow数据读取机制(附代码)
Feb 06 Python
使用django-guardian实现django-admin的行级权限控制的方法
Oct 30 Python
python issubclass 和 isinstance函数
Jul 25 Python
django自带serializers序列化返回指定字段的方法
Aug 21 Python
Python内置数据类型list各方法的性能测试过程解析
Jan 07 Python
Python enumerate内置库用法解析
Feb 24 Python
python实现吃苹果小游戏
Mar 21 Python
浅谈python锁与死锁问题
Aug 14 Python
django如何自定义manage.py管理命令
Apr 27 Python
python简单实现旋转图片的方法
May 30 #Python
Python实现控制台输入密码的方法
May 29 #Python
python删除过期文件的方法
May 29 #Python
Python的Django框架中TEMPLATES项的设置教程
May 29 #Python
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 #Python
Python fileinput模块使用实例
May 28 #Python
Python sys.argv用法实例
May 28 #Python
You might like
PHP6 先修班 JSON实例代码
2008/08/23 PHP
php 带逗号千位符数字的处理方法
2012/01/10 PHP
php开发文档 会员收费1期
2012/08/14 PHP
php利用header函数下载各种文件
2016/08/24 PHP
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
2016/12/14 PHP
php使用fullcalendar日历插件详解
2019/03/06 PHP
php+mysql+ajax 局部刷新点赞/取消点赞功能(每个账号只点赞一次)
2020/07/24 PHP
js 代码集(学习js的朋友可以看下)
2009/07/22 Javascript
javascript中运用闭包和自执行函数解决大量的全局变量问题
2010/12/30 Javascript
基于jQuery的让非HTML5浏览器支持placeholder属性的代码
2011/05/24 Javascript
获取body标签的两种方法
2011/10/13 Javascript
页面调用单个swf文件,嵌套出多个方法。
2011/11/21 Javascript
深入理解JavaScript中的箭头函数
2015/07/28 Javascript
jQuery原理系列-css选择器的简单实现
2016/06/07 Javascript
页面间固定参数,通过cookie传值的实现方法
2017/05/31 Javascript
Javascript(es2016) import和require用法和区别详解
2017/08/11 Javascript
详细分析单线程JS执行问题
2017/11/22 Javascript
Bootstrap modal只加载一次数据的解决办法(推荐)
2017/11/24 Javascript
Webpack实战加载SVG的方法
2017/12/26 Javascript
vue获取dom元素注意事项
2017/12/28 Javascript
微信小程序checkbox组件使用详解
2018/01/31 Javascript
webpack4 SCSS提取和懒加载的示例
2018/09/03 Javascript
React如何解决fetch跨域请求时session失效问题
2018/11/02 Javascript
python处理中文编码和判断编码示例
2014/02/26 Python
Python中让MySQL查询结果返回字典类型的方法
2014/08/22 Python
Python中变量交换的例子
2014/08/25 Python
python标准算法实现数组全排列的方法
2015/03/17 Python
Python实用日期时间处理方法汇总
2015/05/09 Python
Python读写ini文件的方法
2015/05/28 Python
Python脚本利用adb进行手机控制的方法
2019/07/08 Python
Python3使用xlrd、xlwt处理Excel方法数据
2020/02/28 Python
融资租赁计划书
2014/04/29 职场文书
学校与家长安全责任书
2014/07/23 职场文书
贪污受贿检讨书范文
2014/11/19 职场文书
2016年秋季运动会通讯稿
2015/11/25 职场文书
2019个人年度目标制定攻略!
2019/07/12 职场文书