嗨,我正在嘗試創建一個密碼管理器,目的是幫助自己學習 python、tkinter 和 sqlite3,但是我遇到了一個問題,每次我創建一個新密碼時,我都希望出現一個顯示該密碼名的按鈕,然后用戶可以單擊在它上面訪問他們的實際密碼。我試圖通過一個for回圈來做到這一點,但是當它遍歷我資料庫中的每個密碼時,它不會生成我的按鈕。任何幫助將不勝感激。提前謝謝你這是我的代碼(for 回圈在第 89 行和第 92 行之間)。
import sqlite3
from tkinter import *
from tkinter import messagebox
#creating database
conn=sqlite3.connect("Passwords.db")
cursor=conn.cursor()
#creating table-num of quotation is number of lines,name of field then datatype e.g passwordNameoutofdatabase text
cursor.execute("""create table IF NOT EXISTS Passwords
(passwordName text PRIMARY KEY
,UserName text
,password text
)""")#inside are columns/categorys
#login window
login=Tk()
login.title("Password Manager")
login.geometry("1366x768")
screen_width=login.winfo_screenwidth()#gets screen width
screen_height=login.winfo_screenheight()#gets screen height
loginBox=Entry(login,width=100)
loginBox.pack()
def loginClick():
if loginBox.get()=="steve":
#main window
# creating main password screen window
main = Toplevel()
main.title("Password Manager")
main.geometry("1366x768")
screen_width = main.winfo_screenwidth() # gets screen width
screen_height = main.winfo_screenheight() # gets screen height
# setting background
canvas = Canvas(main, width=screen_width, height=screen_height)
canvas.config(bg="white")
canvas.pack()
# creating lines
canvas.create_line(200, 0, 200, screen_height, fill="blue", width=25)
canvas.create_line(0, 100, screen_width, 100, fill="blue",
width=25) # x,y,x1,y1 warning grid system is weird lower y is higher
# creating labels
passwordLabel = Label(main, bg="white", text="Passwords",
font=("freesans", 20)) # set window then text then font then size
passwordLabel.place(x=50, y=50)
# commands for buttons
def newPasswordclick():
# creating Labels for search bars and title
newPasswordlabel = Label(main, text="Create Password Screen", bg="white", font=("freesans", 20))
newPasswordlabel.place(x=215, y=50)
def nameEntry():
passwordNameoutofdatabase = NameofPassword.get()
username = Name.get()
password = Password.get() # gets input from the input box
cursor.execute("""SELECT passwordName
FROM Passwords
WHERE passwordName=?
""",
(passwordNameoutofdatabase,))#selects passwordName1 from table then Selects Where column = passwordNameoutofdatabase/any variable as variables by question marks in sqlite3
primaryKeyCheck=cursor.fetchone()
print(primaryKeyCheck)
#Insert passwordNameoutofdatabase,Username,password
if primaryKeyCheck!=None:
NameofPassword.delete(0, END) # deletes text within the range of 0 to end
Name.delete(0, END)
Password.delete(0, END)
messagebox.showerror(title="ERROR",message="Already have a password with this name")
else:
NameofPassword.delete(0, END) # deletes text within the range of 0 to end
Name.delete(0, END)
Password.delete(0, END)
cursor.execute("INSERT INTO Passwords (passwordName,Username,password )VALUES(?,?,?)",(passwordNameoutofdatabase,username,password,))#Inserts variables into table by referencing which variables correspond to each column
conn.commit()
cursor.execute("SELECT*FROM Passwords")#Selects Passwords table
Data=cursor.fetchall()#fetches everything selected
cursor.execute("SELECT passwordName FROM Passwords")#selects password name from table
passwordNames=cursor.execute("Select passwordName FROM Passwords")
Passwords=cursor.execute("SELECT* FROM Passwords")
for passwordNames in Passwords:
Button(main,text=(str(passwordNames)),font=("freesans",20))
print("steve")
cursor.close()#closes database
print(Data)#prints everything fetched
if passwordNameoutofdatabase != "" and username != "" and password != "": # checks that the user has typed in all the input boxes
NameofPassword.destroy() # destroy destroys labels and other widgets
Name.destroy()
Password.destroy()
UserNameLabel.destroy()
nameEnter.destroy()
newPasswordlabel.destroy()
NameofPasswordlabel.destroy()
PasswordLabel.destroy()
# labels for buttons
NameofPasswordlabel = Label(main, text="Enter the name of your password", bg="white", font=("freesans", 10))
NameofPasswordlabel.place(x=215, y=125)
UserNameLabel = Label(main, text="Enter your username or email address", bg="white", font=("freesans", 10))
UserNameLabel.place(x=215, y=175)
PasswordLabel = Label(main, text="Enter your password", bg="white", font=("freesans", 10))
PasswordLabel.place(x=215, y=225)
# creating search bars
NameofPassword = Entry(main, width=100, highlightthickness=2, highlightbackground="black",
highlightcolor="black")
NameofPassword.place(x=215, y=150)
Name = Entry(main, width=100, highlightthickness=2, highlightbackground="black",
highlightcolor="black") # highlight attributes create border
Name.place(x=215, y=200)
Password = Entry(main, width=100, highlightthickness=2, highlightbackground="black", highlightcolor="black")
Password.place(x=215, y=250)
# buttons
nameEnter = Button(main, text="Enter", command=nameEntry)
nameEnter.place(x=850, y=250)
# creating buttons
NewPasswordImg = PhotoImage(file="C:\VsCode\Password Manager\Create Password Button.png")
newPassword = Button(main, image=NewPasswordImg, compound="center", text="Create new password",command=newPasswordclick) # to combine text with image need to set compound to center,left or right which determines where text is put
newPassword.config(height=50, width=150)
newPassword.place(x=20, y=125)
main.mainloop()
loginButton=Button(login,text="Login",font=("freesans",20),command=loginClick,width=5,height=10)
loginButton.pack()
login.mainloop()
uj5u.com熱心網友回復:
當您嘗試提高您的 Python 技能時,我建議您使用類,這對于在 tkinter 上操作諸如視窗之類的 UI 非常有用。
至于您沒有生成按鈕的問題,您可以嘗試將它們放入串列中。
我試圖用密碼和用戶名做一些事情,出現在一個帶有一些按鈕的視窗上。方法不同,但它可能會為您的專案提供一些想法。
#!/usr/bin/python3
import tkinter as tk
CONNECT_APP_DIMENSIONS=("400", "200")
PASSWORD_APP_DIMENSIONS=("800", "500")
USERNAME_PASSWORD_IN_DB=[
['banane_user', 'banane_password'],
['kiwi_user', 'kiwi_password'],
['patate_user', 'patate_password'],
]
class PasswordApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_geometry(PASSWORD_APP_DIMENSIONS[0] "x" PASSWORD_APP_DIMENSIONS[1])
# ---- ADDING GESTION ----
# username entry
self.username_entry=tk.Entry(self, width=20)
self.username_entry.focus_set()
self.username_entry.insert(tk.END, "new_username")
self.username_entry.grid(row=0,column=0)
# password entry
self.password_entry=tk.Entry(self, width=20)
self.password_entry.focus_set()
self.password_entry.insert(tk.END, "new_password")
self.password_entry.grid(row=0,column=1)
# add username/password button
self.add_button = tk.Button(self, text="add", command=self.add_account)
self.add_button.grid(row=0,column=2)
# status label
self.status_text=tk.StringVar(self)
self.status = tk.Label(self, textvariable=self.status_text)
self.status.grid(row=0,column=3)
# ---- END ADDING GESTION ----
# intiliaze some list
self.display_buttons=[]
self.password_status_texts=[]
self.password_labels=[]
# make a copy of DB
self.database = USERNAME_PASSWORD_IN_DB.copy()
# display username/password list
self.refresh_list()
def refresh_list(self):
cpt_row = 1
simple_cpt = 0
for username_password in self.database:
# new password was added
if simple_cpt < len(self.display_buttons):
cpt_row = 1
simple_cpt = 1
continue
# display password button
self.display_buttons.append(
tk.Button(
self,
text=username_password[0],
command= lambda simple_cpt=simple_cpt: self.display_password(db_id=simple_cpt)
)
)
self.display_buttons[simple_cpt].grid(row=cpt_row,column=0)
# status label
self.password_status_texts.append(tk.StringVar(self, '******'))
self.password_labels.append(tk.Label(self, textvariable=self.password_status_texts[simple_cpt]))
self.password_labels[simple_cpt].grid(row=cpt_row,column=1)
cpt_row = 1
simple_cpt = 1
def display_password(self, db_id):
# updating label associated to username
self.password_status_texts[db_id].set(self.database[db_id][1])
def add_account(self):
# getting user inputs
username=self.username_entry.get()
password=self.password_entry.get()
# check minimum size
if len(username) < 4 or len(password) < 4:
self.status_text.set("Incorrect username/password")
return
# check username not already in self.database
for username_password in self.database:
if username_password[0] == username:
self.status_text.set("Username already in DB")
return
# adding username/password to DB
self.database.append([username, password])
# refreshing list
self.refresh_list()
class ConnectionApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_geometry(CONNECT_APP_DIMENSIONS[0] "x" CONNECT_APP_DIMENSIONS[1])
# username entry
self.entry=tk.Entry(self, width=20)
self.entry.focus_set()
self.entry.insert(tk.END, "username")
self.entry.grid(row=0,column=0)
# connect button
self.connect_button = tk.Button(text="connect", command=self.connect)
self.connect_button.grid(row=0,column=1)
# status label
self.status_text=tk.StringVar()
self.status = tk.Label(textvariable=self.status_text)
self.status.grid(row=0,column=2)
def connect(self):
if self.entry.get() == "steve":
self.status_text.set("Connected")
self.password_app=PasswordApp()
self.destroy()
else:
self.status_text.set("Unknown user")
if __name__ == '__main__':
connect_app=ConnectionApp()
connect_app.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491999.html
