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

荡的,不是本人作品

转载于:https://www.cnblogs.com/qdzj/p/8974688.html

wxpython实现界面跳转相关推荐

  1. HarmonyOS 界面跳转以及界面跳转的同时传递参数

    HarmonyOS 不带参数界面之间跳转 package com.example.myapplication.slice; import com.example.myapplication.Resou ...

  2. # 利用fragment实现界面跳转

    利用fragment实现界面跳转 任务要求 利用fragment实现界面跳转功能,完成效果如下图所示 图片1 我的想法是在xml文件里设置Button.再创建一个fragment文件.在java文件中 ...

  3. swift_004(Storyboard进行界面跳转及传值)

    Storyboard进行界面跳转及传值 方法一 // 方法一 跳转(不用连线) // 获取指定的Storyboard,name填写Storyboard的文件名 let mainStoryboard = ...

  4. 【错误记录】Flutter 界面跳转报错 ( Navigator operation requested with a context that does not include a Naviga )

    文章目录 一.报错信息 二.问题分析 三.解决方案 一.报错信息 Flutter 界面跳转时 , 报如下错误 : ======== Exception caught by gesture ====== ...

  5. 【鸿蒙 HarmonyOS】界面跳转 ( Page Ability 的 action 标识 | Page Ability 之间的界面跳转及传递数据 | 鸿蒙工程下创建 Module | 代码示例 )

    文章目录 一.Page Ability 的 action 标识 二.Page Ability 之间的界面跳转及传递数据 三.鸿蒙工程下创建 Module 四.代码示例 五.执行效果 参考文档 : Pa ...

  6. 【鸿蒙 HarmonyOS】界面跳转 ( AbilitySlice 之间的界面跳转 | AbilitySlice 之间的值传递 )

    文章目录 一.AbilitySlice 之间的界面跳转 二.完整代码示例 三.运行结果 参考文档 : Page Ability 基本概念 Page Ability 声明周期 AbilitySlice ...

  7. 微信小程序界面跳转(2)——按钮

    微信小程序界面跳转(2)--按钮 步骤一:创建一个新界面catPage界面: 先在pages文件夹下创建一个新的文件夹:catPage.单击右键,创建page,page名称和文件夹名称一致.最后创建结 ...

  8. 微信小程序界面跳转(1)

    界面跳转1:点击文字进行跳转 从logs界面跳转到one界面: 步骤一:打开logs.wxml文件,加入如图所示代码:(图后给出可复制代码) <view class="moto-con ...

  9. 1.8 ionic3入门——测滑菜单(side menu)中的界面跳转

    (1)如1.3 所写,测滑菜单写在app.html,所以测滑菜单中的各个按钮的实现就在app.component.ts中写了,如果像其他普通界面一样在app.component.ts中引入NavCon ...

最新文章

  1. C/C++中“#”和“##”的作用和用法
  2. mybatis相关知识
  3. @html.ActionLink的几种参数格式
  4. CSS图片廊实例详解
  5. python中的抽象含义_Python中下划线的5种含义你都知道吗?
  6. linux 脚本中除法运算符,Linux中Shell的算数运算符和位运算符用法笔记
  7. 这几天有django和python做了一个多用户博客系统(可选择模板) 没完成,先分享下...
  8. php实现多条件查找分页,Yii2.0框架实现带分页的多条件搜索功能示例
  9. 便携本市场一片混乱 东芝也加入战斗
  10. C++输入输出流重载
  11. 中国酸性蒸汽清洗系统市场趋势报告、技术动态创新及市场预测
  12. 《SOA In the real world》第一章译稿(含下载)
  13. 酒店系统服务器怎么修改时间,酒店服务器设置
  14. 自学-Linux-老男孩Linux77期-day2
  15. 由QCustomplot引发drawPolyline和drawLine的区别
  16. 秀米的编辑的图片无法显示
  17. Python练习数据结构笔记
  18. 扡扫机器人_石头扫地机器人评测:扫拖一体 指哪扫哪
  19. 使用EXCEL计算日期差时间差
  20. Partial RenderPartial Action RenderAction 区别和用法

热门文章

  1. android 坐标点计算器,Android实现简易计算器
  2. 云南省农村信用社计算机岗位待遇如何,云南农村信用社薪资待遇如何?
  3. mysql与jframe_java-如何在JFrame上显示从mysql检索到的图像
  4. 图片资源 php,php URL图片资源传参生成对应尺寸图片
  5. Linux监控CPU关闭服务器,监控Linux服务器CPU和内存
  6. 与传统的计算机硬件系统相比,计算机一级名词解释
  7. 5页面title样式修改_认识html:实现网站页面是这么简单的一回事
  8. 八皇后时间复杂度_【算法打卡】N皇后
  9. python 修改array_python 基础_ 数组的 增删改查3
  10. c# 中关键字_C#中的“使用”关键字