目的:
1)建立一个GUI
2)可以通过按钮选择文件夹,并获取文件夹地址
3)通过下拉框选择对应的函数
过程:
1)安装wxpython pip install wxpython
2)建立基本的wx框架,了解python类的继承和使用
import wx
import os
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title = "DecodeFrame -ASN18A3", size = (400,250))
panel = wx.Panel(self)
if __name__=='__main__':
app = wx.App()
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()
3)熟悉需要使用的函数。https://docs.wxpython.org/
建立面板 Panel(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TAB_TRAVERSAL, name=PanelNameStr) 添加标签 StaticText(parent, id=ID_ANY, label="", pos=DefaultPosition, size=DefaultSize, style=0, name=StaticTextNameStr)
添加对话框 TextCtrl(parent, id=ID_ANY, value="", pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=TextCtrlNameStr)
添加button Button(parent, id=ID_ANY, label="", pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=ButtonNameStr)
打开文件夹函数 FileDialog(parent, message=FileSelectorPromptStr, defaultDir="", defaultFile="", wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE, pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr) 在button绑定函数的时候可能会调用另外一个文件夹的函数,这个时候,可以直接将py文件当作库导入。如果在同一个文件夹内,可以直接导入。 from RMS_Decode import *
结果和源码
import wx
import os
from GPS_decode import *
from Ecall_Decode import *
from Wifi_Decode import *
from RMS_Decode import *
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title = "DecodeFrame -ASN18A3", size = (400,250))
panel = wx.Panel(self)
# 创建标题 StaticText
self.label1 = wx.StaticText(panel, -1, label = "选择文件:", pos = (50,20))
self.text_file = wx.TextCtrl(panel, -1, value = "",pos = (110, 20), size = (100, 20))
self.Button_File = wx.Button(panel, -1, label = "FileS", pos = (220, 20), size = (50, 20))
self.Button_File.Bind(wx.EVT_BUTTON, self.OnOpenFile)
# 创建下拉列表
self.label2 = wx.StaticText(panel, -1, label = "选择端口:", pos = (50,60))
item = ['50164:charge', '50177:wifi','50176:GPS','50169:Ecall', '50161:RMS']
self.list_item = wx.Choice(panel, -1, pos = (110 ,60), size = (80, 40), choices = item)
# 创建编译按钮
self.Button_compile = wx.Button(panel, -1, label = "Compile", pos = (220, 60), size = (100,100))
self.Button_compile.Bind(wx.EVT_BUTTON, self.Decode_Function)
def OnOpenFile(self, event):
dlg = wx.FileDialog(self, "Open wireshark file", wildcard="pcap files (*.pcap)|*.pcap|cap file(*.cap)|*.cap",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
global pathindir
pathindir = dlg.GetPath()
self.text_file.Clear()
self.text_file.WriteText(pathindir)
# path = self.text_file.GetValue()
# load_pcap(pathindir)
dlg.Destroy()
def Decode_Function(self, event):
print(self.list_item.GetStringSelection())
if self.list_item.GetStringSelection() == '50176:GPS' :
decode_GPS(pathindir)
print("GPS decode end")
if self.list_item.GetStringSelection() == '50169:Ecall' :
ecall_decode(pathindir)
print("ecall decode end")
if self.list_item.GetStringSelection() == '50177:wifi' :
Wifi_Decode(pathindir)
print("wifi decode end")
if self.list_item.GetStringSelection() == '50161:RMS' :
RMS_Decode(pathindir)
print("rms decode end")
if __name__=='__main__':
app = wx.App()
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()