codekingpro/portable-devtools
115k
1"""login -- PythonWin user ID and password dialog box2 3(Adapted from originally distributed with Mark Hammond's PythonWin - 4this now replaces it!)5 6login.GetLogin() displays a modal "OK/Cancel" dialog box with input7fields for a user ID and password. The password field input is masked8with *'s. GetLogin takes two optional parameters, a window title, and a9default user ID. If these parameters are omitted, the title defaults to10"Login", and the user ID is left blank. GetLogin returns a (userid, password)11tuple. GetLogin can be called from scripts running on the console - i.e. you12don't need to write a full-blown GUI app to use it.13 14login.GetPassword() is similar, except there is no username field.15 16Example:17import pywin.dialogs.login18title = "FTP Login"19def_user = "fred"20userid, password = pywin.dialogs.login.GetLogin(title, def_user)21 22Jim Eggleston, 28 August 199623Merged with dlgpass and moved to pywin.dialogs by Mark Hammond Jan 1998.24"""25 26import win32api27import win32con28import win32ui29from pywin.mfc import dialog30 31 32def MakeLoginDlgTemplate(title):33 style = (34 win32con.DS_MODALFRAME35 | win32con.WS_POPUP36 | win32con.WS_VISIBLE37 | win32con.WS_CAPTION38 | win32con.WS_SYSMENU39 | win32con.DS_SETFONT40 )41 cs = win32con.WS_CHILD | win32con.WS_VISIBLE42 43 # Window frame and title44 dlg = [45 [title, (0, 0, 184, 40), style, None, (8, "MS Sans Serif")],46 ]47 48 # ID label and text box49 dlg.append([130, "User ID:", -1, (7, 9, 69, 9), cs | win32con.SS_LEFT])50 s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER51 dlg.append(["EDIT", None, win32ui.IDC_EDIT1, (50, 7, 60, 12), s])52 53 # Password label and text box54 dlg.append([130, "Password:", -1, (7, 22, 69, 9), cs | win32con.SS_LEFT])55 s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER56 dlg.append(57 ["EDIT", None, win32ui.IDC_EDIT2, (50, 20, 60, 12), s | win32con.ES_PASSWORD]58 )59 60 # OK/Cancel Buttons61 s = cs | win32con.WS_TABSTOP62 dlg.append(63 [128, "OK", win32con.IDOK, (124, 5, 50, 14), s | win32con.BS_DEFPUSHBUTTON]64 )65 s = win32con.BS_PUSHBUTTON | s66 dlg.append([128, "Cancel", win32con.IDCANCEL, (124, 20, 50, 14), s])67 return dlg68 69 70def MakePasswordDlgTemplate(title):71 style = (72 win32con.DS_MODALFRAME73 | win32con.WS_POPUP74 | win32con.WS_VISIBLE75 | win32con.WS_CAPTION76 | win32con.WS_SYSMENU77 | win32con.DS_SETFONT78 )79 cs = win32con.WS_CHILD | win32con.WS_VISIBLE80 # Window frame and title81 dlg = [82 [title, (0, 0, 177, 45), style, None, (8, "MS Sans Serif")],83 ]84 85 # Password label and text box86 dlg.append([130, "Password:", -1, (7, 7, 69, 9), cs | win32con.SS_LEFT])87 s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER88 dlg.append(89 ["EDIT", None, win32ui.IDC_EDIT1, (50, 7, 60, 12), s | win32con.ES_PASSWORD]90 )91 92 # OK/Cancel Buttons93 s = cs | win32con.WS_TABSTOP | win32con.BS_PUSHBUTTON94 dlg.append(95 [128, "OK", win32con.IDOK, (124, 5, 50, 14), s | win32con.BS_DEFPUSHBUTTON]96 )97 dlg.append([128, "Cancel", win32con.IDCANCEL, (124, 22, 50, 14), s])98 return dlg99 100 101class LoginDlg(dialog.Dialog):102 Cancel = 0103 104 def __init__(self, title):105 dialog.Dialog.__init__(self, MakeLoginDlgTemplate(title))106 self.AddDDX(win32ui.IDC_EDIT1, "userid")107 self.AddDDX(win32ui.IDC_EDIT2, "password")108 109 110def GetLogin(title="Login", userid="", password=""):111 d = LoginDlg(title)112 d["userid"] = userid113 d["password"] = password114 if d.DoModal() != win32con.IDOK:115 return (None, None)116 else:117 return (d["userid"], d["password"])118 119 120class PasswordDlg(dialog.Dialog):121 def __init__(self, title):122 dialog.Dialog.__init__(self, MakePasswordDlgTemplate(title))123 self.AddDDX(win32ui.IDC_EDIT1, "password")124 125 126def GetPassword(title="Password", password=""):127 d = PasswordDlg(title)128 d["password"] = password129 if d.DoModal() != win32con.IDOK:130 return None131 return d["password"]132 133 134if __name__ == "__main__":135 import sys136 137 title = "Login"138 def_user = ""139 if len(sys.argv) > 1:140 title = sys.argv[1]141 if len(sys.argv) > 2:142 def_userid = sys.argv[2]143 userid, password = GetLogin(title, def_user)144 if userid == password == None:145 print("User pressed Cancel")146 else:147 print("User ID: ", userid)148 print("Password:", password)149 newpassword = GetPassword("Reenter just for fun", password)150 if newpassword is None:151 print("User cancelled")152 else:153 what = ""154 if newpassword != password:155 what = "not "156 print("The passwords did %smatch" % (what))157 