You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击按钮在Tkinter Entry框显示新密码?

问题解决:每次点击按钮生成新密码

你的判断完全正确——当前代码里,密码只在打开密码生成子窗口时生成一次,后续点击"Create Password"按钮只是重复填入这个已经生成好的旧值,没有重新生成新密码。

修改方案

把密码生成逻辑移到按钮的点击事件中,确保每次点击按钮时才重新生成新密码:

修改后的完整代码

import customtkinter
from tkinter import *
import secrets
import string

class EasyPass(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        # Form/GUI
        self.title("EasyPass")
        self.geometry(f"{300}x{100}")
        self.resizable(False,False)

        # GuiWidgets
        self.label = customtkinter.CTkLabel(master=self,
                                       text="EasyPass v1"   ,
                                       width=120,
                                       height=25,
                                       corner_radius=8)
        self.label.place(relx=0.30, rely=0.045,)

        self.pass_creator_button = customtkinter.CTkButton(master=self,
                                         width=100,
                                         height=50,
                                         border_width=0,
                                         corner_radius=8,
                                         text="Password Creator",
                                         command=self.pass_creator)
        self.pass_creator_button.place(relx=0.03, rely=0.3)

        self.pass_checker_button = customtkinter.CTkButton(master=self,
                                                           width=100,
                                                           height=50,
                                                           border_width=0,
                                                           corner_radius=8,
                                                           fg_color= "grey",
                                                           text="Password Checker",
                                                           command=self.pass_check)
        self.pass_checker_button.place(relx=0.557, rely=0.3)



    def pass_creator(self):
        # 调整fill_password逻辑,每次调用时重新生成密码
        def fill_password():
            alphabet = string.ascii_letters + string.digits + "@$!%*?&'/"
            password = ''.join(secrets.choice(alphabet) for i in range(10))
            passEntry.delete(0, END)
            passEntry.insert(0, password)

        window = customtkinter.CTkToplevel(self)
        window.geometry("300x150")
        window.title("Password Creator")

        # create label on CTkToplevel window
        label = customtkinter.CTkLabel(window, text="Create your Password")
        label.place(relx=0.28, rely=0.03)

        #create Entry Field to display created password
        passEntry = customtkinter.CTkEntry(window, placeholder_text="",
                                                justify='center', text_color="#DDE6E8", width=200,
                                                text_font=("Segoe UI", 15, "bold"))
        passEntry.pack( padx=5, pady=30)

        # Button to create the password
        pass_creator_button1 = customtkinter.CTkButton(window,
                                                     width=100,
                                                     height=50,
                                                     border_width=0,
                                                     corner_radius=8,
                                                     text="Create Password",
                                                     command=fill_password)  # 直接绑定fill_password,无需lambda传参
        pass_creator_button1.place(relx=0.30, rely=0.5)


    def pass_check(self):
        pass

if __name__ == "__main__":
    app = EasyPass()
    app.mainloop()

修改说明

  • 将密码生成的代码从pass_creator方法的外层移到了fill_password函数内部,这样每次调用fill_password(即点击按钮时)都会重新生成新密码。
  • 按钮的command直接绑定fill_password,不需要用lambda传递旧的密码变量,避免了捕获固定值的问题。

内容的提问来源于stack exchange,提问作者IIIIII

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:20:26