【问题标题】:Tkinter Button does nothing when clicked单击时 Tkinter 按钮不执行任何操作
【发布时间】: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 循环?

标签: python tkinter pyperclip


【解决方案1】:

不是最好的解决方案,但您绝对可以尝试一下。

import tkinter as tk
from datetime import datetime
import string
import pyperclip as pc
import random
root = tk.Tk()
root.title("PW Gen")
root.geometry('+10+10')
mainframe = tk.Frame(root)
text = tk.Text(root, height=5, width=25, font="Raleway")
def pwgen():
    last4=text.get('1.0','end')[-5:]
    j=[char for char in last4 if char in string.ascii_uppercase or  char in string.ascii_lowercase]
    print(j)
    random.shuffle(j)
    print(j)
    today = datetime.today()
    pw = int(today.strftime("%d")) * int(today.strftime("%m"))
    last4=''.join(j)
    pwgen = "$ynEL" + str(pw) + str()
    pc.copy(pwgen)
    print("\nThe password is $ynEL" + str(pw) + str(last4))
class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.quit1 = tk.Button(self, text="QUIT", fg="black", command=self.master.destroy)
        self.quit1.pack(side="top")
        text.pack(side="bottom")
        self.b2 = tk.Button(self)
        self.create_widgets()
        self.pack()
    def create_widgets(self):
        self.b2["text"] = "GenPW"
        self.b2["command"] = pwgen
        self.b2.pack(side="left")
    def b1(self):
        self.b2 = tk.Button(self, text="Generate PW", command=pwgen)
app = Application(root)
root.mainloop()

【讨论】:

  • 感谢您的建设性批评,感谢您的帮助。还要感谢您的解决方案,因为我是 Python 新手并且只是想学习,所以我可能试图让事情变得过于复杂。
  • @Sujay 你知道没有任何东西在使用mainframe。名为 text 的 Text 小部件是无用的。你甚至没有使用b1 方法。
【解决方案2】:

代码有很多问题。 mainframe 未显示且从未使用过。 Text 已显示,但从未使用过。 Text 包含在 Application 中,但不是 Application 的一部分。

pwgen 使用 input 并且没有 GUI 小部件。带有 GUI 应用程序的无限循环并不好。 您应该直接使用日期时间的属性。 您不应在代码中的两个位置生成密码。 您不应该将局部变量命名为函数。 在Application 中,您使用b1 作为方法,该方法被b1 属性重载,因此您不能再使用该方法。 在b1 方法中,您正在生成一个新的butten,而不是调用函数pwgen。

import tkinter as tk
from datetime import datetime
import pyperclip as pc

def pwgen():
    today = datetime.today()
    pw = today.day * today.month
    last4 = input("Last 4 of Reader Name [****] ")
    password = f"$ynEL{pw}{last4}"
    pc.copy(password)
    print("\nThe password is", password)


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.quit = tk.Button(self, text="QUIT", fg="black", command=self.master.destroy)
        self.quit.pack(side="top")
        self.b1 = tk.Button(self, text="GenPW", command=self.click_b1)
        self.b1.pack(side="left")

    def click_b1(self):
        pwgen()


def main():
    root = tk.Tk()
    root.title("PW Gen")
    root.geometry('+10+10')
    text = tk.Text(root, height=5, width=25, font="Raleway")
    text.pack(side="bottom")
    app = Application(master=root)
    app.pack()
    root.mainloop()

if __name__ == "__main__":
    main()

【讨论】:

  • input(...) 位于 pwgen 函数中,这将使 tkinter 无响应。使用在main 中定义的Text 小部件不是更好吗?让它成为一个全局变量?
  • @TheLizzard 我更新了上面帖子中的代码,让我知道你现在的想法。我把没用的东西去掉了,照你说的做,直接用属性生成密码。我只是想不通为什么在运行程序时会有很大的差距,以及如何让程序生成多个密码而不删除以前的密码。
  • @Mocha 您的第一个标签没有任何文字,但占用了 5 个字符的高度。这就是为什么你在那里有差距。也看看这个:int(today.strftime("%d")) * int(today.strftime("%m"))。它需要一天并乘以月份。因此,如果您想要一个新密码,您将不得不等待至少一天。或者更改那行代码(更简单的选项)。
【解决方案3】:

这行得通。我使用网格管理器进行更多控制并删除了未使用的代码。


import tkinter as tk
from tkinter import *
from datetime import datetime
import pyperclip as pc

root = tk.Tk()
root.title("PW Gen")
root.geometry('+10+10')
Text = tk.Text(root, height=5, width=25, font="Raleway")

def pwgen():
    while True:
        today = datetime.today()
        pw = int(today.strftime("%d")) * int(today.strftime("%m"))
        last4 = input("Last 4 of Reader Name [****] ")
        pwgen = "$ynEL" + str(pw) + str(last4)

        pc.copy(pwgen)
        print("\nThe password is $ynEL" + str(pw) + str(last4))


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.quit = tk.Button(self, text="QUIT", fg="black", command=self.master.destroy)
        self.quit.grid(row=0,column=0,sticky=tk.EW)
        Text.grid(row=1,column=0,sticky=tk.NSEW)
        self.b1 = tk.Button(self, text="Generate PW", command=pwgen)
        self.b1.grid(row=0,column=1,sticky=tk.EW)
        self.grid(row=0,column=0,sticky=tk.NSEW)

app = Application(master=root)
root.mainloop()

以前的版本创建了多个生成密码按钮。

【讨论】:

  • tkinter 窗口在进入while True 循环时将停止响应。此外,您似乎没有将Text 小部件用于任何事情。
  • @TheLizzard 我不得不删除import pyperclip as pc,因为它不是标准的python 模块。解决了无响应按钮的问题。我认为@Sujay 将继续发展pwgen,因为障碍已经清除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
相关资源
最近更新 更多