在单独的 window Tkinter 中绘制由 class 定义的小部件
drawing a widget defined by class in separate window Tkinter
所以我正在尝试制作一个密码保险库,您可以在其中通过按一个按钮来生成随机的字母串,并且可以通过调整滑块来确定长度。要保存密码,请按 "Save password" 按钮并为密码写一个标题,这样您就知道它的用途了。然后它将密码和标题写入 PC 上某处的单独文件。当您需要查看密码时,您只需单击 "Show passwords" 按钮,它会打开一个单独的 window,其中应该包含所有密码和标题,但我不知道如何每隔一行写将文件作为标签,因为当我将密码写入文件时,我将每个密码直接写在标题下方。我曾尝试使用 class 定义标签,但在 window.
中显示小部件时遇到问题
我知道那是一个很长的解释,可能有点令人困惑。
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()
弹出窗口中必须有一个 tk.Text
小部件。它必须用 Update.txt
中的数据填充,然后显示在 window.
中
代码仍有需要更正的元素,但下面显示了密码在弹出窗口中的正确位置,当按下按钮时,它回答了所问的问题。
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()
所以我正在尝试制作一个密码保险库,您可以在其中通过按一个按钮来生成随机的字母串,并且可以通过调整滑块来确定长度。要保存密码,请按 "Save password" 按钮并为密码写一个标题,这样您就知道它的用途了。然后它将密码和标题写入 PC 上某处的单独文件。当您需要查看密码时,您只需单击 "Show passwords" 按钮,它会打开一个单独的 window,其中应该包含所有密码和标题,但我不知道如何每隔一行写将文件作为标签,因为当我将密码写入文件时,我将每个密码直接写在标题下方。我曾尝试使用 class 定义标签,但在 window.
中显示小部件时遇到问题我知道那是一个很长的解释,可能有点令人困惑。
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()
弹出窗口中必须有一个 tk.Text
小部件。它必须用 Update.txt
中的数据填充,然后显示在 window.
代码仍有需要更正的元素,但下面显示了密码在弹出窗口中的正确位置,当按下按钮时,它回答了所问的问题。
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()