基于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 相关文章推荐
30秒轻松实现TensorFlow物体检测
Mar 14 Python
python实现列表中由数值查到索引的方法
Jun 27 Python
python3使用QQ邮箱发送邮件
May 20 Python
解决python中使用PYQT时中文乱码问题
Jun 17 Python
用django设置session过期时间的方法解析
Aug 05 Python
Django实现分页显示效果
Oct 31 Python
完美解决Django2.0中models下的ForeignKey()问题
May 19 Python
Python生成pdf目录书签的实例方法
Oct 29 Python
python dir函数快速掌握用法技巧
Dec 09 Python
Pytorch 统计模型参数量的操作 param.numel()
May 13 Python
django学习之ajax post传参的2种格式实例
May 14 Python
详解Python描述符的工作原理
Jun 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
elgg 获取文件图标地址的方法
2010/03/20 PHP
PHP文件读写操作之文件写入代码
2011/01/13 PHP
控制PHP的输出:缓存并压缩动态页面
2013/06/11 PHP
微信随机生成红包金额算法php版
2016/07/21 PHP
PHP设置images目录不充许http访问的方法
2016/11/01 PHP
ThinkPHP3.1.2 使用cli命令行模式运行的方法
2020/04/14 PHP
js 操作css实现代码
2009/06/11 Javascript
基于jquery的一个简单的脚本验证插件
2010/04/05 Javascript
仿jQuery的siblings效果的js代码
2011/08/09 Javascript
将文本输入框内容加入表中的js代码
2013/08/18 Javascript
JavaScript调用ajax获取文本文件内容实现代码
2014/03/28 Javascript
javascript实现禁止鼠标滚轮事件
2015/07/24 Javascript
浅谈JavaScript的Polymer框架中的事件绑定
2015/07/29 Javascript
通过Jquery.cookie.js实现展示浏览网页的历史记录超管用
2015/10/23 Javascript
jQuery UI库中dialog对话框功能使用全解析
2016/04/23 Javascript
JS实现环形进度条(从0到100%)效果
2016/07/05 Javascript
jquery动态添加文本并获取值的方法
2016/10/12 Javascript
Vue.directive自定义指令的使用详解
2017/03/10 Javascript
angularjs指令之绑定策略(@、=、&)
2017/04/13 Javascript
ExtJs的Ext.Ajax.request实现waitMsg等待提示效果
2017/06/14 Javascript
css和js实现弹出登录居中界面完整代码
2017/11/26 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
JavaScript中Dom操作实例详解
2019/07/08 Javascript
JS动态显示倒计时效果
2019/12/12 Javascript
vue-router 控制路由权限的实现
2020/09/24 Javascript
Python 忽略warning的输出方法
2018/10/18 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2019/11/21 Python
Python如何定义有默认参数的函数
2020/08/10 Python
python3列表删除大量重复元素remove()方法的问题详解
2021/01/04 Python
HTML5拖拽的简单实例
2016/05/30 HTML / CSS
linux面试题参考答案(11)
2012/05/01 面试题
工作中的自我评价如何写好
2013/10/28 职场文书
自我评价怎么写好呢?
2013/12/05 职场文书
师德自我剖析材料范文
2014/10/06 职场文书
新郎答谢词
2015/01/04 职场文书
Nginx虚拟主机的搭建的实现步骤
2022/01/18 Servers