wxpython
基本组件介绍:
frame(窗口)
参数: parent = None #父元素,假如为None,代表顶级窗口 id = None #组件的标识,唯一,假如id为-1代表系统分配id title = None #窗口组件的名称 pos = None #组件的位置,就是组件左上角点距离父组件或者桌面左和上的距离 size = None #组件的尺寸,宽高 style = None #组件的样式 name = None #组件的名称,也是用来标识组件的,但是用于传值
TextCtrl(文本框)
参数: parent = None #父元素,假如为None,代表顶级窗口 id = None #组件的标识,唯一,假如id为-1代表系统分配id value = None #文本框当中的内容 GetValue #获取文本框的值 SetValue #设置文本框的值 pos = None #组件的位置,就是组件左上角点距离父组件或者桌面左和上的距离 size = None #组件的尺寸,宽高 style = None #组件的样式 validator = None #验证 name = None #组件的名称,也是用来标识组件的,但是用于传值
Button(按钮)
参数: parent = None #父元素,假如为None,代表顶级窗口 id = None #组件的标识,唯一,假如id为-1代表系统分配id lable = None #按钮的标签 pos = None #组件的位置,就是组件左上角点距离父组件或者桌面左和上的距离 size = None #组件的尺寸,宽高 style = None #组件的样式 validator = None #验证 name = None #组件的名称,也是用来标识组件的,但是用于传值
一.基本窗口
import wx app = wx.App() frame = wx.Frame(None,title = "Gui Test Editor",pos = (1000,200),size = (500,400)) path_text = wx.TextCtrl(frame,pos = (5,5),size = (350,24)) open_button = wx.Button(frame,label = "打开",pos = (370,5),size = (50,24)) save_button = wx.Button(frame,label = "保存",pos = (430,5),size = (50,24))
二.事件绑定
import wx def openfile(event): # 定义打开文件事件 print("虫虫我爱你啊") app = wx.App() frame = wx.Frame(None, title="虫虫我爱你", pos=(1000, 200), size=(500, 400)) path_text = wx.TextCtrl(frame, pos=(5, 5), size=(350,24)) open_button = wx.Button(frame, label="打开", pos=(370, 5), size=(50, 24)) open_button.Bind(wx.EVT_BUTTON, openfile) # 绑定打开文件事件到open_button按钮上 save_button = wx.Button(frame, label="保存", pos=(430, 5), size=(50, 24)) content_text= wx.TextCtrl(frame,pos = (5,39),size = (475,300),style = wx.TE_MULTILINE) # wx.TE_MULTILINE可以实现以滚动条方式多行显示文本,若不加此功能文本文档显示为一行 frame.Show() app.MainLoop()
三.尺寸器
按照上面的GUI代码有一个缺陷,由于我们各个组件都固定了大小,因此在框体拉伸时,对应的组件不会对应进行拉伸,比较影响用户体验。
import wx def openfile(event): # 定义打开文件事件 print("虫虫我爱你啊") app = wx.App() frame = wx.Frame(None, title="虫虫我爱你", pos=(1000, 200), size=(500, 400)) panel = wx.Panel(frame) # path_text = wx.TextCtrl(frame, pos=(5, 5), size=(350,24)) path_text = wx.TextCtrl(panel) # open_button = wx.Button(frame, label="打开", pos=(370, 5), size=(50, 24)) open_button = wx.Button(panel, label="打开") open_button.Bind(wx.EVT_BUTTON, openfile) # save_button = wx.Button(frame, label="保存", pos=(430, 5), size=(50, 24)) save_button = wx.Button(panel, label="保存") # content_text= wx.TextCtrl(frame,pos = (5,39),size = (475,300),style = wx.TE_MULTILINE) content_text= wx.TextCtrl(panel, style=wx.TE_MULTILINE) box = wx.BoxSizer() # 不带参数表示默认实例化一个水平尺寸器 box.Add(path_text, proportion=5, flag=wx.EXPAND | wx.ALL, border=3) # 添加组件 # proportion:相对比例 # flag:填充的样式和方向,wx.EXPAND为完整填充,wx.ALL为填充的方向 # border:边框 box.Add(open_button, proportion=2, flag=wx.EXPAND | wx.ALL, border=3) # 添加组件 box.Add(save_button, proportion=2, flag=wx.EXPAND | wx.ALL, border=3) # 添加组件 v_box = wx.BoxSizer(wx.VERTICAL) # wx.VERTICAL参数表示实例化一个垂直尺寸器 v_box.Add(box, proportion=1, flag=wx.EXPAND | wx.ALL, border=3) # 添加组件 v_box.Add(content_text, proportion=5, flag=wx.EXPAND | wx.ALL, border=3) # 添加组件 panel.SetSizer(v_box) # 设置主尺寸器 frame.Show() app.MainLoop()
四.基本结构
import wx import win32api import sys, os import pyautogui import webbrowser as web from time import sleep import os import time APP_TITLE = u'控件事件、鼠标事件、键盘事件、系统事件' APP_ICON = 'favicon.ico' # 获取当前屏幕分辨率 screenWidth, screenHeight = pyautogui.size() # 03405197 currentMouseX, currentMouseY = pyautogui.position() # pyautogui.FAILSAF123456aB-E = True # pyautogui.PAUSE = 0.5 # 用IE浏览器打开指定网址 def ie_open_url(url): browser_path = r'C:Program Files (x86)Internet Exploreriexplore.exe' web.register('IE', None, web.BackgroundBrowser(browser_path)) web.get('IE').open_new_tab(url) print('use_IE_open_url open url ending ....') # 输入文字 def type_text(text): pyautogui.typewrite(text) def run(): ie_open_url('www.baidu.com') # 打开浏览器 time.sleep(2) type_text('1024') # 输入文字 time.sleep(2) class mainFrame(wx.Frame): '''程序主窗口类,继承自wx.Frame''' def __init__(self, parent): '''构造函数''' wx.Frame.__init__(self, parent, -1, APP_TITLE) self.SetBackgroundColour(wx.Colour(224, 224, 224)) self.SetSize((520, 220)) self.Center() if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe": exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None)) icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO) else: icon = wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO) self.SetIcon(icon) wx.StaticText(self, -1, u'第一行输入框:', pos=(40, 50), size=(100, -1), style=wx.ALIGN_RIGHT) wx.StaticText(self, -1, u'第二行输入框:', pos=(40, 80), size=(100, -1), style=wx.ALIGN_RIGHT) self.tip = wx.StaticText(self, -1, u'', pos=(145, 110), size=(150, -1), style=wx.ST_NO_AUTORESIZE) self.tc1 = wx.TextCtrl(self, -1, '', pos=(145, 50), size=(150, -1), name='TC01', style=wx.TE_CENTER) self.tc2 = wx.TextCtrl(self, -1, '', pos=(145, 80), size=(150, -1), name='TC02', style=wx.TE_PASSWORD | wx.ALIGN_RIGHT) btn_mea = wx.Button(self, -1, u'鼠标左键事件', pos=(350, 50), size=(100, 25)) btn_meb = wx.Button(self, -1, u'鼠标所有事件', pos=(350, 80), size=(100, 25)) btn_close = wx.Button(self, -1, u'关闭窗口', pos=(350, 110), size=(100, 25)) # 控件事件 self.tc1.Bind(wx.EVT_TEXT, self.EvtText) self.tc2.Bind(wx.EVT_TEXT, self.EvtText) self.Bind(wx.EVT_BUTTON, self.OnClose, btn_close) # 鼠标事件 btn_mea.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) btn_mea.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) btn_mea.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel) btn_meb.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse) # 键盘事件 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) # 系统事件 self.Bind(wx.EVT_CLOSE, self.OnClose) self.Bind(wx.EVT_SIZE, self.On_size) # self.Bind(wx.EVT_PAINT, self.On_paint) # self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None) def EvtText(self, evt): '''输入框事件函数''' obj = evt.GetEventObject() objName = obj.GetName() text = evt.GetString() if objName == 'TC01': self.tc2.SetValue(text) elif objName == 'TC02': self.tc1.SetValue(text) def On_size(self, evt): '''改变窗口大小事件函数''' self.Refresh() evt.Skip() # 体会作用 def OnClose(self, evt): '''关闭窗口事件函数''' dlg = wx.MessageDialog(None, u'确定要关闭本窗口?', u'操作提示', wx.YES_NO | wx.ICON_QUESTION) if (dlg.ShowModal() == wx.ID_YES): self.Destroy() def OnLeftDown(self, evt): '''左键按下事件函数''' run() self.tip.SetLabel(u'左键按下') def OnLeftUp(self, evt): '''左键弹起事件函数''' self.tip.SetLabel(u'左键弹起') def OnMouseWheel(self, evt): '''鼠标滚轮事件函数''' vector = evt.GetWheelRotation() self.tip.SetLabel(str(vector)) def OnMouse(self, evt): '''鼠标事件函数''' self.tip.SetLabel(str(evt.EventType)) def OnKeyDown(self, evt): '''键盘事件函数''' key = evt.GetKeyCode() self.tip.SetLabel(str(key)) class mainApp(wx.App): def OnInit(self): self.SetAppName(APP_TITLE) self.Frame = mainFrame(None) self.Frame.Show() return True if __name__ == "__main__": app = mainApp() app.MainLoop()
简单的按钮驱动
import wx import win32api import sys, os APP_TITLE = "数据采集" APP_ICON = 'favicon.ico' class mainFrame(wx.Frame): def __init__(self, parent): wx.Frame.__init__(self, parent, -1, APP_TITLE) self.SetBackgroundColour(wx.Colour(224, 224, 224)) self.SetSize((800, 600)) self.Center() if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe": exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None)) icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO) else: icon = wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO) self.SetIcon(icon) # self.Maximize() btn_mea = wx.Button(self, -1, u'数据采集一', pos=(350, 50), size=(100, 25)) btn_meb = wx.Button(self, -1, u'数据采集二', pos=(350, 80), size=(100, 25)) btn_close = wx.Button(self, -1, u'关闭窗口', pos=(350, 110), size=(100, 25)) btn_mea.Bind(wx.EVT_BUTTON, self.openfile_a) btn_meb.Bind(wx.EVT_BUTTON, self.openfile_b) def openfile_a(self, event): # 定义打开文件事件 print("虫虫我爱你啊") def openfile_b(self, event): # 定义打开文件事件 print("我中意嫩") class mainApp(wx.App): def OnInit(self): self.SetAppName(APP_TITLE) self.Frame = mainFrame(None) self.Frame.Show() return True if __name__ == "__main__": app = mainApp() app.MainLoop()