【发布时间】:2021-09-15 04:39:59
【问题描述】:
这是我个人项目的代码,由于某种原因,按下按钮时我无法执行任何操作。
编辑:所以我设法修复了每次输入后删除的文本,所以现在它将继续输出我想要的尽可能多的密码。我删除了 output.delete 中的 0.0,因此它不会在每个生成的密码后默认文本为 0。
from tkinter import *
from datetime import datetime
import pyperclip as pc
def close_window():
window.destroy()
exit()
# Main GUI program
window = Tk()
window.frame()
window.grid_rowconfigure((0, 1), weight=1)
window.grid_columnconfigure((0, 1), weight=1)
window.title("PW Gen")
window.geometry('+10+10')
window.configure(background="black")
Label(window, bg="black", height=0, width=25, font="Raleway").grid(row=1, column=0, sticky=NSEW)
# Enter the last 4 and generate the password
Label(window, text="Enter the last 4 digits of the Reader Name:", bg="black", fg="white", font="Raleway 12 bold").grid(row=1, column=0, sticky=W)
textentry = Entry(window, width=53, bg="white")
textentry.grid(row=2, column=0, sticky=W)
Button(window, text="Generate Password", width=16, font="Raleway 8 bold", command=click).grid(row=3, column=0, sticky=W)
# Output the password
Label(window, text="\nPassword", bg="black", fg="white", font="Raleway 12 bold").grid(row=4, column=0, sticky=W)
output = Text(window, width=40, height=4, wrap=WORD, background="white")
output.grid(row=5, column=0, columnspan=2, sticky=W)
# Button and Text To Quit GUI
Label(window, text="Click to Quit", bg="black", fg="white", font="Raleway 12 bold").grid(row=6, column=0, sticky=W)
Button(window, text="Quit", width=14, font="Raleway 8 bold", command=close_window).grid(row=7, column=0, sticky=W)
window.mainloop()
【问题讨论】:
-
为什么会这样?
tk.Button(TOP, text="Generate PW", command=pwgen)。你给了Tk而不是tk -
首先为什么你有
app.mainloop(),然后是root.mainloop()?没有意义,甚至可能引发错误。您还使用self.b1作为方法、按钮(在__init__中定义)和另一个按钮(在b1中定义)???为什么? -
self.b1["command"] = self.b1正在调用自己?! -
这段代码没有意义?您将函数重命名为按钮,将命令设置为按钮
-
你为什么要使用
while循环?