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

 

 

源码下载地址:

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

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值