wxpython中文教程_wxPython快速入门教程

wxpython中文教程_wxPython快速入门教程1第一个应用程序“Hello,world”importwxapp=wx.App(False)frame=wx.Frame(None,wx.ID_ANY,”HolloWorld”)frame.Show(True)app.MainLoop()2是创造一个wx.App实例。参数是“False”的意思是不将stdout和stderr重定向到一个窗口,这个参数是“True”对这个例子没有…

大家好,又见面了,我是你们的朋友全栈君。

1 第一个应用程序 “Hello,world”

import wx

app = wx.App(False)

frame = wx.Frame(None, wx.ID_ANY, “Hollo World”)

frame.Show(True)

app.MainLoop()

2是创造一个wx.App实例。参数是“False”的意思是不将stdout和stderr重定向到一个窗口,这个参数是“True”对这个例子没有影响。

3创建一个顶级窗口,语法为x.Frame(parent,ID,标题)。这个例子中wx.ID_ANY wxWidgets为我们挑选一个id。

4显示窗口

5主循环,处理事件

2输入多行文字wx.TextCtrl

import wx

class my_frame(wx.Frame):

“””We simple derive a new class of Frame”””

def __init__(self,parent, title):

wx.Frame.__init__(self, parent, title=title,size=(300,100))

self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)

self.Show(True)

app = wx.App(False)

frame = my_frame (None,’Small edior’)

app.MainLoop()

继承来自wx.Frame的__init__方法。声明一个wx.TextCtrl控件

(简单的文本编辑控件)

3增加一个菜单

import wx

class my_frame(wx.Frame):

“””We simple derive a new class of Frame”””

def __init__(self,parent, title):

wx.Frame.__init__(self, parent, title=title,size=(300,200))

self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)

self.Show(True)

self.CreateStatusBar()#创建窗口底部的状态栏

filemenu = wx.Menu()

filemenu.Append(wx.ID_EXIT, “Exit”, “Termanate the program”)

filemenu.AppendSeparator()

filemenu.Append(wx.ID_ABOUT, “About”, “Information about this program”)#设置菜单的内容

menuBar = wx.MenuBar()

menuBar.Append(filemenu, u”设置”)

self.SetMenuBar(menuBar)#创建菜单条

self.Show(True)

app = wx.App(False)

frame = my_frame(None, ‘Small edior’)

app.MainLoop()

wx.ID_ABOUT和wx.id_EXIT这是标准wxWidgets提供的id,这样做的好处是可以保证兼容性,多个平台可以运行

4事件处理

import wx

class my_frame(wx.Frame):

“””We simple derive a new class of Frame”””

def __init__(self,parent, title):

wx.Frame.__init__(self, parent, title=title,size=(300,200))

self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)

self.Show(True)

self.CreateStatusBar()#创建窗口底部的状态栏

filemenu = wx.Menu()

menu_exit = filemenu.Append(wx.ID_EXIT, “Exit”, “Termanate the program”)

filemenu.AppendSeparator()

menu_about = filemenu.Append(wx.ID_ABOUT, “About”, “Information about this program”)#设置菜单的内容

menuBar = wx.MenuBar()

menuBar.Append(filemenu, u”设置”)

self.SetMenuBar(menuBar)#创建菜单条

self.Show(True)

self.Bind(wx.EVT_MENU, self.on_about, menu_about)

self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来

def on_about(self,e):#about按钮的处理函数

dlg = wx.MessageDialog(self,”A samll text editor”, “About sample Editor”,wx.OK)#创建一个对话框,有一个ok的按钮

dlg.ShowModal()#显示对话框

dlg.Destroy()#完成后,销毁它。

def on_exit(self,e):

self.Close(True)

app = wx.App(False)

frame = my_frame(None, ‘Small edior’)

app.MainLoop()

第一步是设定事件,然后设定事件出现后应该执行什么操作,最后把事件和操作连接起来。

5弹出对话框,选择要编辑的文件

def on_open(self,e):

“””open a file”””

self.dirname = ”

