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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue通信、传值的多种方式(详细)

    vue通信、传值的多种方式(详细)Vue通信、传值的多种方式,详解(都是干货):一、通过路由带参数进行传值①两个组件A和B,A组件通过query把orderId传递给B组件(触发事件可以是点击事件、钩子函数等)this.$router.push({path:’/conponentsB’,query:{orderId:123}})//跳转到B②在B组件中获取A组件传递过来的参数…

    2022年6月10日
    32
  • leetcode – Populating Next Right Pointers in Each Node II

    leetcode – Populating Next Right Pointers in Each Node II

    2022年1月20日
    42
  • 什么是java的多态

    什么是java的多态多态分为两种a.编译时多态:方法的重载;b. 运行时多态:JAVA运行时系统根据调用该方法的实例的类型来决定选择调用哪个方法则被称为运行时多态。(我们平时说得多的事运行时多态,所以多态主要也是指运行时多态);上述描述认为重载也是多态的一种表现,不过多态主要指运行时多态。2.运行时多态a.面向对象的三大特性:封装、继承、多态。从一定角度来看,封装和继承几乎都是为多态而准备的。…

    2022年7月7日
    22
  • 3D实例分割「建议收藏」

    3D实例分割「建议收藏」13D实例分割-云+社区-腾讯云2提出“3D-BoNet”,比3D点云实例分割算法快10倍!

    2022年8月23日
    8
  • 架设邮件服务器-windows 2003 POP3服务,SMTP服务收发邮件「建议收藏」

    1.默认安装的系统是没有安装POP3组件,SMTP组件,搞个盘过来,或从网上下载一个i386(下载地址:http://down.spdns.com/i386.rar ).(1)从“控制面板→添加/删除程序→添加windows组件”中,进入“Windwos组件”界面,激活“应用程序服务器”一行,然后单击“详细信息”按钮,进入“应用程序服务器”页,选择“Internet信息服务(IIS)”复选

    2022年4月6日
    36
  • sql语句 模糊查找like

    sql语句 模糊查找like模糊查找:like语法形式:字段like’要查找字符’说明:1、like模糊查找用于对字符类型的字段进行字符匹配查找。2、要查找的字符中,有两个特殊含义的字符:%,_:2.1:%含义是:代表0或多个的任意字符2.2:_含义是:代表1个任意字符2.3:这里的字符都是指现实中可见的一个“符号”,而不是字节。3、语法:like’%关键字%’SELECT*FROMstudentWHERENAMELIKE’张%’;–…

    2022年6月5日
    39

发表回复

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

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