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. DB Query Analyzer 6.04 is distributed, 78 articles concerned have been published

        DB Query Analyzer 6.04 is distributed,78 articles concerned have been published  DB Query Analyz ...

  2. 设计模式之——工厂模式(B)

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41142929 工厂方法模式定义了一个创建对象的接口,但由子 ...

  3. 面向对象编程(OOP)的五大特征-java学习之旅(1)

    这是Alan Kay关于第一个成功的面向对象语言SmallTalk的总结: 1.所有的东西都是对象.可将对象想象成一种新型的变量:它保存着数据,但是可要求它对自身进行操作,理论上讲,可从要解决的问题身 ...

  4. org/w3c/dom/ElementTraversal 错误解决办法

    org/w3c/dom/ElementTraversal 错误解决办法 不记得之前几天把什么maven依赖包删除了,今天利用htmlunit运行代码的时候报了下面的错误: Exception in t ...

  5. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

  6. Linux 进程等待队列

    Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制. 在这个链表中,有两种数据结构:等待队列头(wait_queue_head_t)和等待 ...

  7. 基于VB中WINSOCK控件的网上象棋系统的实现

    本文发表在<微型机与应用>杂志2001年第3期. 基于VB中WINSOCK控件的网上象棋系统的实现 马根峰1   ,  孙艳2  , 王平1 (1.重庆邮电学院自动化学院,重庆,40006 ...

  8. Javascript和BHO的相互调用简介

    v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...

  9. Android下VideoView的研究

    VideoView继承自SurfaceView,实现了MediaController.MediaPlayerControl的接口.在android系统中的包名为android.widget.Video ...

  10. saiku运行时报错max_length_for_sort_data 需要set higher

     infiniDB或者mysql数据库,运行时,按某个字段排序会出错.报错:max_length_for_sort_data  ... set higher. saiku报错, 也是这样. 这是数 ...