Wxpython在主框架启动时显示对话框

时间:2014-10-28 06:20:50

标签: wxpython

我是一个使用python和wxpython的新手。我有一个问题是在主框架启动后显示登录表单对话框,如this picture

因此,如果用户未登录,则主框架无法访问。如果用户单击关闭/ tutup按钮,它也将关闭主框架。请给我一个示例代码:)

2 个答案:

答案 0 :(得分:1)

import wx
from wx.lib import sized_controls


class MainFrame(sized_controls.SizedFrame):

    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)
        self.SetTitle('MainFrame')
        pane = self.GetContentsPane()
        wx.Button(pane, label='No access until logged in')
        self.SetInitialSize((400, 400))


class LoginFrame(sized_controls.SizedDialog):

    def __init__(self, *args, **kwargs):
        super(LoginFrame, self).__init__(*args, **kwargs)
        self.parent = args[0]
        self.logged_in = False

        pane = self.GetContentsPane()

        pane_form = sized_controls.SizedPanel(pane)
        pane_form.SetSizerType('form')

        label = wx.StaticText(pane_form, label='User Name')
        label.SetSizerProps(halign='right', valign='center')

        self.user_name_ctrl = wx.TextCtrl(pane_form, size=((200, -1)))

        label = wx.StaticText(pane_form, label='Password')
        label.SetSizerProps(halign='right', valign='center')

        self.password_ctrl = wx.TextCtrl(
            pane_form, size=((200, -1)), style=wx.TE_PASSWORD)

        pane_btns = sized_controls.SizedPanel(pane)
        pane_btns.SetSizerType('horizontal')
        pane_btns.SetSizerProps(halign='right')

        login_btn = wx.Button(pane_btns, label='Login')
        login_btn.SetDefault()
        cancel_btn = wx.Button(pane_btns, label='Cancel')
        self.Fit()
        self.SetTitle('Login')
        self.CenterOnParent()
        self.parent.Disable()

        login_btn.Bind(wx.EVT_BUTTON, self.on_btn_login)
        cancel_btn.Bind(wx.EVT_BUTTON, self.on_btn_cancel)
        self.Bind(wx.EVT_CLOSE, self.on_close)

    def on_btn_login(self, event):
        user_name = self.user_name_ctrl.GetValue()
        password = self.password_ctrl.GetValue()
        print 'logged in as {} with password {}'.format(user_name, password)
        self.logged_in = True
        self.Close()

    def on_btn_cancel(self, event):
        self.Close()

    def on_close(self, event):
        if not self.logged_in:
            self.parent.Close()
        self.parent.Enable()
        event.Skip()


if __name__ == '__main__':
    wxapp = wx.App(False)
    parent_frame = MainFrame(None)
    parent_frame.Show()
    login_frame = LoginFrame(parent_frame)
    login_frame.Show()
    wxapp.MainLoop()

答案 1 :(得分:0)

我今年早些时候关于这个话题wrote。这是一个应该帮助你的例子。请注意,它不会显示带有对话框的框架,但这样做很容易改变。相反,它只显示登录对话框,除非用户成功登录,否则框架将不会显示。

import wx

if "2.8" in wx.version():
    import wx.lib.pubsub.setupkwargs
    from wx.lib.pubsub import pub
else:
    from wx.lib.pubsub import pub


########################################################################
class LoginDialog(wx.Dialog):
    """
    Class to define login dialog
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Login")

        # user info
        user_sizer = wx.BoxSizer(wx.HORIZONTAL)

        user_lbl = wx.StaticText(self, label="Username:")
        user_sizer.Add(user_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.user = wx.TextCtrl(self)
        user_sizer.Add(self.user, 0, wx.ALL, 5)

        # pass info
        p_sizer = wx.BoxSizer(wx.HORIZONTAL)

        p_lbl = wx.StaticText(self, label="Password:")
        p_sizer.Add(p_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.password = wx.TextCtrl(self, style=wx.TE_PASSWORD)
        p_sizer.Add(self.password, 0, wx.ALL, 5)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(user_sizer, 0, wx.ALL, 5)
        main_sizer.Add(p_sizer, 0, wx.ALL, 5)

        btn = wx.Button(self, label="Login")
        btn.Bind(wx.EVT_BUTTON, self.onLogin)
        main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)

        cancel = wx.Button(self, wx.ID_CANCEL)
        main_sizer.Add(cancel, 0, wx.ALL|wx.CENTER, 5)

        self.SetSizer(main_sizer)

    #----------------------------------------------------------------------
    def onLogin(self, event):
        """
        Check credentials and login
        """
        stupid_password = "pa$$w0rd!"
        user_password = self.password.GetValue()
        if user_password == stupid_password:
            print "You are now logged in!"
            pub.sendMessage("frameListener", message="show")
            self.Destroy()
        else:
            print "Username or password is incorrect!"

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)


########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Main App")
        panel = MyPanel(self)
        pub.subscribe(self.myListener, "frameListener")

        # Ask user to login
        dlg = LoginDialog()
        res = dlg.ShowModal()
        if res == wx.ID_CANCEL:
            dlg.Destroy()
            self.Close()


    #----------------------------------------------------------------------
    def myListener(self, message, arg2=None):
        """
        Show the frame
        """
        self.Show()


if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()