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

 #coding=utf-8

 import wx
import guiManager as FrameManager class MainAPP(wx.App): def OnInit(self):
self.manager = FrameManager.GuiManager(self.UpdateUI)
self.frame = self.manager.GetFrame(0)
self.frame.Show()
return True def UpdateUI(self, type):
self.frame.Show(False)
self.frame = self.manager.GetFrame(type)
self.frame.Show(True) def main():
app = MainAPP()
app.MainLoop() if __name__ == '__main__':
main()

(4-2)guiManager.py

 #coding=utf-8
import loginFrame
import contentFrame class GuiManager():
def __init__(self, UpdateUI):
self.UpdateUI = UpdateUI
self.frameDict = {} # 用来装载已经创建的Frame对象 def GetFrame(self, type):
frame = self.frameDict.get(type) if frame is None:
frame = self.CreateFrame(type)
self.frameDict[type] = frame return frame def CreateFrame(self, type):
if type == 0:
return loginFrame.LoginFrame(parent=None, id=type, UpdateUI=self.UpdateUI)
elif type == 1:
return contentFrame.ContentFrame(parent=None, id=type, UpdateUI=self.UpdateUI)

(4-3)loginFrame.py

 #coding=utf-8
import wx
# 导入wxPython中的通用Button
import wx.lib.buttons as wxButton from utils import load_image
import xDialog class LoginFrame(wx.Frame):
def __init__(self, parent=None, id=-1, UpdateUI=None):
wx.Frame.__init__(self, parent, id, title='登录界面', size=(280, 400), pos=(500, 200)) self.UpdateUI = UpdateUI
self.InitUI() # 绘制UI界面 def InitUI(self):
panel = wx.Panel(self) logo_sys = wx.Image(load_image('logo_sys.png'), wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(panel, -1, logo_sys, pos=(90, 90), size=(100, 100)) logo_title = wx.StaticText(panel, -1, '天马行空', pos=(120, 210))
logo_title.SetForegroundColour('#0a74f7')
titleFont = wx.Font(13, wx.DEFAULT, wx.BOLD, wx.NORMAL, True)
logo_title.SetFont(titleFont) button_Login = wxButton.GenButton(panel, -1, '登录', pos=(40, 270), size=(200, 40), style=wx.BORDER_MASK)
button_Login.SetBackgroundColour('#0a74f7')
button_Login.SetForegroundColour('white')
self.Bind(wx.EVT_BUTTON, self.loginSys, button_Login) def loginSys(self, event):
dlg = LoginDialog(self.loginFunction, '#0a74f7')
dlg.Show() def loginFunction(self, account, password):
print '接收到用户的输入:', account, password
self.UpdateUI(1) #更新UI-Frame class LoginDialog(xDialog.InputDialog):
def __init__(self, func_callBack, themeColor):
xDialog.InputDialog.__init__(self, '登录系统', func_callBack, themeColor)

(4-4)contentFrame.py

 #coding=utf-8
import wx class ContentFrame(wx.Frame):
def __init__(self, parent=None, id=-1, UpdateUI=None):
wx.Frame.__init__(self, parent, -1, title='天马行空的朋友圈', size=(400, 400), pos=(500, 200)) self.UpdateUI = UpdateUI
self.InitUI() #绘制UI def InitUI(self): panel = wx.Panel(self)
wx.StaticText(panel, -1, u'欢迎您的到来!', pos=(30, 30))

(4-5)xDialog.py

 #coding=utf-8

 import wx

 class InputDialog(wx.Dialog):
def __init__(self, title, func_callBack, themeColor):
wx.Dialog.__init__(self, None, -1, title, size=(300, 200))
self.func_callBack = func_callBack
self.themeColor = themeColor self.InitUI() #绘制Dialog的界面 def InitUI(self):
panel = wx.Panel(self) font = wx.Font(14, wx.DEFAULT, wx.BOLD, wx.NORMAL, True) accountLabel = wx.StaticText(panel, -1, '账号', pos=(20, 25))
accountLabel.SetForegroundColour(self.themeColor)
accountLabel.SetFont(font) self.accountInput = wx.TextCtrl(panel, -1, u'', pos=(80, 25), size=(180, -1))
self.accountInput.SetForegroundColour('gray')
self.accountInput.SetFont(font) passwordLabel = wx.StaticText(panel, -1, '密码', pos=(20, 70))
passwordLabel.SetFont(font)
passwordLabel.SetForegroundColour(self.themeColor) self.passwordInput = wx.TextCtrl(panel, -1, u'', pos=(80, 70), size=(180, -1), style=wx.TE_PASSWORD)
self.passwordInput.SetForegroundColour(self.themeColor)
self.passwordInput.SetFont(font) sureButton = wx.Button(panel, -1, u'登录', pos=(20, 130), size=(120, 40))
sureButton.SetForegroundColour('white')
sureButton.SetBackgroundColour(self.themeColor)
# 为【确定Button】绑定事件
self.Bind(wx.EVT_BUTTON, self.sureEvent, sureButton) cancleButton = wx.Button(panel, -1, u'取消', pos=(160, 130), size=(120, 40))
cancleButton.SetBackgroundColour('black')
cancleButton.SetForegroundColour('#ffffff')
# 为【取消Button】绑定事件
self.Bind(wx.EVT_BUTTON, self.cancleEvent, cancleButton) def sureEvent(self, event):
account = self.accountInput.GetValue()
password = self.passwordInput.GetValue()
# 通过回调函数传递数值
self.func_callBack(account, password)
self.Destroy() #销毁隐藏Dialog def cancleEvent(self, event):
self.Destroy() #销毁隐藏Dialog

(4-6)utils.py

 #coding=utf-8
import os.path main_dir = os.path.split(os.path.abspath(__file__))[0] # 返回icon中文件的系统文件路径
def load_image(file):
filePath = os.path.join(main_dir, 'icon', file)
return filePath

荡的,不是本人作品

wxpython实现界面跳转的更多相关文章

  1. Android 手机卫士--设置界面&功能列表界面跳转逻辑处理

    在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...

  2. iOS界面跳转的一些优化方案

    原文地址: http://blog.startry.com/2016/02/14/Think-Of-UIViewController-Switch/ iOS界面跳转的一些优化方案 App应用程序开发, ...

  3. Android activity界面跳转动画

    实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...

  4. 如何优雅的实现界面跳转 之 统跳协议 - DarwinNativeRouter

    PS 感谢大家的关注,由于我本想开源4个库,除了router, 另外三个分别是native dispatcher, web dispatcher 和 react dispatcher , 所以rout ...

  5. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...

  6. PyQt5实现界面跳转

    网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下 一.首先是主界面 1 # -*- coding: utf-8 -*- # Form implementatio ...

  7. Android原生和H5交互;Android和H5混合开发;WebView点击H5界面跳转到Android原生界面。

    当时业务的需求是这样的,H5有一个活动商品列表的界面,IOS和Android共用这一个界面,点击商品可以跳转到Android原生的商品详情界面并传递商品ID:  大概就是点击H5界面跳转到Androi ...

  8. ios的两种界面跳转方式

    1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...

  9. Android之Activity界面跳转--生命周期方法调用顺序

    这本是一个很基础的问题,很惭愧,很久没研究这一块了,已经忘得差不多了.前段时间面试,有面试官问过这个问题.虽然觉得没必要记,要用的时候写个Demo,打个Log就清楚了.但是今天顺手写了个Demo,也就 ...

随机推荐

  1. 【翻译】Ext JS 6 Beta发布

    原文:Ext JS 6 Beta is Now Available 概述 Ext JS 6的好处 新的Ext JS功能和工具 需要你的反馈意见 概述 很高兴,Ext JS 6 beta版本现在发布了. ...

  2. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十一)

    机器人发射子弹已经完成了,下面看看怎么给玩家设置障碍. 大家从上篇的图可以看到,在机器和篮筐直接有若干障碍物.我们先看如何实现它们. 打开SpriteBuilder,在Sprites文件夹中新建Sma ...

  3. Java进阶(二十一)java 空字符串与null区别

    java 空字符串与null区别 1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ; ""表示的是一个空字符串, ...

  4. ROS_RGB-D SLAM学习笔记--室内环境测试

    ROS_RGB-D SLAM学习笔记 RTAB-Map's ros-pkg. RTAB-Map is a RGB-D SLAM approach with real-time constraints. ...

  5. 使用JCrop进行图片裁剪,裁剪js说明,裁剪预览,裁剪上传,裁剪设计的图片处理的工具类和代码

     1.要想制作图片裁剪功能,可以使用网上的裁剪工具JCrop,网址是:https://github.com/tapmodo/Jcrop/ 案例效果如下: 2.引入JCrop的js代码,具体要引入那 ...

  6. 仿百度壁纸客户端(五)——实现搜索动画GestureDetector手势识别,动态更新搜索关键字

    仿百度壁纸客户端(五)--实现搜索动画GestureDetector手势识别,动态更新搜索关键字 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Frag ...

  7. 【面试笔试算法】Program 3 : Complicated Expression(网易游戏笔试题)

    时间限制:50000ms 单点时限:5000ms 内存限制:256MB 描述 在lisp语言中,表达式都用前缀法表示,例如,1 + 2 在lisp中使用(+ 1 2)来表示,其中,表达式的括号是必需的 ...

  8. Hbase replication操作

    1.修改hbase-site.xml文件 <property>  <name>hbase.replication</name>  <value>true ...

  9. SharePoint 2013 页面访问,Url中间多一段&quot;_layouts/15/start.aspx#&quot;

    问题描述: 我想访问如下页面 http://Host/_layouts/15/ManageFeatures.aspx 点击以后页面地址没有错,但是中间多了一段"_layouts/15/sta ...

  10. 关于java和c++中布尔量的比较

    在c++中允许 bool 量和 int 整形常量相互转换,并且用cout<<true; 在控制台上可以输出为 1 int main(int argc, _TCHAR* argv[]) { ...