简体   繁体   English

Tkinter - 单击第二个按钮后,将按钮 Function 更改为关闭 Window

[英]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.在我第二次单击“ Submit ”按钮后,我试图找出一种更改按钮文本和功能的方法。 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 1)在我输入SecondName的用户名/密码字段并单击Submit后,将按钮的文本从Submit更改为Close

2) Use the Close() function to close the window. 2)使用Close() function 关闭 window。

I have attempted to accomplish these two processes by using an if/else statement.我试图通过使用if/else语句来完成这两个过程。

Tkinter Code Tkinter 代码

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当前结果

在此处输入图像描述

Expected Result预期结果

在此处输入图像描述

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.以下代码行仅在最初绘制 tkinter window 时运行。

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" .这意味着user_entry["text"]永远不是"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 :此外, user_entry["text"]不做你期望它做的事情,它返回textvariable选项的name而不是条目小部件的内容,你需要做的是将你的 function 更改为使用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()旁注:要获取条目小部件内的值,您可以使用user_entry.get()user_var.get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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