wxpython实现界面跳转
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实现界面跳转的更多相关文章
- Android 手机卫士--设置界面&功能列表界面跳转逻辑处理
在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...
- iOS界面跳转的一些优化方案
原文地址: http://blog.startry.com/2016/02/14/Think-Of-UIViewController-Switch/ iOS界面跳转的一些优化方案 App应用程序开发, ...
- Android activity界面跳转动画
实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...
- 如何优雅的实现界面跳转 之 统跳协议 - DarwinNativeRouter
PS 感谢大家的关注,由于我本想开源4个库,除了router, 另外三个分别是native dispatcher, web dispatcher 和 react dispatcher , 所以rout ...
- ios中的界面跳转方式
ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...
- PyQt5实现界面跳转
网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下 一.首先是主界面 1 # -*- coding: utf-8 -*- # Form implementatio ...
- Android原生和H5交互;Android和H5混合开发;WebView点击H5界面跳转到Android原生界面。
当时业务的需求是这样的,H5有一个活动商品列表的界面,IOS和Android共用这一个界面,点击商品可以跳转到Android原生的商品详情界面并传递商品ID: 大概就是点击H5界面跳转到Androi ...
- ios的两种界面跳转方式
1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...
- Android之Activity界面跳转--生命周期方法调用顺序
这本是一个很基础的问题,很惭愧,很久没研究这一块了,已经忘得差不多了.前段时间面试,有面试官问过这个问题.虽然觉得没必要记,要用的时候写个Demo,打个Log就清楚了.但是今天顺手写了个Demo,也就 ...
随机推荐
- UNIX环境高级编程——非阻塞设置
非阻塞I/O使我们可以调用open.read和write这样的I/O操作,并使这些操作不会永远阻塞.如果这种操作不能完成, 则调用立即出错返回,表示该操作如继续执行将阻塞. 对于一个给定的描述符有两种 ...
- Linux中的端口占用问题
本文将会阐述两种解决端口占用的方法. 本文会用到的服务器端的程序如下: #include "unp.h" #include <time.h> int main(int ...
- Andoird Crash的跟踪方法,使用腾讯Bugly来捕捉一些疑难杂症,让我们APP稳定上线
Andoird Crash的跟踪方法,使用腾讯Bugly来捕捉一些疑难杂症,让我们APP稳定上线 我们在开发中常常会注意到一些Crash,这正是很头疼的,而且Crash会带来很多意想不到的状态,很恶心 ...
- EXCEPTION与ERROR的区别
EXCEPTION与ERROR的区别
- polaris: session和middleware的支持
起因 polaris虽然是模仿tornado开发,但我觉得作为一个go的web框架,还需要提供一些额外的扩展支持. polaris现在已经支持session以及middleware,主要参加djang ...
- Django项目导入Eclipse运行调试
受不了没有调试的感觉. 前提是Eclipse插件已经安装完成并且可以成功运行例子.参考:http://blog.csdn.net/jerome_s/article/details/46340079 1 ...
- [问与答]Python 中 __all__ 的作用 ?
你要是看Python的源码或者相关框架的源码,总是在 __init__.py 或者是源文件的开头看到一个 __all__ 变量的定义,今天就说说它的作用. orangleliu 问题出处 Can so ...
- mysql进阶(七)limit的用法
limit是mysql的语法 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条. select * ...
- mysql进阶(八)怎么对varchar类型排序问题
MySQL中怎么对varchar类型排序问题 asc 升级 desc降序 在mysql默认order by 只对数字与日期类型可以排序,但对于varchar字符型类型排序好像没有用了,下面我来给各位同 ...
- 跟我一起写Makefile(转)
这是我见过最全的Makefile编写指南:跟我一起写Makefile. PDF版本可以从这里下载得到.