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,也就 ...
随机推荐
- Chapter 2 User Authentication, Authorization, and Security(10):创建包含数据库
原文出处:http://blog.csdn.net/dba_huangzj/article/details/39473895,专题目录:http://blog.csdn.net/dba_huangzj ...
- Chapter 2 User Authentication, Authorization, and Security(5):使用固定服务器角色
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38844999,专题目录:http://blog.csdn.net/dba_huangzj ...
- Linux信号实践(5) --时间与定时器
三种不同精度的睡眠 1.sleep #include <unistd.h> unsigned int sleep(unsigned int seconds); RETURN VALUE Z ...
- go-mysql: database/sql 接口适配
go-mysql已经支持golang database/sql接口,并通过https://github.com/bradfitz/go-sql-test测试用例. 现在go-mysql可以直接通过go ...
- 海量数据挖掘MMDS week3:社交网络之社区检测:高级技巧
http://blog.csdn.net/pipisorry/article/details/49052255 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- UVa - 1616 - Caravan Robbers
二分找到最大长度,最后输出的时候转化成分数,比较有技巧性. AC代码: #include <iostream> #include <cstdio> #include <c ...
- sed命令 linux
sed 实用工具是一个"编辑器",但它与其它大多数编辑器不同.除了不面向屏幕之外,它还是非交互式的.这意味着您必须将要对数据执行的命令插入到命令行或要处 理的脚本中.当显示它时,请 ...
- Android 免Root实现Apk静默安装,覆盖兼容市场主流的98%的机型
地址:http://blog.csdn.net/sk719887916/article/details/46746991 作者: skay 最近在做apk自我静默更新,在获取内置情况下,或者已root ...
- Python学习笔记 - map reduce
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def f(x): return x * x r = map(f, [1, 2, 3, 4, 5, 6, ...
- spring 注解模式 详解
Spring基于注解实现Bean定义支持如下三种注解: Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图12-1所示: JSR-2 ...