简体   繁体   中英

Tkinter Widget Attribute Update (using ttk frame)

I wanted to know if there was a way to update a parameter of a tkinter widget once a button is clicked. For instance,

master = Tk()
master.title("Create Bank Account")
master.geometry("400x400+100+100")
mainframe = ttk.Frame(master, padding=(3,3,12,12))
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
'''master.columnconfigure(0, weight=1) #defines where the frame expands
master.rowconfigure(0, weight=1)
ttk.Label(mainframe, text="Username: ", width=25).grid(column=0,row=0,sticky=(N,W,E,S))
ttk.Label(mainframe, text="Email: ", width=25).grid(column=0,row=1,sticky=(N,W,E,S))
ttk.Label(mainframe, text="Password: ", width=25).grid(column=0,row=2,sticky=(N,W,E,S))
UserEntTK=StringVar()
Username= ttk.Entry(mainframe, textvariable=UserEntTK, width=20)
Username.grid(column=1, row=0, sticky=(W,E))
UserEmailTK=StringVar()
Email= ttk.Entry(mainframe, textvariable=UserEmailTK, width=20)
Email.grid(column=1, row=1, sticky=(W,E))'''
PassTK= StringVar()
Password = ttk.Entry(mainframe, textvariable=PassTK, width=20, show="*")
Password.grid(column=1, row=2, sticky=(W,E))
ttk.Button(mainframe, text="Show", command=show_pass).grid(column=1, row=2, sticky=N)
Button(mainframe, text="Continue", command=master.destroy()).grid(column=1,row=4,sticky=N)
master.mainloop()

Where the command show_pass converts the show attribute of the Password widget from a value of "*" to basically showing all characters. The above code is my programmed tk window as of now, so the code in quotes isn't relevant to the question but might help better understand the purpose and context of the question.

Also, I'm a beginner to Python programming so all changes in program structure, (ie whether you want to convert everything into a class), are welcome because it helps me learn. Really appreciate all help :D

I think you just want to configure the Password widget so that show="" :

def show_pass():
    Password.configure(show="")

this will show the password when clicking the button but does not allow to change back, so you may want a Checkbutton instead:

def show_pass():
    Password.configure(show=show_option.get())

show_option= StringVar(master,"*") #could also use Password.cget("show") as initial value

Checkbutton(mainframe,command=show_pass,
            text="show password",
            variable=show_option,
            onvalue ="",
            offvalue="*").grid(column=1, row=3, sticky=N)

As a side note you are calling master.destroy() right away instead of passing the method itself to the command of the button:

# use this
Button(... command=master.destroy).grid(...)
# instead of this
Button(... command=master.destroy()).grid(...)
                               # ^this calls it right away!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM