前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wxPython_04_实现Frame之间的跳转/更新的一种方法

wxPython_04_实现Frame之间的跳转/更新的一种方法

作者头像
码农帮派
发布2020-04-01 15:21:12
1.1K0
发布2020-04-01 15:21:12
举报
文章被收录于专栏:码农帮派码农帮派

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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

源码下载地址:

http://download.csdn.net/detail/tianmaxingkong_/9693699

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农帮派 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档