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

 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389753.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java职业技能了解精通_如何通过精通数字分析来提升职业生涯的发展,第8部分...

java职业技能了解精通Continuing from the seventh article in this series, we are going to explore ways to present data. Over the past few years, Marketing and SEO field has become more data-driven than in the past thanks to tools like Google Webmaster Tools …

2028. 找出缺失的观测数据

2028. 找出缺失的观测数据 现有一份 n m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 1 到 6 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好你有之前计算过的这 n m 次投掷数据的 平均值 。 给你一个长度为 m 的整数数组 …

51nod 1250 排列与交换——dp

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId1250 仔细思考dp。 第一问,考虑已知 i-1 个数有多少种方案。再放入一个数,它是最大的且在最后面,所以它的位置不同的话,就是不同的方案。它在特定…

BZOJ.1024.[SCOI2009]生日快乐(记忆化搜索)

题目链接 搜索,枚举切的n-1刀。 对于长n宽m要切x刀,可以划分为若干个 长n宽m要切x刀 的子问题,对所有子问题的答案取max 对所有子问题的方案取min 就是当前状态答案。 这显然是会有很多重复状态的,用map记忆化(长宽都是double)。 …

kfc流程管理炸薯条几秒_炸薯条成为数据科学的最后前沿

kfc流程管理炸薯条几秒In February, our Data Science team had an argument about which restaurant we went to made the best French Fry.2月,我们的数据科学团队对我们去哪家餐厅做得最好的炸薯条产生了争议。 We decided to make it a competition throughout…

XSS理解与防御

一、说明 我说我不理解为什么别人做得出来我做不出来,比如这里要说的XSS我觉得很多人就不了解其定义和原理的,在不了解定义和原理的背景下他们可以拿站,这让人怎么理解呢。那时我最怕两个问题,第一个是题目做得怎么样第二个是你能…

Java大数

转自:https://www.cnblogs.com/zufezzt/p/4794271.html import java.util.*; import java.math.*; public class Main{public static void main(String args[]){Scanner cin new Scanner(System.in);BigInteger a, b;//以文件EOF结束while (cin.hasNext()){a cin.…

2027. 转换字符串的最少操作次数

2027. 转换字符串的最少操作次数 给你一个字符串 s ,由 n 个字符组成,每个字符不是 ‘X’ 就是 ‘O’ 。 一次 操作 定义为从 s 中选出 三个连续字符 并将选中的每个字符都转换为 ‘O’ 。注意,如果字符已经是 ‘O’ ,只需要保持…

bigquery_到Google bigquery的sql查询模板,它将您的报告提升到另一个层次

bigqueryIn this post, we’re sharing report templates that you can build with SQL queries to Google BigQuery data.在本文中,我们将分享您可以使用SQL查询为Google BigQuery数据构建的报告模板。 First, you’ll find out about what you can calculate wit…

分类树/装袋法/随机森林算法的R语言实现

原文首发于简书于[2018.06.12] 本文是我自己动手用R语言写的实现分类树的代码,以及在此基础上写的袋装法(bagging)和随机森林(random forest)的算法实现。全文的结构是: 分类树 基本知识predginisplitrules…

数据科学学习心得_学习数据科学时如何保持动力

数据科学学习心得When trying to learn anything all by yourself, it is easy to lose motivation and get thrown off track.尝试自己学习所有东西时,很容易失去动力并偏离轨道。 In this article, I will provide you with some tips that I used to stay focus…

用php当作cat使用

今天,本来是想敲 node test.js 执行一下,test.js文件,结果 惯性的敲成了 php test.js, 原文输出了 test.js的内容。 突然觉得,这东西 感觉好像是 cat 命令,嘿嘿,以后要是ubuntu 上没装 cat , …

建信01. 间隔删除链表结点

建信01. 间隔删除链表结点 给你一个链表的头结点 head,每隔一个结点删除另一个结点(要求保留头结点)。 请返回最终链表的头结点。 示例 1: 输入:head [1,2,3,4] 输出: [1,3] 解释: 蓝色结点为删除的结点…

python安装Crypto:NomodulenamedCrypto.Cipher

python 安装Crypto时出现的错误:NomodulenamedCrypto.Cipher 首先直接pip install Crypto 这时会在lib/site-packages/ 文件夹下生成crypto文件夹,将其重命名为Crypto ...然而这个文件夹下没有Cipher模块,还需要pip安装一个pycrypto,不过wind…

python多项式回归_在python中实现多项式回归

python多项式回归Video Link影片连结 You can view the code used in this Episode here: SampleCode您可以在此处查看 此剧 集中使用的代码: SampleCode 导入我们的数据 (Importing our Data) The first step is to import our data into python.第一步是将我们的…

Uboot 命令是如何被使用的?

有什么问题请 发邮件至syyxyoutlook.com, 欢迎交流~ 在uboot代码中命令的模式是这个样子: 这样是如何和命令行交互的呢? 在command.h 中, 我们可以看到如下宏定义 将其拆分出来: #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ U_…

2029. 石子游戏 IX

2029. 石子游戏 IX Alice 和 Bob 再次设计了一款新的石子游戏。现有一行 n 个石子,每个石子都有一个关联的数字表示它的价值。给你一个整数数组 stones ,其中 stones[i] 是第 i 个石子的价值。 Alice 和 Bob 轮流进行自己的回合,Alice 先手…

大数据可视化应用_在数据可视化中应用种族平等意识

大数据可视化应用The following post is a summarized version of the article accepted to the 2020 Visualization for Communication workshop as part of the 2020 IEEE VIS conference to be held in October 2020. The full paper has been published as an OSF Preprint…

Windows10电脑系统时间校准

有时候新安装电脑系统,系统时间不对,需要主动去校准系统时间。1、点击时间 2、日期和时间设置 3、其他日期、时间和区域设置 4、设置时间和日期 5、Internet 时间 6、点击立即更新,如果更新失败就查电脑是否已联网,重试点击立即更…

webpack问题

Cannot find module webpack/lib/node/NodeTemplatePlugin 全局:npm i webpack -g npm link webpack --save-dev 转载于:https://www.cnblogs.com/doing123/p/8994269.html