@@ -249,6 +249,7 @@ def __init__(self, name, level=_NOTSET, width: int = 80, height: int = 20, defau
249249
250250 # Input related lines
251251 self .getting_input = False
252+ self .getting_pass = False
252253 self .input_text = ''
253254 # cursor counter is used for making a nice blinking cursor
254255 self .__cursor_counter = 1
@@ -317,14 +318,41 @@ def input(self, *msg, args: tuple = (), end='', **kwargs) -> str:
317318 self .getting_input = False
318319 return self .input_text
319320
321+ def getpass (self , * msg , args : tuple = (), end = '' , ** kwargs ) -> str :
322+ """
323+ Prints a message and waits for input.
324+
325+ :param msg: The message to print.
326+ :param args: The arguments to pass to the message.
327+ :param end: The end of the message.
328+ :param kwargs:
329+ :return: The input.
330+ """
331+ msg = ' ' .join ([str (m ) for m in msg ]) + end
332+ self ._log (self .level if self .level >= _NOTSET else _NOTSET , msg , args , ** kwargs )
333+ self .input_text = ''
334+ self .getting_pass = True
335+ self .cursor_position = 0
336+ self .logs .focus ()
337+ try :
338+ while self .getting_pass :
339+ self .cursor_position = self .cursor_position
340+ self .window .update ()
341+ self .window .after (10 )
342+ except KeyboardInterrupt :
343+ self .input_text = ''
344+ self .getting_pass = False
345+ return self .input_text
346+
320347 def key_press (self , event ):
321348 """
322349 KeyPress event callback for self.logs.
323350 """
324- if self .getting_input :
351+ if self .getting_input or self . getting_pass :
325352 # Handles Enter key
326353 if event .keysym == 'Return' :
327354 self .getting_input = False
355+ self .getting_pass = False
328356 self .cursor_position = 0
329357 self .logs .config (state = _tkinter .NORMAL )
330358 self .logs .insert (_tkinter .END , '\n ' )
@@ -337,24 +365,30 @@ def key_press(self, event):
337365 self .logs .delete (f'end-{ len (self .input_text ) + 1 } c' , 'end-1c' )
338366 self .input_text = self .input_text [:self .cursor_position - 1 ] + \
339367 self .input_text [self .cursor_position :]
340- self .logs .insert (_tkinter .END , self .input_text )
368+ if self .getting_pass :
369+ self .logs .insert (_tkinter .END , '*' * len (self .input_text ))
370+ else :
371+ self .logs .insert (_tkinter .END , self .input_text )
341372 self .logs .config (state = _tkinter .DISABLED )
342373 self .cursor_position -= 1
343374 # Handles Right Arrow
344375 elif event .keysym == 'Right' :
345- if self .cursor_position < len (self .input_text ):
376+ if self .cursor_position < len (self .input_text ) and not self . getting_pass :
346377 self .cursor_position += 1
347378 # Handles Left Arrow
348379 elif event .keysym == 'Left' :
349- if self .cursor_position > 0 :
380+ if self .cursor_position > 0 and not self . getting_pass :
350381 self .cursor_position -= 1
351382 # Handles other keys
352383 elif event .char :
353384 self .logs .config (state = _tkinter .NORMAL )
354385 self .logs .delete (f'end-{ len (self .input_text ) + 1 } c' , 'end-1c' )
355386 self .input_text = self .input_text [:self .cursor_position ] + event .char + \
356387 self .input_text [self .cursor_position :]
357- self .logs .insert (_tkinter .END , self .input_text )
388+ if self .getting_pass :
389+ self .logs .insert (_tkinter .END , '*' * len (self .input_text ))
390+ else :
391+ self .logs .insert (_tkinter .END , self .input_text )
358392 self .logs .config (state = _tkinter .DISABLED )
359393 self .cursor_position += 1
360394
0 commit comments