I am a new using python and wxpython. I have a problem to show login form dialog after main frame startup like this picture
So if the user is not logged, the main frame
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()
I wrote about this topic earlier this year. Here is an example that should help you. Note that it does NOT show the frame with the dialog, but that would be trivial to change. Instead it just shows the login dialog and the frame won't show unless the user logs in successfully.
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()