基于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创建文件和追加文件内容实例
Oct 21 Python
Python中用memcached来减少数据库查询次数的教程
Apr 07 Python
在Python下使用Txt2Html实现网页过滤代理的教程
Apr 11 Python
浅谈终端直接执行py文件,不需要python命令
Jan 23 Python
使用Python对Csv文件操作实例代码
May 12 Python
python输入错误密码用户锁定实现方法
Nov 27 Python
判断python字典中key是否存在的两种方法
Aug 10 Python
对Pandas MultiIndex(多重索引)详解
Nov 16 Python
Python列表list操作相关知识小结
Jan 29 Python
解决django中form表单设置action后无法回到原页面的问题
Mar 13 Python
python自定义函数def的应用详解
Jun 03 Python
用Python提取PDF表格的方法
Apr 11 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
3.从实例开始
2006/10/09 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
2008/06/05 PHP
php无序树实现方法
2015/07/28 PHP
PHP模板引擎Smarty内建函数section,sectionelse用法详解
2016/04/11 PHP
php实现留言板功能
2017/03/05 PHP
jquery插件制作 手风琴Panel效果实现
2012/08/17 Javascript
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
JQUERY dialog的用法详细解析
2013/12/19 Javascript
jquery 删除字符串最后一个字符的方法解析
2014/02/11 Javascript
JavaScript_ECMA5数组新特性详解
2016/06/12 Javascript
原生js实现秒表计时器功能
2017/02/16 Javascript
详解React Native开源时间日期选择器组件(react-native-datetime)
2017/09/13 Javascript
JS实现倒计时图文效果
2018/11/17 Javascript
JS实现的杨辉三角【帕斯卡三角形】算法示例
2019/02/26 Javascript
Python实现的求解最小公倍数算法示例
2018/05/03 Python
使用python获取(宜宾市地震信息)地震信息
2019/06/20 Python
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
2019/08/19 Python
3种python调用其他脚本的方法
2020/01/06 Python
python3实现名片管理系统(控制台版)
2020/11/29 Python
Python 中 sorted 如何自定义比较逻辑
2021/02/02 Python
浅析HTML5中header标签的用法
2016/06/24 HTML / CSS
Pretty Green美国:英式摇滚服饰风格代表品牌之一
2019/01/23 全球购物
Shopee越南:东南亚与台湾电商平台
2019/02/03 全球购物
Columbia Sportswear法国官网:全球户外品牌
2020/09/25 全球购物
3.15国际消费者权益日主题活动活动总结
2014/03/16 职场文书
我心目中的好老师活动方案
2014/08/19 职场文书
共青团员自我评价范文
2014/09/14 职场文书
班子个人四风问题整改措施
2014/10/04 职场文书
刑事附带民事上诉状
2015/05/23 职场文书
小学六一主持词开场白
2015/05/28 职场文书
趣味运动会新闻稿
2015/07/17 职场文书
村党总支部公开承诺书2016
2016/03/25 职场文书
一封真诚的自荐信帮你赢得机会
2019/05/07 职场文书
手把手教你制定暑期学习计划,让你度过充实的暑假
2019/08/22 职场文书
python 三边测量定位的实现代码
2021/04/22 Python
游戏《铁拳》动画化!2022年年内播出
2022/03/21 日漫