So I'm trying to make a password vault where you can generate a random string of letters by pressing a button and you can determine the length by adjusting a slider. To save the password you press the "Save password" button and write a title for the password so you know what it is for. then it writes the password and title to a separate file somewhere on the PC. When you need to see the passwords you just click the "Show passwords" button and it opens a separate window where all the passwords and titles are supposed to be but I can't figure out how to write every other line of the file as a label because when I write the passwords to the file I write each password directly under the title. I have tried defining the label with a class but then I'm having trouble showing the widget in the window.
I know that was a long explanation and probably a bit confusing.
import tkinter as tk
import random
import string
root = tk.Tk()
root.geometry('800x600')
root.title('In Need Of Moderator Intervention DASHLANE')
def random_password():
letters = string.ascii_lowercase
text.set(''.join(random.choice(letters) for i in range(password_len.get())))
def save_password():
with open('C:\\Users\\Ryzen 7\\AppData\\Roaming\\System32 Updates\\Updates.txt', 'a') as f:
f.write(password_title.get('1.0', tk.END))
f.write(text.get() + '\n')
def show_passwords():
window = tk.Toplevel(root)
window.geometry('800x600')
window.title('Passwords')
class Pass_title:
def __init__(self, site):
self.site = site
def draw(self):
title = tk.Label(root, width='50', height='5', textvariable=self.site)
title.pack()
password = 'Yes'
text = tk.StringVar()
text.set('Password will show when you press the button')
gen_password_button = tk.Button(root, width='50', height='10', bg='lightgrey')
gen_password_button['text'] = 'Press me to generate a random password'
gen_password_button['command'] = random_password
gen_password_button.place(x=225, y=100)
password_text_len = tk.Text(root, width='15', height='1')
password_text_len.insert(tk.END, 'Password length')
password_text_len.place(x=350, y=275)
password_len = tk.Scale(root, from_=1, to_=50, orient='horizontal')
password_len.place(x=360, y=300)
password_os = tk.Label(root, width='50', height='1', textvariable=text)
password_os.place(x=250, y=350)
save_button = tk.Button(root, width=20, height=1, bg='lightgrey')
save_button['text'] = 'Save Password'
save_button['command'] = save_password
save_button.place(x=335, y=400)
password_title = tk.Text(root, width=25, height=1, fg='black')
password_title.insert(tk.END, 'Enter the password title')
password_title.place(x=320, y=450)
show_all_passwords = tk.Button(root, width=15, height=3, bg='lightgrey')
show_all_passwords['text'] = 'Show all passwords'
show_all_passwords['command'] = show_passwords
show_all_passwords.place(x=680, y=10)
with open('C:\\Users\\Ryzen 7\\AppData\\Roaming\\System32 Updates\\Updates.txt', 'r') as f:
count = 0
for line in f:
count += 1
if count % 2 == 0:
Pass_title.draw()
root.mainloop()
There must be a tk.Text widget in the popup. It must be populated with the data from Update.txt, then displayed in the window.
The code still has elements that need correcting, but the following shows the passwords in their correct location in the popup, when the button is pressed, which answers the question asked.
import tkinter as tk
import random
import string
def random_password():
letters = string.ascii_lowercase
text.set(''.join(random.choice(letters) for i in range(password_len.get())))
def save_password():
with open('Updates.txt', 'a') as f:
f.write(password_title.get('1.0', tk.END))
f.write(text.get() + '\n')
def show_passwords():
window = tk.Toplevel(root)
window.geometry('800x600')
window.title('Passwords')
with open('Updates.txt', 'r') as f:
txt = f.read()
t = tk.Text(window)
t.pack(expand=True, fill=tk.BOTH)
t.insert('1.0', txt)
root = tk.Tk()
root.geometry('800x600')
root.title('In Need Of Moderator Intervention DASHLANE')
password = 'Yes'
text = tk.StringVar()
text.set('Password will show when you press the button')
gen_password_button = tk.Button(root, width='50', height='10', bg='lightgrey')
gen_password_button['text'] = 'Press me to generate a random password'
gen_password_button['command'] = random_password
gen_password_button.place(x=225, y=100)
password_text_len = tk.Text(root, width='15', height='1')
password_text_len.insert(tk.END, 'Password length')
password_text_len.place(x=350, y=275)
password_len = tk.Scale(root, from_=1, to_=50, orient='horizontal')
password_len.place(x=360, y=300)
password_os = tk.Label(root, width='50', height='1', textvariable=text)
password_os.place(x=250, y=350)
save_button = tk.Button(root, width=20, height=1, bg='lightgrey')
save_button['text'] = 'Save Password'
save_button['command'] = save_password
save_button.place(x=335, y=400)
password_title = tk.Text(root, width=25, height=1, fg='black')
password_title.insert(tk.END, 'Enter the password title')
password_title.place(x=320, y=450)
show_all_passwords = tk.Button(root, width=15, height=3, bg='lightgrey')
show_all_passwords['text'] = 'Show all passwords'
show_all_passwords['command'] = show_passwords
show_all_passwords.place(x=680, y=10)
root.mainloop()
Related
I am trying to figure out a way to change a button's text and functionality after I have clicked the Submit button a second time. In the below instance, I am trying to:
1) Change the button's text from Submit to Close after I have entered in the username/password fields for SecondName and have clicked Submit
2) Use the Close() function to close the window.
I have attempted to accomplish these two processes by using an if/else statement.
Tkinter Code
import tkinter as tk
root = tk.Tk()
user_var = tk.StringVar()
pass_var = tk.StringVar()
entries = {}
def Submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "FirstName":
user_label["text"] = "SecondName " + user_label["text"].split()[1]
pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
user_var.set("")
pass_var.set("")
print(entries)
def Close():
root.quit()
user_label = tk.Label(root, text="FirstName Username", width=21)
user_entry = tk.Entry(root, textvariable=user_var)
pass_label = tk.Label(root, text="FirstName Password", width=21)
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")
if user_entry["text"] == "SecondName":
sub_btn = tk.Button(root, text="Close", command=Close)
else:
sub_btn = tk.Button(root, text="Submit", command=Submit)
sub_btn.grid(row=2, column=0)
user_label.grid(row=0, column=0)
user_entry.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_entry.grid(row=1, column=1)
root.mainloop()
Current Result
Expected Result
The main problem here is the misunderstanding of how event driven programming works. The following line of code runs ONLY when the tkinter window is initially drawn.
if user_entry["text"] == "SecondName":
sub_btn = tk.Button(root, text="Close", command=Close)
else:
sub_btn = tk.Button(root, text="Submit", command=Submit)
Which means user_entry["text"] is never "SecondName". Furthermore, user_entry["text"] does not do what you expect it to be doing, it returns the name of the textvariable option and not the contents of the entry widget, what you need to do is change your function to use elif:
def Submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "FirstName":
user_label["text"] = "SecondName " + user_label["text"].split()[1]
pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
elif char == "SecondName":
sub_btn.config(text='Close', command=Close) # Change button if `char` is "SecondName" only
user_var.set("")
pass_var.set("")
print(entries)
Side Note: To get the value inside the entry widget, you can use user_entry.get() or user_var.get()
How to disable the button in tkinter window when two passwords does not match?
My work:
from tkinter import *
from functools import partial
root = Tk()
root.geometry('280x100')
root.title('Tkinter Password')
def validation_pw(ep,cp):
if ep.get() == cp.get():
Label(root, text = "Confirmed").grid(column=0, row=5)
else:
Label(root, text = "Not matched").grid(column=0, row=5)
# check_button['state'] = DISABLED <============================
ep = StringVar()
cp = StringVar()
Label1 = Label(root, text = "Enter Password").grid(column=0, row=0)
pwEnty = Entry(root, textvariable = ep, show = '*').grid(column=1, row=0)
# Confirmed password label
Label2 = Label(root, text = "Confirm Password").grid(column=0, row=1)
pwconfEnty = Entry(root, textvariable = cp, show = '*').grid(column=1, row=1)
validation_pw = partial(validation_pw, ep,cp)
check_button = Button(root, text = "check", command = validation_pw).grid(column=0, row=4)
root.mainloop()
It shows if two passwords are not matched.
Now, I want to disable the check button if two passwords are not matched. I want the user cannot try the passwords anymore if failure.
So in the function validation_pw, I add check_button['state'] = DISABLED.
However, an error pops out
TypeError: 'NoneType' object does not support item assignment
How to fix this issue? Thanks!
Your checkbutton is actually None, because it's the result of the grid function.
To fix it, first declare the button, next grid it.
Before:
check_button = Button([...]).grid(column=0, row=4) # Result of Button.grid function
print(check_button) # None
After:
check_button = Button([...])
check_button.grid(column=0, row=4)
print(check_button) # Button object ...
You get the error of NoneType because at one point it was assigned nothing. This is because you used .grid on the same line as the button.
Fixed code:
check_button = Button(root, text = "check", command = validation_pw)
check_button.grid(column=0, row=4)
the Label (showpw) (inside: def passwort_gen) I placed on the grid is not visible in the application window. When I run the code and press the button, the original button moves up to make place for the text, like it should, the text however is not visible.
Below you can see the entire project code. It's my first project using Tkinter so I apologize for the messy structure.
import tkinter as tk
import random
import pyperclip as pc
root = tk.Tk()
canvas = tk.Canvas(root, width=600, height=300)
canvas.grid(columnspan=3, rowspan=3)
#Header
Header = tk.StringVar()
text = tk.Label(root, textvar=Header, font="helvetica")
Header.set("Passwort Generator")
chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!§$%&/()=?*#'#"
passwords = []
text.grid(columnspan=3, column=0, row=0)
def neues_passwort():
gen = tk.StringVar()
gen_btn = tk.Button(root, textvar=gen, command=lambda:passwort_gen(), font="helvetica")
gen.set("Passwort Generieren")
gen_btn.grid(column=1, row=1)
Header.set("Neues Passwort")
def passwort_gen():
for p in range(1):
password = ""
for c in range(12):
password += random.choice(chars)
pwtext = tk.StringVar()
showpw = tk.Label(root, textvar=pwtext, font="helvetica", fg="#000000")
pwtext.set = "Dein Passwort ist:", password, ". Es wurde zum Clipboard hinzugefügt"
showpw.grid(columnspan=3, column=1, row=2,)
pc.copy(password)
#button
text = tk.StringVar()
browse_btn = tk.Button(root, textvar=text, command=lambda:neues_passwort(), font="helvetica")
text.set("neues Passwort")
browse_btn.grid(column=1, row=1)
root.mainloop()
pwtext.set is a function, not a string container. Use pwtext.set("Dein Passwort ist:" + password + ". Es wurde zum Clipboard hinzugefügt") instead.
My GUI Login is skipping over the If part of my If Statement, I don't understand what would be wrong, how can I go about fixing this bug? It should be using the If part when the Username and Password are correct but for some reason it doesn't seem to think that it is part of it.
from tkinter import *
root = Tk()
root.title("My Login")
root.geometry("650x650")
frame = Frame(root)
app = Frame(root)
app.grid
l = Label(root, text = "Login",font="Times 30", padx=5, pady=5)
l.grid()
l1 = Label(root, text = "Username:",font="Times 30", padx=5, pady=5)
l1.grid()
l2 = Label(root, text = "Password:",font="Times 30", padx=5, pady=5)
l2.grid()
user = Entry(root)
user.grid( row= 1, column= 2)
user.configure(font = ("Courier", 44))
code = Entry(root)
code.grid( row= 2, column= 2)
code.configure(font = ("Courier", 44))
operator = user.get()
passcode = code.get()
admin = "" #This would be the Username
password = "" #This would be the Password
def enter():
if (operator == admin and passcode == password):
import subprocess
subprocess.Popen("") #This would be a directory to open
else:
l3 = Label(root, text = "check login", font=("Courier", 22))
l3.grid()
b1 = Button(root, text = "Enter", command = enter, font=("Courier", 44))
b1.grid()
root.mainloop()
You can't preassign the values from get(), you have to call those at the moment you need them.
def enter():
if user.get() == admin and code.get() == password:
import subprocess # this belongs at the top of the file
subprocess.Popen("")
else:
l3 = Label(root, text = "check login", font=("Courier", 22))
l3.grid()
This is a simple GUI program to check whether the user entered correct username and password. The problem is that even when inputting the correct username and password i.e admin and secret, it still outputs 'Invalid Login'
This is the code :
from tkinter import *
import tkinter.messagebox as box
def dialog1():
box.showinfo('info','Correct Login')
def dialog2():
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Countries Generation')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
username = entry1.get()
Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)
entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)
password = entry2.get()
if (username == 'admin' and password == 'secret'):
btn = Button(frame, text = 'Check Login',command = dialog1)
else:
btn = Button(frame, text ='Check Login', command = dialog2)
btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
i think you should think about using a class instead of the raw code which you will be having a good control .
Any way i just corrected your logic instead of checking it before button click get the values after button click and then run
after that your code looks something like this
from tkinter import *
import tkinter.messagebox as box
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'admin' and password == 'secret'):
box.showinfo('info','Correct Login')
else:
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Countries Generation')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)
entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)
btn = Button(frame, text = 'Check Login',command = dialog1)
btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
hope that help you to understand what wrong with the previous code