------------恢复内容开始------------

学习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")

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学习笔记的更多相关文章

  1. wxPython学习笔记(初识)

    今天正式开始学习wxPython,基于对类的不熟悉,理解有点生硬,但还是做了些笔记. 1.是什么组成了一个wxpython程序? 一个wxpython程序必须有一个application(wx.App ...

  2. wxPython学习笔记(三)

    要理解事件,我们需要知道哪些术语? 事件(event):在你的应用程序期间发生的事情,它要求有一个响应. 事件对象(event object):在wxPython中,它具体代表一个事件,其中包括了事件 ...

  3. wxPython学习笔记(二)

    如何创建和使用一个应用程序对象? 任何wxPython应用程序都需要一个应用程序对象.这个应用程序对象必须是类wx.App或其定制的子类的一个实例.应用程序对象的主要目的是管理幕后的主事件循环. 父类 ...

  4. wxPython学习笔记(一)

    创建最小的空的wxPython程序 frame = wx.Frame(parent=None, title='Bare') frame.Show() return True app = App() a ...

  5. wxPython学习笔记1

    wxpython介绍: wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的  GUI 用户界面. wxPython 是作为优 ...

  6. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  7. wxpython学习资源

    http://www.cnblogs.com/dyx1024/archive/2012/07/15/2592202.html wxPython:布局管理器sizer介绍 ogs.com/dyx1024 ...

  8. Python学习笔记之基础篇(-)python介绍与安装

    Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...

  9. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

随机推荐

  1. 在centos6.3下安装php的Xdebug

    首先下载一个xdebug http://www.xdebug.org/docs/ 官网上有windos版本和linux源码版本的,我们下载一个源码包xdebug-2.2.5.tgz 然后进入安装流程 ...

  2. mysql 查询指定数据库所有表, 指定表所有列, 指定列所有表 所有外键及索引, 以及索引的创建和删除

    查询指定 数据库 中所有 表 (指定数据库的,所有表) // 可以把 TABLE_NAME 换成 * 号, 查看更丰富的信息 SELECT TABLE_NAME FROM information_sc ...

  3. Mysql 命令 操作

    1.user表        如果需要从其他机器连接 mysql 服务器报这个错“ERROR 1130: Host 'root' is not allowed to connect to this M ...

  4. Day3前端学习之路——CSS基本知识

    课程目标 初步了解什么是CSS,掌握基本的CSS概念,语法,针对选择器特殊性的计算处理,以及学习如何设置一些简单的样式 任务一:回答问题 1.什么是CSS,CSS是如何工作的? CSS 指层叠样式表 ...

  5. 容器监控工具WeaveScope

    最近一段时间整了一些docker容器,弄了一些基于docker的微服务通信,弄好一套服务系统之后,对于服务的性能,基础数据的监控就显的很重要, 不然就是两眼一抹黑了,要不就是维护成本很高,这些都不符合 ...

  6. DotNet 源码学习——QUEUE

    1.Queue声明创建对象.(Queue为泛型对象.) public class Queue<T> :IEnumerable<T>,System.Collections.ICo ...

  7. 关于elementui的table组件单元格的内容自定义写法

    ------------恢复内容开始------------ 记录老哥的写法 columns是表格的配置文件 在表格渲染的时候通过renderTableCell传入表格的row以及配置文件中的rend ...

  8. 教你一种简单方法给word和PDF格式的文件使用电子签名

      前言  虽然还处在非常时期,但很多公司已陆陆续续复工或准备复工.   上周,人事妹纸给了我们一份,企业员工健康情况申报表.具体如下 现在问题来了,需要本人签名,电脑打上去的不算,需要手写. 此时, ...

  9. Kubernetes CI/CD(1)

    本文通过在kubernetes上启动Jenkins服务,并将宿主机上的docker.docker.sock挂载到Jenkins容器中,实现在Jenkins容器中直接打镜像的形式实现CI功能. Kube ...

  10. Numerical Testing Reportes of A New Conjugate Gradient Projection Method for Convex Constrained Nonlinear Equations

    Numerical Testing Reportes of A New Conjugate Gradient Projection Method for Convex Constrained Nonl ...