【问题标题】:drawing a widget defined by class in separate window Tkinter在单独的窗口 Tkinter 中绘制由类定义的小部件
【发布时间】:2020-05-20 21:04:48
【问题描述】:

所以我正在尝试创建一个密码库,您可以在其中通过按下按钮生成随机的字母串,并且可以通过调整滑块来确定长度。要保存密码,请按“保存密码”按钮并为密码写一个标题,以便您知道它的用途。然后它将密码和标题写入 PC 上某个单独的文件。当您需要查看密码时,您只需单击“显示密码”按钮,它会打开一个单独的窗口,其中所有密码和标题都应该是,但我不知道如何将文件的每一行写为标签,因为当我将密码写入文件时,我将每个密码直接写在标题下方。我尝试使用类定义标签,但在窗口中显示小部件时遇到问题。

我知道这是一个很长的解释,可能有点令人困惑。

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()

【问题讨论】:

  • 为什么不将标题和密码放在一行中,例如文件中的逗号分隔?为什么使用Text 而不是Entry 因为你需要的是一个单行输入框?

标签: python python-3.x class tkinter passwords


【解决方案1】:

弹出窗口中必须有一个tk.Text 小部件。它必须填充来自Update.txt 的数据,然后显示在窗口中。

代码仍有需要更正的元素,但以下显示了密码在弹出窗口中的正确位置,当按下按钮时,它回答了所提出的问题。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2018-02-06
    相关资源
    最近更新 更多