python - What is the significance of the number 4 when one feeds a callback to the show argument of a Tkinter Entry Box? -


question

why random ascii character selector function outputting fours, , significance of number 4 in context? why not recieving error message?

remember, question not how solve issue, why particular number output.

background , code

i trying creating basic email client. thought cool password box show random characters instead of obvious *. so, created function chose random ascii letter.

import random import string  def random_char():      char_select = random.randrange(52)      char_choice = string.ascii_letters[char_select]      return char_choice 

when run in interactive terminal, spits out random letter. but, when run through widget

self.password = entry (self, show = lambda: random_char()) 

i met bunch of fours.

extra credit

if have time, please visit related question, how have tkinter entry box repeat function each time character inputted?

the show parameter accepts value not callback. tkinter taking callback object , trying convert string , when type in entry box.

instead can re-configure entry after type using binding:

def key(event):     entry.configure(show = random_char())  entry = tk.entry (self) entry.pack() entry.bind("<key>", key) 

edit

bryan oakley correct in change characters same single random character type. showing different random characters type not way supposed use entry widget. can try like:

def key(event):     global real_password     global garbage     current_len = len(v.get())     if event.char , event.char in string.ascii_letters:         real_password += event.char         garbage += random_char()     garbage = garbage[:current_len]     v.set(garbage)  v = tk.stringvar() real_password = "" garbage = "" entry = tk.entry (self, textvariable = v) entry.pack() entry.bind("<keyrelease>", key) 

of course there lots of limitations, last character typed changed when key released not when pressed, have type fast :) , there not control on cursor movement keys etc. anyway fun trying.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -