python使用wxpython开发简单记事本的方法

yipeiwu_com6年前Python基础

本文实例讲述了python使用wxpython开发简单记事本的方法。分享给大家供大家参考。具体分析如下:

wxPython是Python编程语言的一个GUI工具箱。他使得Python程序员能够轻松的创建具有健壮、功能强大的图形用户界面的程序。它是Python语言对流行的wxWidgets跨平台GUI工具库的绑定。而wxWidgets是用C++语言写成的。

和Python语言与wxWidgetsGUI工具库一样,wxPython是开源软件。这意味着任何人都可以免费地使用它并且可以查看和修改它的源代码,或者贡献补丁,增加功能。

wxPython是跨平台的。这意味着同一个程序可以不经修改地在多种平台上运行。现今支持的平台有:32位微软Windows操作系统、大多数Unix或类Unix系统、苹果MacOS X。

下面使用wxpython编写一个简单的记事本程序,可以打开本地文件,编辑,保存。

#!/usr/bin/python 
import wx 
def OnOpen(event): 
  """ 
  Load a file into the textField. 
  """ 
  dialog = wx.FileDialog(None,'Notepad',style = wx.OPEN) 
  if dialog.ShowModal() == wx.ID_OK: 
    filename.SetValue(dialog.GetPath()) 
    file = open(dialog.GetPath()) 
    contents.SetValue(file.read()) 
    file.close() 
  dialog.Destroy() 
def OnSave(event): 
  """ 
  Save text into the orignal file. 
  """ 
  if filename.GetValue() == '': 
    dialog = wx.FileDialog(None,'Notepad',style = wx.SAVE) 
    if dialog.ShowModal() == wx.ID_OK: 
      filename.SetValue(dialog.GetPath()) 
      file = open(dialog.GetPath(), 'w') 
      file.write(contents.GetValue()) 
      file.close() 
    dialog.Destory() 
  else: 
    file = open(filename.GetValue(), 'w') 
    file.write(contents.GetValue()) 
    file.close() 
app = wx.App() 
win = wx.Frame(None, title="Simple Editor", size=(600,400)) 
bkg = wx.Panel(win) 
# Define a 'load' button and its label, 
# bind to an button event with a function 'load' 
loadButton = wx.Button(bkg, label='Open') 
loadButton.Bind(wx.EVT_BUTTON, OnOpen) 
# Define a 'save' button and its label, 
# bind to an button event with a function 'save' 
saveButton = wx.Button(bkg, label='Save') 
saveButton.Bind(wx.EVT_BUTTON, OnSave) 
# Define a textBox for filename. 
filename = wx.TextCtrl(bkg) 
# Define a textBox for file contents. 
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL) 
# Use sizer to set relative position of the components. 
# Horizontal layout 
hbox = wx.BoxSizer() 
hbox.Add(filename, proportion=1, flag=wx.EXPAND) 
hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5) 
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5) 
# Vertical layout 
vbox = wx.BoxSizer(wx.VERTICAL) 
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5) 
vbox.Add(contents, proportion=1, 
     flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5) 
bkg.SetSizer(vbox) 
win.Show() 
app.MainLoop() 

运行效果如下图所示:

这个例子是《Python基础教程》中的一个例子,并做了一些修改。虽然完成了基本的记事本功能,但是界面略显简单,而且代码也没有很好地遵循面向对象编程原则。

希望本文所述对大家的Python程序设计有所帮助。

相关文章

详细介绍Python的鸭子类型

鸭子类型基本定义 首先Python不支持多态,也不用支持多态,python是一种多态语言,崇尚鸭子类型。 以下是维基百科中对鸭子类型得论述: 在程序设计中,鸭子类型(英语:duck t...

学习python的几条建议分享

熟悉python语言,以及学会python的编码方式。熟悉python库,遇到开发任务的时候知道如何去找对应的模块。知道如何查找和获取第三方的python库,以应付开发任务。 安装开发...

基于Django filter中用contains和icontains的区别(详解)

qs.filter(name__contains="e") qs.filter(name__icontains="e") 对应sql 'contains': 'LIKE BI...

在python里从协程返回一个值的示例

下面的例子演法了怎么样从协程里返回一个值: import asyncio async def coroutine(): print('in coroutine') ret...

pytorch 可视化feature map的示例代码

之前做的一些项目中涉及到feature map 可视化的问题,一个层中feature map的数量往往就是当前层out_channels的值,我们可以通过以下代码可视化自己网络中某层的f...