使用wx制作一个桌面软件
前面因为抓取数据,为了方便期间做了各界面,用到了wx,觉得很好用,所以最近几天专门看了一下wx的使用,并练习了一下。
代码:
import os,sys,re,time import wx,wx.xrc,wx.adv,wx.grid import json,math,random import subprocess,threading from win32api import GetSystemMetrics from faker import Faker APP_ICON = '.\\favicon_test2.ico' APP_TITLE = '测试软件' nowtime1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) nowtime2 = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) nowunixtime = int(round(time.time() * 1000)) username = '' password = '' percent = 0.6 faker = Faker(locale='zh_CN') show_icon = False show_window = True myFrames = {} myFrame = '' abouttext = ''' import wx class MyFrame(wx.Frame): def __init__(self, parent): wx.Frame.__init__(self, parent, -1, "多行文本框示例") panel = wx.Panel(self) # 创建多行文本框 self.multi_text_ctrl = wx.TextCtrl(panel, style=wx.TE_MULTILINE) # 设置布局管理器 sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.multi_text_ctrl, 1, wx.ALL|wx.EXPAND, 5) panel.SetSizer(sizer) if __name__ == "__main__": app = wx.App(False) frame = MyFrame(None) frame.Show(True) app.MainLoop() '''.strip() datas = [] for i in range(0, 1017): sex = random.randint(0,1) if sex == 0: name = faker.name_male() sex2 = '男' else: name = faker.name_female() sex2 = '女' datas.append([name,sex2,str(random.randint(1,99)),faker.email(),faker.company(),faker.address()]) labelSize = (int(90*percent), int(45*percent)) textSize = (int(250*percent), int(45*percent)) buttonSize = (int(110*percent), int(45*percent)) imgSize = (int(1024*percent), int(768*percent)) lineHeight = int(45*percent) def getFileContent(file): fp = open(file, 'r', encoding='utf-8') res = fp.read() fp.close() return res def saveFileContent(file, content): fp = open(file, 'w', encoding='utf-8') fp.write(content) fp.close() class FolderBookmarkTaskBarIcon(wx.adv.TaskBarIcon): MENU_ID1, MENU_ID2 = wx.NewIdRef(count=2) def __init__(self): super().__init__() self.SetIcon(wx.Icon(APP_ICON), APP_TITLE) self.Bind(wx.EVT_MENU, self.onShow, id=self.MENU_ID1) self.Bind(wx.EVT_MENU, self.onExit, id=self.MENU_ID2) def CreatePopupMenu(self): menu = wx.Menu() menu.Append(self.MENU_ID1, '显示隐藏') menu.Append(self.MENU_ID2, '退出') return menu def onShow(self, event): global show_window,myFrames,myFrame frame = myFrames[myFrame] if show_window == True: frame.Show(False) show_window = False else: frame.Show(True) show_window = True def onExit(self, event): wx.Exit() class LoginFrame ( wx.Frame): sub_title = '登录' my_frame = 'login' def __init__( self, parent ): global percent,labelSize,textSize,buttonSize,lineHeight,username,show_window,myFrames,myFrame,show_icon wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE ) labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) labelColor = wx.Colour( 139, 69, 19 ) #设置窗口位置,大小 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) self.SetSize((window_width, window_height)) position_x = (screen_width - window_width) // 2 position_y = (screen_height - window_height) // 2 self.SetPosition(wx.Point(position_x, position_y)) #计算控件坐标 positionx_label = (window_width - (labelSize[0] + textSize[0])) // 2 positionx_text = positionx_label + labelSize[0] positionx_button = positionx_text + textSize[0] - buttonSize[0] positiony_label1 = (window_height - (labelSize[1]*3 + lineHeight*2)) // 2 positiony_label2 = positiony_label1 + labelSize[1] + lineHeight positiony_label3 = positiony_label1 + labelSize[1] + lineHeight + labelSize[1] + lineHeight self.usernameLabel = wx.StaticText(self, label='用户名:', size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label, positiony_label1)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) self.usernameInput = wx.TextCtrl(self, size=textSize) self.usernameInput.SetPosition(wx.Point(positionx_text, positiony_label1)) self.usernameInput.SetValue("admin") self.passwordLabel = wx.StaticText(self, label='密码:', size=labelSize) self.passwordLabel.SetPosition(wx.Point(positionx_label, positiony_label2)) self.passwordLabel.SetFont(labelFont) self.passwordLabel.SetForegroundColour(labelColor) self.passwordInput = wx.TextCtrl(self, style=wx.TE_PASSWORD, size=textSize) self.passwordInput.SetPosition(wx.Point(positionx_text, positiony_label2)) self.passwordInput.SetValue("123456") self.loginButton = wx.Button( self, label='登录', size=buttonSize) self.loginButton.SetPosition(wx.Point(positionx_button, positiony_label3)) self.loginButton.SetFont(labelFont) self.loginButton.SetForegroundColour(labelColor) self.loginButton.Bind( wx.EVT_BUTTON, self.login ) self.SetWindowStyle(wx.STAY_ON_TOP | wx.CAPTION | wx.SYSTEM_MENU) self.SetTitle(APP_TITLE+'-'+self.sub_title) self.SetIcon(wx.Icon(APP_ICON)) self.SetBackgroundColour( wx.Colour( 245, 222, 179 ) ) self.Raise() self.Show(True) show_window = True myFrame = self.my_frame if not myFrame in myFrames: myFrames[myFrame] = self #显示icon if show_icon == False: FolderBookmarkTaskBarIcon() show_icon = True #绑定事件 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.timer.Start(3000) def OnTimer(self, event): thread_obj = threading.Thread(target=self.OnTimer1, args=(event, )) thread_obj.start() def OnTimer1(self, event): #设置窗口位置,大小 return ; screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) print('屏幕大小: %s*%s' % (screen_width, screen_height)) (window_widthx, window_heightx) = self.GetSize() print('窗口大小: %s*%s' % (window_widthx, window_heightx)) (position_xx, position_yy) = self.GetPosition() print('窗口位置: %s*%s' % (position_xx, position_yy)) print() def login(self, event): global username, password usernameVal = self.usernameInput.GetValue() passwordVal = self.passwordInput.GetValue() if not usernameVal in ['admin', 'root'] or passwordVal != '123456': wx.MessageBox('用户名和密码不匹配') return False username = usernameVal password = passwordVal self.Hide() mfObj = MainFrame(None) return True def __del__( self ): pass class MainFrame ( wx.Frame): sub_title = '首页' my_frame = 'main' def __init__( self, parent ): global percent,labelSize,textSize,buttonSize,imgSize,lineHeight,username,show_window,myFrames,myFrame wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE ) labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) labelColor = wx.Colour( 139, 69, 19 ) #设置窗口位置,大小 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) self.SetSize((window_width, window_height)) position_x = (screen_width - window_width) // 2 position_y = (screen_height - window_height) // 2 self.SetPosition(wx.Point(position_x, position_y)) labelSize = (int(310*percent), int(25*percent)) positionx_label1 = int(5*percent) positiony_label1 = int(5*percent) positionx_label2 = window_width - labelSize[0] positiony_label2 = int(5*percent) self.usernameLabel = wx.StaticText(self, label='', size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label1, positiony_label1)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if username == '': username = '未知用户' self.usernameLabel = wx.StaticText(self, label='登录用户:'+username, size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label2, positiony_label2)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if not username == '未知用户': self.usernameLabel.Bind( wx.EVT_LEFT_DOWN, self.logout ) positionx_label3 = (window_width - buttonSize[0]) // 2 positiony_label3 = window_height - int(150*percent) self.SelBtn = wx.Button(self, label='选择图片', size=buttonSize) self.SelBtn.SetPosition(wx.Point(positionx_label3, positiony_label3)) self.SelBtn.Bind(wx.EVT_BUTTON, self.OnChooseFile) self.SelBtn.SetFont(labelFont) self.SelBtn.SetForegroundColour(labelColor) positionx_label4 = (window_width - imgSize[0]) // 2 positiony_label4 = (window_height - imgSize[1] - labelSize[1] - buttonSize[1]) // 2 self.image_ctrl = wx.StaticBitmap(self, size=imgSize) self.image_ctrl.SetPosition(wx.Point(positionx_label4, positiony_label4)) #添加菜单 self.appendmenus() self.SetWindowStyle(wx.STAY_ON_TOP | wx.CAPTION | wx.SYSTEM_MENU) self.SetTitle(APP_TITLE+'-'+self.sub_title) self.SetIcon(wx.Icon(APP_ICON)) self.SetBackgroundColour( wx.Colour( 245, 222, 179 ) ) self.Raise() self.Show(True) show_window = True myFrame = self.my_frame if not myFrame in myFrames: myFrames[myFrame] = self #绑定事件 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.timer.Start(3000) def OnChooseFile(self, event): global imgSize wildcard = 'All files(*.jpg)|*.jpg|*.png|*.gif' dialog = wx.FileDialog(None,'select',os.getcwd(),'',wildcard,wx.FD_OPEN) if dialog.ShowModal() == wx.ID_OK: fileName = dialog.GetPath() image = wx.Image(fileName, wx.BITMAP_TYPE_ANY) image.Rescale(imgSize[0], imgSize[1], wx.IMAGE_QUALITY_NORMAL) self.image_ctrl.SetBitmap(wx.Bitmap(image)) dialog.Destroy() def appendmenus(self): menu_bar = wx.MenuBar() gongneng_menu = wx.Menu() guanyu_menu = wx.Menu() index_item = gongneng_menu.Append(-1, "&首页", "返回首页") user_item1 = gongneng_menu.Append(-1, "&用户列表", "查看用户信息") tree_item1 = gongneng_menu.Append(-1, "&展示树", "查看树形结构") about_item = guanyu_menu.Append(-1, "&关于我们", "关于我们") self.Bind(wx.EVT_MENU, self.toindex, index_item) self.Bind(wx.EVT_MENU, self.touserlist, user_item1) self.Bind(wx.EVT_MENU, self.totree, tree_item1) self.Bind(wx.EVT_MENU, self.toabout, about_item) menu_bar.Append(gongneng_menu, "&功能") menu_bar.Append(guanyu_menu, "&关于") self.SetMenuBar(menu_bar) def logout(self, event): global username, password dlg = wx.MessageDialog(None, '确认退出吗?', '用户确认', wx.YES_NO) result = dlg.ShowModal() if result == wx.ID_YES: username = '' password = '' self.tologin(event) else: pass dlg.Destroy() return True def tologin(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'login' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def toindex(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'main' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def touserlist(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'user' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = UserFrame(None) show_window = True def totree(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'tree' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = TreeFrame(None) show_window = True def toabout(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'about' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = AboutFrame(None) show_window = True def OnTimer(self, event): thread_obj = threading.Thread(target=self.OnTimer1, args=(event, )) thread_obj.start() def OnTimer1(self, event): pass def __del__( self ): pass class UserFrame ( wx.Frame): sub_title = '用户列表' my_frame = 'user' page = 1 pagesize = 50 def __init__( self, parent ): global percent,labelSize,textSize,buttonSize,lineHeight,username,show_window,myFrames,myFrame wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE ) labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) labelColor = wx.Colour( 139, 69, 19 ) #设置窗口位置,大小 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) self.SetSize((window_width, window_height)) position_x = (screen_width - window_width) // 2 position_y = (screen_height - window_height) // 2 self.SetPosition(wx.Point(position_x, position_y)) labelSize = (int(310*percent), int(25*percent)) positionx_label1 = int(5*percent) positiony_label1 = int(5*percent) positionx_label2 = window_width - labelSize[0] positiony_label2 = int(5*percent) self.usernameLabel = wx.StaticText(self, label='', size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label1, positiony_label1)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if username == '': username = '未知用户' self.usernameLabel = wx.StaticText(self, label='登录用户:'+username, size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label2, positiony_label2)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if not username == '未知用户': self.usernameLabel.Bind( wx.EVT_LEFT_DOWN, self.logout ) self.showGridData(self.page) positionx_label4 = int(5*percent) positiony_label4 = int(930*percent) self.preButton = wx.Button( self, label='上一页', size=buttonSize) self.preButton.SetPosition(wx.Point(positionx_label4, positiony_label4)) self.preButton.SetFont(labelFont) self.preButton.SetForegroundColour(labelColor) self.preButton.Bind( wx.EVT_BUTTON, self.showGridDataPre ) positionx_label5 = int(160*percent) positiony_label5 = int(930*percent) self.nextButton = wx.Button( self, label='下一页', size=buttonSize) self.nextButton.SetPosition(wx.Point(positionx_label5, positiony_label5)) self.nextButton.SetFont(labelFont) self.nextButton.SetForegroundColour(labelColor) self.nextButton.Bind( wx.EVT_BUTTON, self.showGridDataNext ) #添加菜单 self.appendmenus() self.SetWindowStyle(wx.STAY_ON_TOP | wx.CAPTION | wx.SYSTEM_MENU) self.SetTitle(APP_TITLE+'-'+self.sub_title) self.SetIcon(wx.Icon(APP_ICON)) self.SetBackgroundColour( wx.Colour( 245, 222, 179 ) ) self.Raise() self.Show(True) show_window = True myFrame = self.my_frame if not myFrame in myFrames: myFrames[myFrame] = self #绑定事件 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.timer.Start(3000) def appendmenus(self): menu_bar = wx.MenuBar() gongneng_menu = wx.Menu() guanyu_menu = wx.Menu() index_item = gongneng_menu.Append(-1, "&首页", "返回首页") user_item1 = gongneng_menu.Append(-1, "&用户列表", "查看用户信息") tree_item1 = gongneng_menu.Append(-1, "&展示树", "查看树形结构") about_item = guanyu_menu.Append(-1, "&关于我们", "关于我们") self.Bind(wx.EVT_MENU, self.toindex, index_item) self.Bind(wx.EVT_MENU, self.touserlist, user_item1) self.Bind(wx.EVT_MENU, self.totree, tree_item1) self.Bind(wx.EVT_MENU, self.toabout, about_item) menu_bar.Append(gongneng_menu, "&功能") menu_bar.Append(guanyu_menu, "&关于") self.SetMenuBar(menu_bar) def showGridDataPre(self, event): global datas self.page = self.page - 1 totalpage = math.ceil(len(datas) / (self.pagesize + 0.00)) if self.page < 1: self.page = 1 if self.page > totalpage: self.page = totalpage self.showGridData(self.page) def showGridDataNext(self, event): global datas self.page = self.page + 1 totalpage = math.ceil(len(datas) / (self.pagesize + 0.00)) if self.page < 1: self.page = 1 if self.page > totalpage: self.page = totalpage self.showGridData(self.page) def showGridData(self, page): try: self.grid.Destroy() except: pass labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) gridSize = (int(1880*percent), int(885*percent)) positionx_label3 = int(5*percent) positiony_label3 = int(30*percent) self.grid = wx.grid.Grid(self, size=gridSize) self.grid.SetPosition(wx.Point(positionx_label3, positiony_label3)) self.grid.SetFont(labelFont) self.grid.SetRowLabelSize(25) # 设置行标签的宽度 self.grid.SetColLabelSize(25) # 设置列标签的宽度 self.grid.SetColLabelValue(0, "姓名") self.grid.SetColLabelValue(1, "性别") self.grid.SetColLabelValue(2, "年龄") self.grid.SetColLabelValue(3, "邮箱") self.grid.SetColLabelValue(4, "公司") self.grid.SetColLabelValue(5, "地址") cellColor = wx.Colour( 240, 255, 255 ) data = self.getData(page) self.grid.CreateGrid(len(data), len(data[0])) for row in range(len(data)): for col in range(len(data[row])): self.grid.SetCellValue(row, col, data[row][col]) self.grid.SetCellBackgroundColour(row, col, cellColor) if col in [0,1,2]: self.grid.SetColSize(col, int(215*percent)) elif col in [3]: self.grid.SetColSize(col, int(350*percent)) elif col in [4]: self.grid.SetColSize(col, int(350*percent)) elif col in [5]: self.grid.SetColSize(col, int(500*percent)) def getData(self, page): global datas totalpage = math.ceil(len(datas) / (self.pagesize + 0.00)) if page < 1: page = 1 if page > totalpage: page = totalpage start = (page - 1) * self.pagesize end = start + self.pagesize resdata = [] for i,data in enumerate(datas): if i>= start and i<end: resdata.append(data) return resdata def logout(self, event): global username, password dlg = wx.MessageDialog(None, '确认退出吗?', '用户确认', wx.YES_NO) result = dlg.ShowModal() if result == wx.ID_YES: username = '' password = '' self.tologin(event) else: pass dlg.Destroy() return True def tologin(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'login' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def toindex(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'main' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def touserlist(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'user' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = UserFrame(None) show_window = True def totree(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'tree' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = TreeFrame(None) show_window = True def toabout(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'about' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = AboutFrame(None) show_window = True def OnTimer(self, event): thread_obj = threading.Thread(target=self.OnTimer1, args=(event, )) thread_obj.start() def OnTimer1(self, event): pass def __del__( self ): pass class AboutFrame ( wx.Frame): sub_title = '关于我们' my_frame = 'about' def __init__( self, parent ): global percent,labelSize,textSize,buttonSize,lineHeight,username,abouttext,show_window,myFrames,myFrame wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE ) labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) labelColor = wx.Colour( 139, 69, 19 ) #设置窗口位置,大小 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) self.SetSize((window_width, window_height)) position_x = (screen_width - window_width) // 2 position_y = (screen_height - window_height) // 2 self.SetPosition(wx.Point(position_x, position_y)) labelSize = (int(310*percent), int(25*percent)) positionx_label1 = int(5*percent) positiony_label1 = int(5*percent) positionx_label2 = window_width - labelSize[0] positiony_label2 = int(5*percent) self.usernameLabel = wx.StaticText(self, label='', size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label1, positiony_label1)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if username == '': username = '未知用户' self.usernameLabel = wx.StaticText(self, label='登录用户:'+username, size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label2, positiony_label2)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if not username == '未知用户': self.usernameLabel.Bind( wx.EVT_LEFT_DOWN, self.logout ) textSize1 = (int(1880*percent), int(945*percent)) positionx_label3 = int(8*percent) positiony_label3 = int(30*percent) self.usernameInput = wx.TextCtrl(self, style=wx.TE_MULTILINE, size=textSize1) self.usernameInput.SetPosition(wx.Point(positionx_label3, positiony_label3)) self.usernameInput.SetValue(abouttext) #添加菜单 self.appendmenus() self.SetWindowStyle(wx.STAY_ON_TOP | wx.CAPTION | wx.SYSTEM_MENU) self.SetTitle(APP_TITLE+'-'+self.sub_title) self.SetIcon(wx.Icon(APP_ICON)) self.SetBackgroundColour( wx.Colour( 245, 222, 179 ) ) self.Raise() self.Show(True) show_window = True myFrame = self.my_frame if not myFrame in myFrames: myFrames[myFrame] = self #绑定事件 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.timer.Start(3000) def appendmenus(self): menu_bar = wx.MenuBar() gongneng_menu = wx.Menu() guanyu_menu = wx.Menu() index_item = gongneng_menu.Append(-1, "&首页", "返回首页") user_item1 = gongneng_menu.Append(-1, "&用户列表", "查看用户信息") tree_item1 = gongneng_menu.Append(-1, "&展示树", "查看树形结构") about_item = guanyu_menu.Append(-1, "&关于我们", "关于我们") self.Bind(wx.EVT_MENU, self.toindex, index_item) self.Bind(wx.EVT_MENU, self.touserlist, user_item1) self.Bind(wx.EVT_MENU, self.totree, tree_item1) self.Bind(wx.EVT_MENU, self.toabout, about_item) menu_bar.Append(gongneng_menu, "&功能") menu_bar.Append(guanyu_menu, "&关于") self.SetMenuBar(menu_bar) def logout(self, event): global username, password dlg = wx.MessageDialog(None, '确认退出吗?', '用户确认', wx.YES_NO) result = dlg.ShowModal() if result == wx.ID_YES: username = '' password = '' self.tologin(event) else: pass dlg.Destroy() return True def tologin(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'login' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def toindex(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'main' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def touserlist(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'user' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = UserFrame(None) show_window = True def totree(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'tree' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = TreeFrame(None) show_window = True def toabout(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'about' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = AboutFrame(None) show_window = True def OnTimer(self, event): thread_obj = threading.Thread(target=self.OnTimer1, args=(event, )) thread_obj.start() def OnTimer1(self, event): pass def __del__( self ): pass class TreeFrame ( wx.Frame): sub_title = '展示树' my_frame = 'tree' def __init__( self, parent ): global percent,labelSize,textSize,buttonSize,lineHeight,username,abouttext,show_window,myFrames,myFrame wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE ) labelFont = wx.Font(int(18*percent), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) labelColor = wx.Colour( 139, 69, 19 ) #设置窗口位置,大小 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) self.SetSize((window_width, window_height)) position_x = (screen_width - window_width) // 2 position_y = (screen_height - window_height) // 2 self.SetPosition(wx.Point(position_x, position_y)) labelSize = (int(310*percent), int(25*percent)) positionx_label1 = int(5*percent) positiony_label1 = int(5*percent) positionx_label2 = window_width - labelSize[0] positiony_label2 = int(5*percent) self.usernameLabel = wx.StaticText(self, label='', size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label1, positiony_label1)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if username == '': username = '未知用户' self.usernameLabel = wx.StaticText(self, label='登录用户:'+username, size=labelSize) self.usernameLabel.SetPosition(wx.Point(positionx_label2, positiony_label2)) self.usernameLabel.SetFont(labelFont) self.usernameLabel.SetForegroundColour(labelColor) if not username == '未知用户': self.usernameLabel.Bind( wx.EVT_LEFT_DOWN, self.logout ) textSize1 = (int(1880*percent), int(945*percent)) positionx_label3 = int(8*percent) positiony_label3 = int(30*percent) self.tree = wx.TreeCtrl(self, size=textSize1) self.tree.SetPosition(wx.Point(positionx_label3, positiony_label3)) self.root = self.tree.AddRoot("食物") self.zhushi = self.tree.AppendItem(self.root, "主食") self.shucai = self.tree.AppendItem(self.root, "蔬菜") self.shuiguo = self.tree.AppendItem(self.root, "水果") self.mifan = self.tree.AppendItem(self.zhushi, "米饭") self.miantiao = self.tree.AppendItem(self.zhushi, "面条") self.shuijiao = self.tree.AppendItem(self.zhushi, "水饺") self.chaocai = self.tree.AppendItem(self.zhushi, "炒菜") self.liangbancai = self.tree.AppendItem(self.zhushi, "凉拌菜") self.tree.AppendItem(self.shucai, "土豆") self.tree.AppendItem(self.shucai, "青椒") self.tree.AppendItem(self.shucai, "西红柿") self.tree.AppendItem(self.shucai, "茄子") self.tree.AppendItem(self.shucai, "青菜") self.tree.AppendItem(self.shucai, "芹菜") self.tree.AppendItem(self.shucai, "辣椒") self.tree.AppendItem(self.shucai, "豆角") self.tree.AppendItem(self.shuiguo, "西瓜") self.tree.AppendItem(self.shuiguo, "葡萄") self.tree.AppendItem(self.shuiguo, "桃子") self.tree.AppendItem(self.shuiguo, "苹果") self.tree.AppendItem(self.shuiguo, "橘子") self.tree.AppendItem(self.shuiguo, "哈密瓜") self.tree.AppendItem(self.miantiao, "炸酱面") self.tree.AppendItem(self.miantiao, "板面") self.tree.AppendItem(self.miantiao, "烩面") self.tree.AppendItem(self.miantiao, "刀削面") self.tree.AppendItem(self.miantiao, "扯面") self.tree.AppendItem(self.miantiao, "云吞面") self.tree.Expand(self.root) self.tree.Expand(self.zhushi) self.tree.Expand(self.miantiao) #添加菜单 self.appendmenus() self.SetWindowStyle(wx.STAY_ON_TOP | wx.CAPTION | wx.SYSTEM_MENU) self.SetTitle(APP_TITLE+'-'+self.sub_title) self.SetIcon(wx.Icon(APP_ICON)) self.SetBackgroundColour( wx.Colour( 245, 222, 179 ) ) self.Raise() self.Show(True) show_window = True myFrame = self.my_frame if not myFrame in myFrames: myFrames[myFrame] = self #绑定事件 self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.timer.Start(3000) def appendmenus(self): menu_bar = wx.MenuBar() gongneng_menu = wx.Menu() guanyu_menu = wx.Menu() index_item = gongneng_menu.Append(-1, "&首页", "返回首页") user_item1 = gongneng_menu.Append(-1, "&用户列表", "查看用户信息") tree_item1 = gongneng_menu.Append(-1, "&展示树", "查看树形结构") about_item = guanyu_menu.Append(-1, "&关于我们", "关于我们") self.Bind(wx.EVT_MENU, self.toindex, index_item) self.Bind(wx.EVT_MENU, self.touserlist, user_item1) self.Bind(wx.EVT_MENU, self.totree, tree_item1) self.Bind(wx.EVT_MENU, self.toabout, about_item) menu_bar.Append(gongneng_menu, "&功能") menu_bar.Append(guanyu_menu, "&关于") self.SetMenuBar(menu_bar) def logout(self, event): global username, password dlg = wx.MessageDialog(None, '确认退出吗?', '用户确认', wx.YES_NO) result = dlg.ShowModal() if result == wx.ID_YES: username = '' password = '' self.tologin(event) else: pass dlg.Destroy() return True def tologin(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'login' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def toindex(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'main' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = MainFrame(None) show_window = True def touserlist(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'user' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = UserFrame(None) show_window = True def totree(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'tree' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = TreeFrame(None) show_window = True def toabout(self, event): global myFrames,show_window,myFrame self.Hide() myFrame = 'about' if myFrame in myFrames: mfObj = myFrames[myFrame] mfObj.Show(True) show_window = True else: mfObj = AboutFrame(None) show_window = True def OnTimer(self, event): thread_obj = threading.Thread(target=self.OnTimer1, args=(event, )) thread_obj.start() def OnTimer1(self, event): pass def __del__( self ): pass if __name__ == "__main__": app = wx.App(False) login = LoginFrame(None) app.MainLoop()
截图:
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18349077