I have two labels and entry fields (A & B). When I enter the username/password for "A Username/A Password", I want to click the "Submit" button, then have the labels/entry fields change to "B Username/B Password" and be able to click the "Submit" button again, using Tkinter
.
Python Code
import tkinter as tk
root = tk.Tk()
a_user_var = tk.StringVar()
a_pass_var = tk.StringVar()
b_user_var = tk.StringVar()
b_pass_var = tk.StringVar()
def submit():
a_user = a_user_var.get()
a_pass = a_pass_var.get()
a_user_var.set("")
a_pass_var.set("")
b_user = b_user_var.get()
b_pass = b_pass_var.get()
b_user_var.set("")
b_pass_var.set("")
a_user_label = tk.Label(root, text="A Username")
a_user_entry = tk.Entry(root, textvariable=a_user_var)
a_pass_label = tk.Label(root, text="A Password")
a_pass_entry = tk.Entry(root, textvariable=a_pass_var, show="•")
b_user_label = tk.Label(root, text="B Username")
b_user_entry = tk.Entry(root, textvariable=b_user_var)
b_pass_label = tk.Label(root, text="B Password")
b_pass_entry = tk.Entry(root, textvariable=b_pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
a_user_label.grid(row=0, column=0)
a_user_entry.grid(row=0, column=1)
a_pass_label.grid(row=1, column=0)
a_pass_entry.grid(row=1, column=1)
b_user_label.grid(row=0, column=0)
b_user_entry.grid(row=0, column=1)
b_pass_label.grid(row=1, column=0)
b_pass_entry.grid(row=1, column=1)
sub_btn.grid(row=2, column=0)
root.mainloop()
Current Result
Desired Result (after clicking Submit button)
CodePudding user response:
There is no need to create a unique label and entry widgets for A
and B
. Instead just use one set of widgets and change the label's text upon pressing the button to B
. If you need to store the contents of the entry widget, you can grab the label text and parse it to see which character the specific set belongs to.
For example:
import tkinter as tk
root = tk.Tk()
user_var = tk.StringVar()
pass_var = tk.StringVar()
entries = {}
def submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "A":
user_label["text"] = "B" label_text[1:]
pass_label["text"] = "B" pass_label["text"][1:]
user_var.set('')
pass_var.set('')
print(entries)
user_label = tk.Label(root, text="A Username")
user_entry = tk.Entry(root, textvariable=user_var)
pass_label = tk.Label(root, text="A Password")
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
sub_btn.grid(row=2, column=0)
user_label.grid(row=0, column=0)
user_entry.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_entry.grid(row=1, column=1)
root.mainloop()