python学习笔记十四:wxPython Demo

一、简介

wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的、功能键全的GUI用户界面。 wxPython是作为优秀的跨平台GUI库wxWidgets的Python封装和Python模块的方式提供给用户的。

二、安装

参考官方网站:http://www.wxpython.org/download.php

三、DEMO

本demo是一个简单的记事本软件,可以打开文件,修改并保存。

import wx

app = wx.App()
win = wx.Frame(
    None,
    title="simple editor",
    size=(410, 335))

bkg = wx.Panel(win)


def openFile(evt):
    dlg = wx.FileDialog(
        win,
        "Open",
        "",
        "",
        "All files (*.*)|*.*",
        wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
    filepath = ''
    if dlg.ShowModal() == wx.ID_OK:
        filepath = dlg.GetPath()
    else:
        return
    filename.SetValue(filepath)
    fopen = open(filepath)
    fcontent = fopen.read()
    contents.SetValue(fcontent)
    fopen.close()


def saveFile(evt):
    fcontent = contents.GetValue()
    fopen = open(filename.GetValue(), 'w')
    fopen.write(fcontent)
    fopen.close()

openBtn = wx.Button(bkg, label='open')
openBtn.Bind(wx.EVT_BUTTON, openFile)

saveBtn = wx.Button(bkg, label='save')
saveBtn.Bind(wx.EVT_BUTTON, saveFile)

filename = wx.TextCtrl(bkg, style=wx.TE_READONLY)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)

hbox = wx.BoxSizer()
hbox.Add(openBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
hbox.Add(filename, proportion=1, flag=wx.EXPAND | wx.TOP | wx.BOTTOM, border=5)
hbox.Add(saveBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)

bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL)
bbox.Add(
    contents,
    proportion=1,
    flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT,
    border=5)

bkg.SetSizer(bbox)
win.Show()
app.MainLoop()

运行效果:

转载于:https://my.oschina.net/artwl/blog/694370

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值