wxPython中帧之间的全局变量 [英] Global variable between frames in wxPython

查看:28
本文介绍了wxPython中帧之间的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在子框架中定义的变量.我不知道如何在子框架中成功登录事件后将用户名拉回主框架.我想更新静态标签以显示用户名.

I wanted to use a variable defined in a child frame. I can't figure out how to pull back the username after a successful login event in the child frame back to the main frame. I wanted to update the static label to show the username.

import wx
import MySQLdb
import sys
username = ""

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, title = 'Application Title',style = wx.SYSTEM_MENU)
        panel = wx.Panel(self)
        menubar = wx.MenuBar()
        panel.SetBackgroundColour((200,200,200))
        vbox = wx.BoxSizer(wx.HORIZONTAL)
        midPan = wx.Panel(panel)
        midPan.SetBackgroundColour((100,0,0))
        pan2 = wx.Panel(panel)
        pan2.SetBackgroundColour((0,100,0))
        vbox.Add(pan2, 1, wx.EXPAND | wx.ALL)
        vbox.Add(midPan, 5, wx.EXPAND | wx.ALL)
        hbox = wx.BoxSizer(wx.VERTICAL)
        hbox1=wx.BoxSizer(wx.VERTICAL)
        h1=wx.Panel(midPan)
        h1.SetBackgroundColour((200,0,100))
        h2=wx.Panel(midPan)
        h2.SetBackgroundColour((0,100,200))
#######################################################################
        text1 = wx.StaticText(h1, label="Welcome ") #insert the global variable in this label after the user has successfully logged in
        text2 = wx.StaticText(h1, label="Label2")
        hbox.Add(h1,3,wx.EXPAND|wx.ALL,20)
        hbox.Add(h2,1,wx.EXPAND|wx.ALL,20)
        hbox1.Add(text1,1,wx.ALIGN_LEFT)        
        hbox1.Add(text2,1,wx.ALIGN_RIGHT)  
        panel.SetSizer(vbox)
        midPan.SetSizer(hbox)
        h1.SetSizer(hbox1)
        self.statusbar = self.CreateStatusBar()
        self.SetMenuBar(menubar)
        self.Centre()
        self.Maximize()
        self.Show(True)
        frame = LoginFrame(self)
        frame.Show(True)
        frame.MakeModal(True)

    def OnExit(self, e):
        self.Close()

class LoginFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent,style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX | wx.CLOSE_BOX | wx.MINIMIZE_BOX ), title = "System Login")
        self.panel = wx.Panel(self)     
        self.userlabel = wx.StaticText(self.panel, label="Username:")
        self.passlabel = wx.StaticText(self.panel, label="Password:")
        self.userbox = wx.TextCtrl(self.panel, size=(140, -1))
        self.passbox = wx.TextCtrl(self.panel, size=(140, -1), style=wx.TE_PASSWORD)
        self.login = wx.Button(self.panel, label="Login")
        self.exit = wx.Button(self.panel, label="Exit")
        self.Centre()
        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)        
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.userlabel, (0, 0))
        self.sizer.Add(self.userbox, (0, 1))
        self.sizer.Add(self.passlabel, (1, 0))
        self.sizer.Add(self.passbox, (1, 1))
        self.sizer.Add(self.login, (2, 0))
        self.sizer.Add(self.exit, (2, 1))
        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
        self.panel.SetSizerAndFit(self.border)  
        self.SetSizerAndFit(self.windowSizer)  
        self.login.Bind(wx.EVT_BUTTON, self.OnLogin)
        self.exit.Bind(wx.EVT_BUTTON, self.OnExit)

    def OnLogin(self, e):
        db = MySQLdb.connect("localhost","root","","test" )
        cursor = db.cursor()
        username = self.userbox.GetValue()
        password = self.passbox.GetValue()
        try:
            cursor.execute("SELECT * FROM useraccounts WHERE username=%s" ,username)
            data = cursor.fetchall()
            for row in data:
                dbpass = row[2]
            if dbpass == password:
                self.MakeModal(False)
                e.Skip()
                self.Close()
                wx.MessageBox('You are now logged in', 'Info', wx.OK | wx.ICON_INFORMATION)
            else:
                wx.MessageBox('Username or Password is incorrect', 'Info', wx.OK | wx.ICON_INFORMATION)
        except:
            wx.MessageBox('Username or Password is incorrect', 'Info', wx.OK | wx.ICON_INFORMATION)

    def OnExit(self,e):
        self.MakeModal(False)
        e.Skip()
        self.Close()
        sys.exit(0)

if __name__ == '__main__':
    app = wx.App()
    MainFrame()
    app.MainLoop()

推荐答案

我认为最简单、最干净的方法是使用 pubsub.pubsub 的 API 因您使用的 wxPython 版本而异.不过,我已经为两者编写了教程.所以对于 wxPython 2.8,你应该看看这个:

I think the easiest and cleanest way to accomplish this is to use pubsub. The API for pubsub is different depending on which version of wxPython you use. I have written up tutorials for both though. So for wxPython 2.8, you'd want to look at this one:

对于 wxPython 2.9+,你想看看这个:

For wxPython 2.9+, you'd want to take a look at this one:

有一个技巧可以让 wxPython 2.8 以新的方式运行,我在第二篇文章中解释了这一点.