dlg = wx.FileDialog(self,”Choose a file”, self.dirname, “”,”*.*”,wx.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”)

dlg.Destroy()

然后把这个方法和添加进入菜单和一个按钮事件绑定起来

完整代码

import wx

import os

class my_frame(wx.Frame):

“””This is a simple text editor”””

def __init__(self,parent, title):

wx.Frame.__init__(self, parent, title=title,size=(300,200))

self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)

self.Show(True)

self.CreateStatusBar()#创建窗口底部的状态栏

filemenu = wx.Menu()

menu_open = filemenu.Append(wx.ID_OPEN,U”打开文件”, ” “)

menu_exit = filemenu.Append(wx.ID_EXIT, “Exit”, “Termanate the program”)

filemenu.AppendSeparator()

menu_about = filemenu.Append(wx.ID_ABOUT, “About”, “Information about this program”)#设置菜单的内容

menuBar = wx.MenuBar()

menuBar.Append(filemenu, u”设置”)

self.SetMenuBar(menuBar)#创建菜单条

self.Show(True)

self.Bind(wx.EVT_MENU,self.on_open,menu_open)

self.Bind(wx.EVT_MENU, self.on_about, menu_about)

self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来

def on_about(self,e):#about按钮的处理函数

dlg = wx.MessageDialog(self,”A samll text editor”, “About sample Editor”,wx.OK)#创建一个对话框,有一个ok的按钮

dlg.ShowModal()#显示对话框

dlg.Destroy()#完成后,销毁它。

def on_exit(self,e):

self.Close(True) def on_open(self,e):

“””open a file”””

self.dirname = ”

dlg = wx.FileDialog(self,”Choose a file”, self.dirname, “”,”*.*”,wx.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”)

dlg.Destroy()

app = wx.App(False)

frame = my_frame(None, ‘Small edior’)

app.MainLoop()

6.把文件读取出来的数据,显示在文本框内。并加入保存文件的功能。打开文件时使用decode(),保存时使用encode(),使用unicode防止因为中文出现的错误。

# -*- coding: utf-8 -*-

import wximport os

class my_frame(wx.Frame):

“””This is a simple text editor”””

def __init__(self,parent, title):

wx.Frame.__init__(self, parent, title=title,size=(300,200))

self.control = wx.TextCtrl(self, -1,u”请先打开要修改的文件”, style=wx.TE_MULTILINE,)

self.Show(True)

self.CreateStatusBar()#创建窗口底部的状态栏

filemenu = wx.Menu()

menu_open = filemenu.Append(wx.ID_OPEN, U”打开文件”, ” “)

menu_save = filemenu.Append(wx.ID_SAVE, U”保存修改”,)

menu_exit = filemenu.Append(wx.ID_EXIT, “Exit”, “Termanate the program”)

filemenu.AppendSeparator()

menu_about = filemenu.Append(wx.ID_ABOUT, “About”, “Information about this program”)#设置菜单的内容

menuBar = wx.MenuBar()

menuBar.Append(filemenu, u”选项”)

self.SetMenuBar(menuBar)#创建菜单条

self.Show(True)

self.Bind(wx.EVT_MENU, self.on_open, menu_open)

self.Bind(wx.EVT_MENU, self.on_about, menu_about)

self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来

self.Bind(wx.EVT_MENU, self.on_save, menu_save) def on_about(self,e):#about按钮的处理函数

dlg = wx.MessageDialog(self,”A samll text editor”, “About sample Editor”,wx.OK)#创建一个对话框,有一个ok的按钮

dlg.ShowModal()#显示对话框

dlg.Destroy()#完成后,销毁它。

def on_exit(self,e):

self.Close(True) def on_open(self,e):

“””open a file”””

self.dirname = ”

dlg = wx.FileDialog(self,”Choose a file”, self.dirname, “”,”*.*”,wx.OPEN)#调用一个函数打开对话框

if dlg.ShowModal() == wx.ID_OK:

self.filename = dlg.GetFilename()

self.dirname = dlg.GetDirectory()

self.address = os.path.join(self.dirname,self.filename)

