基于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模拟登陆Tom邮箱示例分享
Jan 13 Python
python通过定义一个类实例作为ftp回调方法
May 04 Python
python结合API实现即时天气信息
Jan 19 Python
Python虚拟环境项目实例
Nov 20 Python
Python基于OpenCV实现视频的人脸检测
Jan 23 Python
python将文本分每两行一组并保存到文件
Mar 19 Python
pytorch程序异常后删除占用的显存操作
Jan 13 Python
利用Vscode进行Python开发环境配置的步骤
Jun 22 Python
python线程里哪种模块比较适合
Aug 02 Python
Django用户认证系统如何实现自定义
Nov 12 Python
利用python实现汉诺塔游戏
Mar 01 Python
Python中三种花式打印的示例详解
Mar 19 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
php fputcsv命令 写csv文件遇到的小问题(多维数组连接符)
2011/05/24 PHP
PHP异步进程助手async-helper
2018/02/05 PHP
javascript入门·对象属性方法大总结
2007/10/01 Javascript
prototype Element学习笔记(Element篇三)
2008/10/26 Javascript
js 验证密码强弱的小例子
2013/03/21 Javascript
jQuery固定浮动侧边栏实现思路及代码
2014/09/28 Javascript
JS实现文字向下滚动完整实例
2015/02/06 Javascript
JavaScript中Function()函数的使用教程
2015/06/04 Javascript
javascript实现密码验证
2015/11/10 Javascript
详解JavaScript UTC时间转换方法
2016/01/07 Javascript
Google 地图获取API Key详细教程
2016/08/06 Javascript
AngularJs验证重复密码的方法(两种)
2016/11/25 Javascript
Node.js复制文件的方法示例
2016/12/29 Javascript
jQuery自动或手动图片切换效果
2017/10/11 jQuery
vue 使某个组件不被 keep-alive 缓存的方法
2018/09/21 Javascript
vue中h5端打开app(判断是安卓还是苹果)
2021/02/26 Vue.js
Python实现从百度API获取天气的方法
2015/03/11 Python
用python找出那些被“标记”的照片
2017/04/20 Python
python的多重继承的理解
2017/08/06 Python
Python计算一个给定时间点前一个月和后一个月第一天的方法
2018/05/29 Python
对python的bytes类型数据split分割切片方法
2018/12/04 Python
python使用matplotlib画柱状图、散点图
2019/03/18 Python
Python微信操控itchat的方法
2019/05/31 Python
vscode调试django项目的方法
2020/08/06 Python
Python页面加载的等待方式总结
2021/02/28 Python
CSS3下的渐变文字效果实现示例
2018/03/02 HTML / CSS
River Island美国官网:英国高街时尚品牌
2018/09/04 全球购物
少先队学雷锋活动月总结
2014/03/09 职场文书
我的中国梦演讲稿800字
2014/08/19 职场文书
特此通知格式
2015/04/27 职场文书
2015年乡镇民政工作总结
2015/05/13 职场文书
CSS filter 有什么神奇用途
2021/05/25 HTML / CSS
vue-cropper插件实现图片截取上传组件封装
2021/05/27 Vue.js
如何设置多台电脑共享打印机?多台电脑共享打印机的方法
2022/04/08 数码科技
Python实现Matplotlib,Seaborn动态数据图
2022/05/06 Python
Python实现双向链表
2022/05/25 Python