使用 Tkinter 应用程序将密码生成器的输出保存到 txt 文件

发布于 2025-01-10 20:43:35 字数 2200 浏览 0 评论 0原文

我正在开发一个独立的桌面应用程序来生成保存到文本文件的随机密码列表。我能够在 PyCharm 中创建一个工作代码,该代码在那里创建一个文本文件,其中包含基于密码长度和要创建的密码数量的随机密码列表。这非常有效。

import random
import csv

password_len = int(input("What length would you like your password to be : "))
password_count = int(input("How many passwords would you like : "))
Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
while 1:
    pwfile = open('passwords.txt','w')
    for x in range(0, password_count):
            password = ""
            for x in range(0, password_len):
                password_char = random.choice(Chars)
                password = password + password_char
            pwfile.write(password + '\n')
pwfile.close()

我遇到的问题是,当我尝试使用 Tkinter 创建桌面应用程序时,我似乎无法让应用程序写入文本文件。该代码创建弹出窗口并询问密码长度和数量。但在我输入这些数字并尝试生成文件后,应用程序就冻结了。我想问题可能是我试图在同一个函数中写入和保存?代码如下:

from tkinter import *

import random
import csv

root = Tk()
root.title('Password Server Generator')
root.geometry('400x600')

def PWgen():
    Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    pw_count = int(passTextbox.get())
    pw_length = int(myTextbox.get())
    while 1:
        pwfile = open('passwords.txt', 'w')
        for x in range(0, pw_count):
            password = ""
            for x in range(0, pw_length):
                password_char = random.choice(Chars)
                password = password + password_char
            pwfile.write(password + '\n')

#Text asking for password length
password_len = Label(root, text="What length would you like your passwords to be : ")
password_len.pack()
#Box to enter password length
myTextbox = Entry(root, width=30)
myTextbox.pack()
#Text asking for how many passwords to generate
password_count = Label(root,text="How many passwords would you like to generate: ")
password_count.pack()
#Box to enter how many passwords to generate
passTextbox = Entry(root, width=30)
passTextbox.pack()
#Button to generate password list
myButton = Button(root, text="Generate Password", command=PWgen)
myButton.pack()

root.mainloop()

理想情况下,这会自动创建一个password.txt 文件,或者提供保存到桌面或自动保存到桌面的选项。任何选择都可以,我只是无法弄清楚我做错了什么。我尝试将 pwfile.close() 添加到应用程序循环的末尾,但仍然崩溃。任何建议将不胜感激。

I am working on a standalone desktop app to generate a list of random passwords that are saved to a text file. I was able to create a working code in PyCharm that creates a text file there with a list of random passwords based on length of password and how many passwords to create. This works perfectly.

import random
import csv

password_len = int(input("What length would you like your password to be : "))
password_count = int(input("How many passwords would you like : "))
Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
while 1:
    pwfile = open('passwords.txt','w')
    for x in range(0, password_count):
            password = ""
            for x in range(0, password_len):
                password_char = random.choice(Chars)
                password = password + password_char
            pwfile.write(password + '\n')
pwfile.close()

The issue I am running into is when I try to create the desktop app with Tkinter, I cannot seem to get the app to write to a text file. The code creates the pop up window and asks for the password length and how many. But after I enter those numbers and attempt to generate the file, the app just freezes. I think maybe the problem is I'm trying to write and save in the same function? Here is the code:

from tkinter import *

import random
import csv

root = Tk()
root.title('Password Server Generator')
root.geometry('400x600')

def PWgen():
    Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    pw_count = int(passTextbox.get())
    pw_length = int(myTextbox.get())
    while 1:
        pwfile = open('passwords.txt', 'w')
        for x in range(0, pw_count):
            password = ""
            for x in range(0, pw_length):
                password_char = random.choice(Chars)
                password = password + password_char
            pwfile.write(password + '\n')

#Text asking for password length
password_len = Label(root, text="What length would you like your passwords to be : ")
password_len.pack()
#Box to enter password length
myTextbox = Entry(root, width=30)
myTextbox.pack()
#Text asking for how many passwords to generate
password_count = Label(root,text="How many passwords would you like to generate: ")
password_count.pack()
#Box to enter how many passwords to generate
passTextbox = Entry(root, width=30)
passTextbox.pack()
#Button to generate password list
myButton = Button(root, text="Generate Password", command=PWgen)
myButton.pack()

root.mainloop()

Ideally this would automatically create a password.txt file or give an option to save to desktop or automatically save to desktop. Any option is fine I just can't figure out what I'm doing incorrectly. I tried adding a pwfile.close() to the end of the app loop but that still just crashes. Any advice would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文