arnpry
arnpry

Reputation: 1141

Tkinter - After Second Button Click, Change Button Function to Close Window

I am trying to figure out a way to change a button's text and functionality after I have clicked the Submit button a second time. In the below instance, I am trying to:

1) Change the button's text from Submit to Close after I have entered in the username/password fields for SecondName and have clicked Submit

2) Use the Close() function to close the window.

I have attempted to accomplish these two processes by using an if/else statement.

Tkinter Code

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 == "FirstName":
        user_label["text"] = "SecondName " + user_label["text"].split()[1]
        pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
    user_var.set("")
    pass_var.set("")
    print(entries)


def Close():
    root.quit()

user_label = tk.Label(root, text="FirstName Username", width=21)
user_entry = tk.Entry(root, textvariable=user_var)

pass_label = tk.Label(root, text="FirstName Password", width=21)
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")

if user_entry["text"] == "SecondName":
    sub_btn = tk.Button(root, text="Close", command=Close)
else:
    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()

Current Result

enter image description here

Expected Result

enter image description here

Upvotes: 0

Views: 111

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

The main problem here is the misunderstanding of how event driven programming works. The following line of code runs ONLY when the tkinter window is initially drawn.

if user_entry["text"] == "SecondName":
    sub_btn = tk.Button(root, text="Close", command=Close)
else:
    sub_btn = tk.Button(root, text="Submit", command=Submit)

Which means user_entry["text"] is never "SecondName". Furthermore, user_entry["text"] does not do what you expect it to be doing, it returns the name of the textvariable option and not the contents of the entry widget, what you need to do is change your function to use elif:

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 == "FirstName":
        user_label["text"] = "SecondName " + user_label["text"].split()[1]
        pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
    elif char == "SecondName":
        sub_btn.config(text='Close', command=Close) # Change button if `char` is "SecondName" only

    user_var.set("")
    pass_var.set("")
    print(entries)

Side Note: To get the value inside the entry widget, you can use user_entry.get() or user_var.get()

Upvotes: 1

Related Questions