wxPython学习笔记
------------恢复内容开始------------
学习wxPython 资料
1.wxpython wiki
Getting started with wxPython
https://wiki.wxpython.org/Getting%20Started
入门例子程序:
A First Application: "Hello, World"
#!/usr/bin/env python
import wx app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True) # Show the frame.
app.MainLoop()
Explanations:
| 
 app = wx.App(False)  | 
 Every wxPython app is an instance of wx.App. For most simple applications you can use wx.App as is. When you get to more complex applications you may need to extend the wx.App class. The "False" parameter means "don't redirect stdout and stderr to a window".  | 
| 
 wx.Frame(None,wx.ID_ANY,"Hello")  | 
 A wx.Frame is a top-level window. The syntax is wx.Frame(Parent, Id, Title). Most of the constructors have this shape (a parent object, followed by an Id). In this example, we use None for "no parent" and wx.ID_ANY to have wxWidgets pick an id for us.  | 
| 
 frame.Show(True)  | 
 We make a frame visible by "showing" it.  | 
| 
 app.MainLoop()  | 
 Finally, we start the application's MainLoop whose role is to handle the events.  | 
Building a simple text editor
#!/usr/bin/env python
import wx
class MyFrame(wx.Frame):
""" We simply derive a new class of Frame. """
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.Show(True) app = wx.App(False)
frame = MyFrame(None, 'Small editor')
app.MainLoop()
In this example, we derive from wx.Frame and overwrite its __init__ method. Here we declare a new wx.TextCtrl which is a simple text edit control. Note that since the MyFrame runs self.Show() inside its __init__ method, we no longer have to call frame.Show() explicitly.
Adding a menu bar
Every application should have a menu bar and a status bar. Let's add them to ours
import wx class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar() # A Statusbar in the bottom of the window # Setting up the menu.
filemenu= wx.Menu() # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program") # Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
self.Show(True) app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
add event handling
import os
import wx class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar() # A StatusBar in the bottom of the window # Setting up the menu.
filemenu= wx.Menu() # wx.ID_ABOUT and wx.ID_EXIT are standard ids provided by wxWidgets.
menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program") # Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. # Set events.
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit) self.Show(True) def OnAbout(self,e):
# A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
dlg = wx.MessageDialog( self, "A small text editor", "About Sample Editor", wx.OK)
dlg.ShowModal() # Show it
dlg.Destroy() # finally destroy it when finished. def OnExit(self,e):
self.Close(True) # Close the frame. app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
add open text event handling
import wx
import os class MainWindow(wx.Frame): def __init__(self, parent, title):
self.dirname = ''
wx.Frame.__init__(self, parent, title=title, size=(200,-1))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,pos=(40,50))
self.CreateStatusBar() # A Statusbar in the bottom of the window #panel = wx.Panel(self)
#self.quote = wx.StaticText(panel, label="This is a static label", pos=(100,-1)) # Setting up the menu.
filemenu= wx.Menu()
opermenu = wx.Menu()
operItem1 = opermenu.Append(wx.ID_ANY, "&operation", "process operation") # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open the file")
filemenu.AppendSeparator()
menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program") # Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
menuBar.Append(opermenu,"&Operation") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. # set events
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit) self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.buttons = []
for i in range(0, 6):
self.buttons.append(wx.Button(self, -1, "Button &"+str(i+1)))
self.sizer2.Add(self.buttons[i], 1, wx.EXPAND) # Use some sizers to see layout options
self.sizer = wx.BoxSizer(wx.VERTICAL)
#self.sizer.Add(self.quote,0,wx.EXPAND)
self.sizer.Add(self.control, 1, wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.EXPAND) #Layout sizers
self.SetSizer(self.sizer)
self.SetAutoLayout(True)
self.sizer.Fit(self) self.Show(True) def OnAbout(self,event):
dlg = wx.MessageDialog(self, "A Small Text Editor", "About Sample Editor", wx.OK)
#wx.MessageDialog()
dlg.ShowModal()
dlg.Destroy() def OnExit(self,event):
self.Close(True) def OnOpen(self,event):
""" open the file """
dlg = wx.FileDialog(self,"choose a file ",self.dirname, "","*.*",wx.FD_OPEN)
dlg = wx.FileDialog(self,"choose a file ",self.dirname, "","*.*",wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
wxPython学习笔记的更多相关文章
- wxPython学习笔记(初识)
		
今天正式开始学习wxPython,基于对类的不熟悉,理解有点生硬,但还是做了些笔记. 1.是什么组成了一个wxpython程序? 一个wxpython程序必须有一个application(wx.App ...
 - wxPython学习笔记(三)
		
要理解事件,我们需要知道哪些术语? 事件(event):在你的应用程序期间发生的事情,它要求有一个响应. 事件对象(event object):在wxPython中,它具体代表一个事件,其中包括了事件 ...
 - wxPython学习笔记(二)
		
如何创建和使用一个应用程序对象? 任何wxPython应用程序都需要一个应用程序对象.这个应用程序对象必须是类wx.App或其定制的子类的一个实例.应用程序对象的主要目的是管理幕后的主事件循环. 父类 ...
 - wxPython学习笔记(一)
		
创建最小的空的wxPython程序 frame = wx.Frame(parent=None, title='Bare') frame.Show() return True app = App() a ...
 - wxPython学习笔记1
		
wxpython介绍: wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的 GUI 用户界面. wxPython 是作为优 ...
 - python学习笔记目录
		
人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...
 - wxpython学习资源
		
http://www.cnblogs.com/dyx1024/archive/2012/07/15/2592202.html wxPython:布局管理器sizer介绍 ogs.com/dyx1024 ...
 - Python学习笔记之基础篇(-)python介绍与安装
		
Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...
 - js学习笔记:webpack基础入门(一)
		
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
 
随机推荐
- C++用rand()和srand()生成随机数
			
内容来自<编程实战宝典> 首先来看函数原型 int rand(void); void srand(unsigned int seed); 1.rand()函数不需要任何参数,直接返回一个随 ...
 - js发展历史与基础
			
最早的浏览器是WWW浏览器 Mosaic浏览器是互联网历史上第一个获普遍使用和能够显示图片的网页浏览器,于1993年问世 浏览器组成部分: 1)shell部分(外壳) 2)内核部分 ① 渲染引擎(语法 ...
 - 编译原理实验之SLR1文法分析
			
---内容开始--- 这是一份编译原理实验报告,分析表是手动造的,可以作为借鉴. 基于 SLR(1) 分析法的语法制导翻译及中间代码生成程序设计原理与实现1 .理论传授语法制导的基本概念,目标代码结 ...
 - Ubuntu14.04安装及配置mysql5.7.19
			
通过uname -a或lsb_release -a 查看版本信息 ,确定linux的发行版是debian还是ubuntu.root@suepc:/usr/local# uname -aLinux su ...
 - NOIP幂次方
			
#include<stdio.h> ] = { ,,,,,,,,,,,,,,, };//由题意n最大为20000,所以最多会用到2的14次方 //为了防止mid+1出错,故写到15次方 i ...
 - redis系列-要命的zrangebyscore
			
0x0 引子 无论做哪种业务都躲不开排行功能.Redis 的 Sorted Sets 结构就是为排行而生的.它简单易用,效率奇高.同时它也有坑,你真的了解它吗? 老规矩,先讲故事,后科普.这里是 So ...
 - RabbitMQ工作模式
			
------------恢复内容开始------------ RabbitMQ基本概念: Producer:生产者(消息的提供者) Consumer:消费者(消息的使用者) Message:消息(程序 ...
 - jQuery on 绑定的事件 执行两次
			
$(".class1").on("click",".class2",function(){ alert('提示'); }); 上面代码,怎么 ...
 - 学习了解CSS3发展方向和CSS样式与优先级
			
通过学习CSS3游戏介绍.CSS样式和优先级章节,了解到html5+css3+js不光可以实现动画,其次可以往这个游戏与建模方向发展,更多css3特效访问Gerard Ferrandez on Cod ...
 - python+selenium自动化测试,浏览器最大化报错解决方法
			
此处以谷歌浏览器为例 [问题1]缺少chrome驱动,webdriver调用谷歌浏览器的时候就报错了,如下图: [原因分析]缺少谷歌驱动程序 [解决办法] 1.查看本地安装chrome浏览器版本 2. ...