'Updating a Text widget live (while typing in it) using Tkinter
Problem: I have a Text widget which in my program is a password Text widget, and I would like it to type stars '*' instead of the text that's been writtin into it (updating while typing).
Code
from tkinter import *
root = Tk()
# Changing the title and the size and the bg-color of the window #
root.geometry("400x550")
root.resizable(False, False)
root.title("Registration form")
root.configure(bg="#20c74c")
# *THE LOGIN PAGE* - START #
# The username & password text-boxes and the submit button #
username = Text(root, height=2, width=20)
username.pack()
username.place(relx=0.5, rely=0.5, anchor='center')
password = Text(root, height=2, width=20)
password.pack()
password.place(relx=0.5, rely=0.4, anchor='center')
# LOGIN FUNCTION #
def login():
acc = [] # This list will have two elements: first element (index position: 0) -> username. Second element (index position: 1) will be -> password.
valid = ""
file_length = 0
length_reached = 0
# A for loop to go through the text file row by row. (Each row has the username and the password, they're seperated by a single space).
with open(r"path", "r") as added_to_file:
for count in added_to_file:
file_length += 1
with open(r"path", "r") as file:
for row in file:
length_reached += 1
acc = row.split(" ")
if length_reached == file_length:
pass
else:
acc[1] = acc[1][:-1]
usern = username.get(1.0, END + "-1c")
passw = password.get(1.0, END + "-1c")
if passw == acc[0] and usern == acc[1]:
valid = "Works!"
print(valid)
break
else:
pass
if valid == "":
print("Account doesn't exist.")
# The submit button #
submit = Button(root, activebackground='#2c67c7', activeforeground='#fff', text='Login', command=login)
submit.pack(ipadx=15, ipady=5)
submit.place(relx=0.5, rely=0.6, anchor='center')
# *THE LOGIN PAGE* - END #
# Navigation system #
register_frame = Frame(root, height=550, width=400, background='#20c74c')
# *THE REGISTERATION PAGE* - START #
# The username & password text-boxes and the submit button #
create_username = Text(register_frame, height=2, width=20)
create_username.pack()
create_username.place(relx=0.5, rely=0.4, anchor='center')
create_password = Text(register_frame, height=2, width=20)
create_password.pack()
create_password.place(relx=0.5, rely=0.5, anchor='center')
def register_button():
with open(r"path", "a") as file: # Opening the file in 'Append' mode to add the new account to the '.txt' file.
new_usern = create_username.get(1.0, END + "-1c")
new_passw = create_password.get(1.0, END + "-1c")
account = f"\n{new_usern} {new_passw}"
file.write(account)
# The submit button #
register = Button(register_frame, activebackground='#2c67c7', activeforeground='#fff', text='Confirm',
command=register_button)
register.pack(ipadx=15, ipady=5)
register.place(relx=0.5, rely=0.6, anchor='center')
# *THE REGISTERATION PAGE* - END #
def login_form():
if register_page['background'] == '#383838':
register_frame.pack_forget()
register_page['background'] = '#fff'
register_page['foreground'] = '#000'
login_page['background'] = '#383838'
login_page['foreground'] = '#fff'
elif login_page['background'] == '#383838':
pass
else:
register_page['background'] = '#fff'
register_page['foreground'] = '#000'
def reg_form():
if login_page['background'] == '#383838':
register_page['background'] = '#383838'
register_page['foreground'] = '#fff'
login_page['background'] = '#fff'
login_page['foreground'] = '#000'
register_frame.pack()
elif register_page['background'] == '#383838':
pass
else:
register_page['background'] = '#383838'
login_page['background'] = '#fff'
login_page['foreground'] = '#000'
register_frame.pack()
register_page = Button(root, text='Sign up', command=reg_form, background='#fff', foreground='#000')
register_page.pack(ipadx=0.05, ipady=0.05)
register_page.place(relx=0.01, rely=0.01)
login_page = Button(root, text='Login', background='#383838', foreground='#fff', command=login_form)
login_page.pack(ipadx=0.05, ipady=0.05)
login_page.place(relx=0.15, rely=0.01)
# Font variable #
font_tuple = ("Comic Sans MS", 16)
userpass_font = ("Roboto", 12)
# Changing the font of some widgets #
username.configure(font=userpass_font)
password.configure(font=userpass_font)
# The mainloop #
mainloop()
So if you know a solution to this problem your help would be very useful to me.
- One more thing that I need to mention, I don't know how to use Object Oriented Programing (OOP) yet, so a solution without using it would be great ^-^
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

