使用 Tkinter 应用程序将密码生成器的输出保存到 txt 文件
我正在开发一个独立的桌面应用程序来生成保存到文本文件的随机密码列表。我能够在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论