f = open(self.address,”r”)

file = (f.read()).decode(encoding=’utf-8′)#解码,使文件可以读取中文

f.close()

self.control.Clear()

self.control.AppendText(file)#把打开的文件内容显示在多行文本框内

dlg.Destroy() def on_save(self, e):

date = (self.control.GetValue()).encode(encoding=”utf-8″)#编码,使中文可以正确存储

f = open(self.address, ‘w’)

f.write(date)

f.close()#把文本框内的数据写入并关闭文件

dlg = wx.MessageDialog(self, u”文件已经成功保存”, u”消息提示”, wx.OK)

dlg.ShowModal()

dlg.Destroy()

self.control.Clear()

self.control.AppendText(u’欢迎使用此软件,作者即刻’)

app = wx.App(False)

frame = my_frame(None, u’迷你文本编辑器’)

app.MainLoop()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143367.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • zoj1204「建议收藏」

    zoj1204「建议收藏」也是这个星期写的了然而zoj识别不聊to_string只好先贴上来#include#include#includeusingnamespacestd;intnum[30],a,b,f;intall;intadd(intx,ints,intn,stringstr);intmain(){ cin>>a; for(inti=0;i

    2022年7月12日
  • 【游戏开发指路】Unity学习路线,三万字大纲(面试题大纲 | 知识图谱 | Unity游戏开发工程师)

    【游戏开发指路】Unity学习路线,三万字大纲(面试题大纲 | 知识图谱 | Unity游戏开发工程师)【游戏开发指路】Unity学习路线,三万字大纲(面试题大纲|知识图谱|Unity游戏开发工程师)

    2022年6月16日
  • 原则与执着:暴雪王牌总结游戏研发得失成败「建议收藏」

    原则与执着:暴雪王牌总结游戏研发得失成败「建议收藏」暴雪执行副总裁、diablo3设计师RobPardo在本年度GDC上详细讲述了暴雪在游戏设计理念的主要原则,总结暴雪过去十余年中的经验教训。  RobPardo在暴雪长期负责游戏设计方面的工作,具有极其丰富的成功经验。他曾是《星际争霸》、《星际争霸:母巢之战》、《魔兽争霸3:混乱之治》、《魔兽争霸3:冰封王座》、《魔兽世界》、《魔兽世界:燃烧远征》的主要设计者,2006年他以《魔兽世界》

    2022年5月7日
  • pytorch-DataLoader(数据迭代器)

    pytorch-DataLoader(数据迭代器)目录1.1dataset1.1.1Map-styledatasets实现方法一(简单直白法)实现方法二(借助TensorDataset直接将数据包装成dataset类)实现方法三(地址读取法)1.1.1Iterable-styledatasets我们一般使用一个for循环(或多层的)来训练神经网络,每一次迭代,加载一个batch的数据,神经网络前向反向传播各一次并更新一次参数。而这个过程中加载一个batch的数据这一步需要使用一个torch.utils.data.DataLoader对象,并且

    2022年6月11日
  • datagrip怎么安装(dg专业版注册码)

    !!!重点先上!!!今天在尝试使用DataGrip,第一次下载了最新的2018版本,后来发现注册码无法获取到(呵呵)。。。又迫于急切需要,对于非数据库开发人员,我觉得2017.2版本足够了(因为它刚好可以通过Licenseserver注册),所以就选择了这个版本!对于需要尝新使用更高版本的话,那就忽略本文的教程哈~1、下载2017.2版本(链接)2、安装后正常启动,进入填写注册码页面时,…

    2022年4月14日
  • 关于winform界面美化。

    关于winform界面美化。今天看到有好多文章写了关于界面美化的方法。有的弄的比较复杂。这里我就简单点介绍一下最方便的方法吧。首先先跟大家分享两个DLL文件。http://files.cnblogs.com/08shiyan/IrisSkin2.zip [适用于.NET2.0、破解版、来源互联网]http://files.cnblogs.com/08shiyan/IrisSkin4.zip [适用于.N

    2022年5月28日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号