wxpython快速入门
admin
2023-07-18 20:42:08
0

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

1 import wx
2 app = wx.App(False)
3 frame = wx.Frame(None, wx.ID_ANY, "Hollo World")
4 frame.Show(True)
5 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()


相关内容

热门资讯

货车集体“偏航”泌阳,南辕北辙... 澎湃首席评论员 与归一个跑长途的货车司机偶尔偏航,可能还属正常;发现偏航后,开下高速在路边休息时却突...
公益组织发文:严禁会员在KTV... 澎湃新闻记者 钟煜豪公益慈善组织中国狮子联会沈阳代表处2026年6月14日发出通知,严禁会员在娱乐场...
“慢镜头下的悲剧”:中东局势冲... 澎湃新闻记者 南博一尽管美伊均对和谈释放积极信号,中东战事仍可能导致全球粮食价格飙升。中央广电总台国...
美媒反思:排除中国,G7还有存... 【文/观察者网 柳白】七国集团(G7)峰会临近,场外的中国,却越来越频繁出现在西方媒体叙事之中。“没...
张凌赫亮相第十八届海峡论坛,称... 据凤凰卫视报道,大陆演员张凌赫因出演的电视剧《逐玉》在宝岛热播,收获众多台湾粉丝,也曾多次被喊话希望...
00后收9万多LV包后维权,别... 据1818黄金眼、中国蓝新闻报道,张女士逛杭州一家LV店时,男友在店员推荐下,花94500元给她买了...
遭赖瑞隆指控家人全在美国,柯志... 海峡导报综合报道 民进党市长参选人、民代赖瑞隆13日质疑,国民党对手、民代柯志恩的资产、家人全在美国...
“青鸟”卜卦算出李四川大胜无奈... 海峡导报综合报道 2026新北市长之战,国民党李四川对决民进党苏巧慧。蓝营网友“政客爽”13日在脸书...
知情人士:伊朗尚未就谅解备忘录... 新华社德黑兰6月14日电 据伊朗法尔斯通讯社14日报道,一名接近伊朗谈判团队的知情人士表示,伊朗尚未...
海南省人工智能服务平台正式上线 中新网海口6月13日电 (记者 王晓斌)海南省人工智能服务平台13日正式上线。这个面向市场主体的人工...