wxPython實現Frame之間的跳轉/更新的一種方法
?
wxPython是Python中重要的GUI框架,下面通過自己的方法實現模擬類似PC版微信登錄,并跳轉到主界面(朋友圈)的流程。
(一)項目目錄
?
【說明】
icon : 保存項目使用的圖片資源
wx_main.py : 項目入口文件,運行此文件可以看見效果。
loginFrame.py:登錄的界面的Frame定義繪制文件
contentFrame.py:登錄成功之后的界面Frame定義繪制文件
guiManager.py:界面創建和管理
utils.py:工具類,其中定義了一個獲取icon文件夾中文件全路徑的工具函數
xDialog.py:定義了有兩項輸入項的Dialog的樣式
?
(二)項目流程圖
?
【說明】
wxPython的應用入口是在wx.App()實現的,在OnInit()函數中創建要顯示的Frame對象,在wx.App子類中實現界面刷新的函數update(),并將其傳遞給新創建的Frame對象,在Frame需要觸發Frame更新的時候,通過這個回調函數update()來通知wx.App()進行Frame的更新。
?
(三)效果演示
略
(四)項目代碼
(4-1)wx_main.py
1 #coding=utf-8 2 3 import wx 4 import guiManager as FrameManager 5 6 class MainAPP(wx.App): 7 8 def OnInit(self): 9 self.manager = FrameManager.GuiManager(self.UpdateUI) 10 self.frame = self.manager.GetFrame(0) 11 self.frame.Show() 12 return True 13 14 def UpdateUI(self, type): 15 self.frame.Show(False) 16 self.frame = self.manager.GetFrame(type) 17 self.frame.Show(True) 18 19 def main(): 20 app = MainAPP() 21 app.MainLoop() 22 23 if __name__ == '__main__': 24 main()
(4-2)guiManager.py
1 #coding=utf-8 2 import loginFrame 3 import contentFrame 4 5 class GuiManager(): 6 def __init__(self, UpdateUI): 7 self.UpdateUI = UpdateUI 8 self.frameDict = {} # 用來裝載已經創建的Frame對象 9 10 def GetFrame(self, type): 11 frame = self.frameDict.get(type) 12 13 if frame is None: 14 frame = self.CreateFrame(type) 15 self.frameDict[type] = frame 16 17 return frame 18 19 def CreateFrame(self, type): 20 if type == 0: 21 return loginFrame.LoginFrame(parent=None, id=type, UpdateUI=self.UpdateUI) 22 elif type == 1: 23 return contentFrame.ContentFrame(parent=None, id=type, UpdateUI=self.UpdateUI)
(4-3)loginFrame.py
1 #coding=utf-8 2 import wx 3 # 導入wxPython中的通用Button 4 import wx.lib.buttons as wxButton 5 6 from utils import load_image 7 import xDialog 8 9 class LoginFrame(wx.Frame): 10 def __init__(self, parent=None, id=-1, UpdateUI=None): 11 wx.Frame.__init__(self, parent, id, title='登錄界面', size=(280, 400), pos=(500, 200)) 12 13 self.UpdateUI = UpdateUI 14 self.InitUI() # 繪制UI界面 15 16 def InitUI(self): 17 panel = wx.Panel(self) 18 19 logo_sys = wx.Image(load_image('logo_sys.png'), wx.BITMAP_TYPE_ANY).ConvertToBitmap() 20 wx.StaticBitmap(panel, -1, logo_sys, pos=(90, 90), size=(100, 100)) 21 22 logo_title = wx.StaticText(panel, -1, '天馬行空', pos=(120, 210)) 23 logo_title.SetForegroundColour('#0a74f7') 24 titleFont = wx.Font(13, wx.DEFAULT, wx.BOLD, wx.NORMAL, True) 25 logo_title.SetFont(titleFont) 26 27 button_Login = wxButton.GenButton(panel, -1, '登錄', pos=(40, 270), size=(200, 40), style=wx.BORDER_MASK) 28 button_Login.SetBackgroundColour('#0a74f7') 29 button_Login.SetForegroundColour('white') 30 self.Bind(wx.EVT_BUTTON, self.loginSys, button_Login) 31 32 33 def loginSys(self, event): 34 dlg = LoginDialog(self.loginFunction, '#0a74f7') 35 dlg.Show() 36 37 def loginFunction(self, account, password): 38 print '接收到用戶的輸入:', account, password 39 self.UpdateUI(1) #更新UI-Frame 40 41 class LoginDialog(xDialog.InputDialog): 42 def __init__(self, func_callBack, themeColor): 43 xDialog.InputDialog.__init__(self, '登錄系統', func_callBack, themeColor)
(4-4)contentFrame.py
1 #coding=utf-8 2 import wx 3 4 class ContentFrame(wx.Frame): 5 def __init__(self, parent=None, id=-1, UpdateUI=None): 6 wx.Frame.__init__(self, parent, -1, title='天馬行空的朋友圈', size=(400, 400), pos=(500, 200)) 7 8 self.UpdateUI = UpdateUI 9 self.InitUI() #繪制UI 10 11 def InitUI(self): 12 13 panel = wx.Panel(self) 14 wx.StaticText(panel, -1, u'歡迎您的到來!', pos=(30, 30))
(4-5)xDialog.py
1 #coding=utf-8 2 3 import wx 4 5 class InputDialog(wx.Dialog): 6 def __init__(self, title, func_callBack, themeColor): 7 wx.Dialog.__init__(self, None, -1, title, size=(300, 200)) 8 self.func_callBack = func_callBack 9 self.themeColor = themeColor 10 11 self.InitUI() #繪制Dialog的界面 12 13 def InitUI(self): 14 panel = wx.Panel(self) 15 16 font = wx.Font(14, wx.DEFAULT, wx.BOLD, wx.NORMAL, True) 17 18 accountLabel = wx.StaticText(panel, -1, '賬號', pos=(20, 25)) 19 accountLabel.SetForegroundColour(self.themeColor) 20 accountLabel.SetFont(font) 21 22 self.accountInput = wx.TextCtrl(panel, -1, u'', pos=(80, 25), size=(180, -1)) 23 self.accountInput.SetForegroundColour('gray') 24 self.accountInput.SetFont(font) 25 26 passwordLabel = wx.StaticText(panel, -1, '密碼', pos=(20, 70)) 27 passwordLabel.SetFont(font) 28 passwordLabel.SetForegroundColour(self.themeColor) 29 30 self.passwordInput = wx.TextCtrl(panel, -1, u'', pos=(80, 70), size=(180, -1), style=wx.TE_PASSWORD) 31 self.passwordInput.SetForegroundColour(self.themeColor) 32 self.passwordInput.SetFont(font) 33 34 sureButton = wx.Button(panel, -1, u'登錄', pos=(20, 130), size=(120, 40)) 35 sureButton.SetForegroundColour('white') 36 sureButton.SetBackgroundColour(self.themeColor) 37 # 為【確定Button】綁定事件 38 self.Bind(wx.EVT_BUTTON, self.sureEvent, sureButton) 39 40 cancleButton = wx.Button(panel, -1, u'取消', pos=(160, 130), size=(120, 40)) 41 cancleButton.SetBackgroundColour('black') 42 cancleButton.SetForegroundColour('#ffffff') 43 # 為【取消Button】綁定事件 44 self.Bind(wx.EVT_BUTTON, self.cancleEvent, cancleButton) 45 46 def sureEvent(self, event): 47 account = self.accountInput.GetValue() 48 password = self.passwordInput.GetValue() 49 # 通過回調函數傳遞數值 50 self.func_callBack(account, password) 51 self.Destroy() #銷毀隱藏Dialog 52 53 def cancleEvent(self, event): 54 self.Destroy() #銷毀隱藏Dialog
(4-6)utils.py
1 #coding=utf-8 2 import os.path 3 4 main_dir = os.path.split(os.path.abspath(__file__))[0] 5 6 # 返回icon中文件的系統文件路徑 7 def load_image(file): 8 filePath = os.path.join(main_dir, 'icon', file) 9 return filePath
蕩的,不是本人作品