There is a trick to make wxPython 2.8 behave in the new manner, which I explain in the second article.

无论如何,介绍已经足够了,让我们在您的代码中实际尝试 2.9 方法:

Anyway, enough with the intro, let's actually try the 2.9 method in your code:

import wx
import sys
username = ""
from wx.lib.pubsub import pub


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, title = 'Application Title',style = wx.SYSTEM_MENU)

        pub.subscribe(self.myListener, "update_frame")

        panel = wx.Panel(self)
        menubar = wx.MenuBar()
        panel.SetBackgroundColour((200,200,200))
        vbox = wx.BoxSizer(wx.HORIZONTAL)
        midPan = wx.Panel(panel)
        midPan.SetBackgroundColour((100,0,0))
        pan2 = wx.Panel(panel)
        pan2.SetBackgroundColour((0,100,0))
        vbox.Add(pan2, 1, wx.EXPAND | wx.ALL)
        vbox.Add(midPan, 5, wx.EXPAND | wx.ALL)
        hbox = wx.BoxSizer(wx.VERTICAL)
        hbox1=wx.BoxSizer(wx.VERTICAL)
        h1=wx.Panel(midPan)
        h1.SetBackgroundColour((200,0,100))
        h2=wx.Panel(midPan)
        h2.SetBackgroundColour((0,100,200))
#######################################################################
        self.text1 = wx.StaticText(h1, label="Welcome ") #insert the global variable in this label after the user has successfully logged in
        text2 = wx.StaticText(h1, label="Label2")
        hbox.Add(h1,3,wx.EXPAND|wx.ALL,20)
        hbox.Add(h2,1,wx.EXPAND|wx.ALL,20)
        hbox1.Add(self.text1,1,wx.ALIGN_LEFT)        
        hbox1.Add(text2,1,wx.ALIGN_RIGHT)  
        panel.SetSizer(vbox)
        midPan.SetSizer(hbox)
        h1.SetSizer(hbox1)
        self.statusbar = self.CreateStatusBar()
        self.SetMenuBar(menubar)
        self.Centre()
        self.Maximize()
        self.Show(True)
        frame = LoginFrame(self)
        frame.Show(True)
        frame.MakeModal(True)

    #----------------------------------------------------------------------
    def myListener(self, msg):
        """"""
        self.text1.SetLabel("Welcome %s" % msg)

    def OnExit(self, e):
        self.Close()

class LoginFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent,style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX | wx.CLOSE_BOX | wx.MINIMIZE_BOX ), title = "System Login")
        self.panel = wx.Panel(self)     
        self.userlabel = wx.StaticText(self.panel, label="Username:")
        self.passlabel = wx.StaticText(self.panel, label="Password:")
        self.userbox = wx.TextCtrl(self.panel, size=(140, -1))
        self.passbox = wx.TextCtrl(self.panel, size=(140, -1), style=wx.TE_PASSWORD)
        self.login = wx.Button(self.panel, label="Login")
        self.exit = wx.Button(self.panel, label="Exit")
        self.Centre()
        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)        
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.userlabel, (0, 0))
        self.sizer.Add(self.userbox, (0, 1))
        self.sizer.Add(self.passlabel, (1, 0))
        self.sizer.Add(self.passbox, (1, 1))
        self.sizer.Add(self.login, (2, 0))
        self.sizer.Add(self.exit, (2, 1))
        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
        self.panel.SetSizerAndFit(self.border)  
        self.SetSizerAndFit(self.windowSizer)  
        self.login.Bind(wx.EVT_BUTTON, self.OnLogin)
        self.exit.Bind(wx.EVT_BUTTON, self.onClose)
        self.Bind(wx.EVT_CLOSE, self.onClose)

    def OnLogin(self, e):
        #db = MySQLdb.connect("localhost","root","","test" )
        #cursor = db.cursor()
        username = self.userbox.GetValue()
        password = self.passbox.GetValue()
        #self.MakeModal(False)

        self.Close()
        #try:
            #cursor.execute("SELECT * FROM useraccounts WHERE username=%s" ,username)
            #data = cursor.fetchall()
            #for row in data:
                #dbpass = row[2]
            #if dbpass == password:
                #
                #e.Skip()
                #self.Close()
                #wx.MessageBox('You are now logged in', 'Info', wx.OK | wx.ICON_INFORMATION)
            #else:
                #wx.MessageBox('Username or Password is incorrect', 'Info', wx.OK | wx.ICON_INFORMATION)
        #except:
            #wx.MessageBox('Username or Password is incorrect', 'Info', wx.OK | wx.ICON_INFORMATION)

    def onClose(self,e):
        username = self.userbox.GetValue()
        pub.sendMessage("update_frame", msg=username)
        self.Destroy()


if __name__ == '__main__':
    app = wx.App()
    MainFrame()
    app.MainLoop()

此代码在带有 wxPython 2.9 和 Python 2.7 的 Windows 7 上对我有用.请注意,我注释掉了 SQL 内容,使其运行起来更简单一些.我还稍微更改了事件绑定,因为 OnExit 没有为我触发.

This code worked for me on Windows 7 with wxPython 2.9 and Python 2.7. Note that I commented out the SQL stuff to make it a little simpler to run. I also changed the event bindings slightly as OnExit wasn't firing for me.

这篇关于wxPython中帧之间的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