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
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()