1

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

enter image description here

Desired Result (after clicking Submit button)

enter image description here

arnpry
  • 1,103
  • 1
  • 10
  • 26

1 Answers1

3

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()
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • This configuration still holds on to the user input from "A", instead of clearing it. – arnpry Jan 12 '23 at 23:11
  • 1
    @arnpry you just need to clear the stringVar's I edited answer – Alexander Jan 12 '23 at 23:17
  • Thank you @Alexander! One other question... I noticed that after "A" user/pass has been inputted, the list for "B" contains both "A" and "B" user/pass entries, instead of just "B" user/pass. Screenshot --> https://i.imgur.com/B8zsg7g.png – arnpry Jan 13 '23 at 14:01
  • @arnpry it isn't a list. it is a dictionary and it is a global variable `entries`. I did that because I assume that you plan on using the information input from the `A` user, and the `B` user after both have entered their information. If that isn't the case then you can just move the `entries` variable inside of the `submit` function and it will no longer be the case – Alexander Jan 14 '23 at 01:48