码迷,mamicode.com
首页 > 编程语言 > 详细

wxpython实现界面跳转

时间:2018-04-30 22:20:09      阅读:854      评论:0      收藏:0      [点我收藏+]

标签:sel   sys   登录界面   normal   reg   文件路径   for   class   turn   

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

荡的,不是本人作品

wxpython实现界面跳转

标签:sel   sys   登录界面   normal   reg   文件路径   for   class   turn   

原文地址:https://www.cnblogs.com/qdzj/p/8974688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!