首页 > 解决方案 > 当使用 tkinter 再次按下具有初始功能的按钮时,如何添加和执行另一个功能?

问题描述

所以...我正在自己编写我的第一个项目之一。这是一个使用 Tkinter 检查您输入的年份是否为闰年的程序。所以这是程序:

import tkinter as tk
import tkinter.messagebox

def check():
    global year
    year = entry_1.get()
    try:
        YEAR = int(year)
    except ValueError:
        tkinter.messagebox.askokcancel(title = "Invalid input", message = "You can only use whole numbers.")
    if YEAR%4 == 0:
        if YEAR%100 == 0:
            if YEAR%400 == 0:
                result_1 = tk.Label(window, text = f"{YEAR} is a leap year.", font = ("Times", 30, "bold"))
                result_1.pack()
            else:
                result_2 = tk.Label(window, text = f"{YEAR} is not a leap year.", font = ("Times", 30, "bold"))
                result_2.pack()
        else:
            result_3 = tk.Label(window, text = f"{YEAR} is a leap year.", font = ("Times", 30, "bold"))
            result_3.pack()
    else:
        result_4 = tk.Label(window, text = f"{YEAR} is not a leap year.", font = ("Times", 30, "bold"))
        result_4.pack()



window = tk.Tk()
window.title("Leap year Checker")
window.resizable(0, 0)

window.geometry("500x350")

label_0 = tk.Label(window, text = "Leap Year Checker", font = ("Verdana", 20, "bold underline"), anchor = "center")
label_0.pack()

label_1 = tk.Label(window, text = "Enter the year: ", font = ("Times", 20), anchor = "center")
label_1.pack()

entry_1 = tk.Entry(window, text = "YYYY", width = 15)
entry_1.pack()

year = 0

button_1 = tk.Button(window, text = "Check", anchor = "e", command = check)
button_1.place(x = 320, y = 72)

window.mainloop()

该程序运行良好,直到您第二次给出输入,而不是覆盖第一个输出,而是显示在第一个输出的正下方。所以我正在考虑在 check() 函数本身中添加另一个函数,比如 clear() 来清除第一个输出。因此,每当我再次按下带有 check() 的按钮时,它都会清除第一个输出,而第二个输出将被覆盖。但我不知道如何使用 Tkinter 做到这一点。我尝试添加一个名为 clear() 的函数,并将结果标签作为参数,并添加了一个条件 if check() == true 以便当按下检查按钮时,也会执行 clear() ,我添加了一些条件从那时起,输出应该开始覆盖,以便在按下按钮时添加下一个输入后立即清除初始输出,而不是第一个输入。

我搜索了解决方案,但我想问题的措辞是我没有得到确切解决方案的唯一问题。

那么您能否建议我在按下该按钮时如何在该按钮中添加另一个功能,以便它将清除初始输出并显示下一个输出,同时在无穷大时不会遇到错误给出按下按钮的条件。

PS。因为我是初学者。我很想听听建议和技巧,以使我的代码更简洁、更高效。

标签: pythontkinter

解决方案


您可以利用.config(). 它允许配置指定小部件的所有参数。您可以在不带任何文本的情况下打包标签,然后使用.config(text=...). 此外,最好在try:块内进行计算,因为无论何时ValueError提出 a ,YEAR都没有定义。这实际上会引发一个UnboundLocalError

import tkinter as tk
import tkinter.messagebox

def check():
    global year
    year = entry_1.get()
    try:
        YEAR = int(year)
        if YEAR%4 == 0:
            if YEAR%100 == 0:
                if YEAR%400 == 0:
                    
                    result_1.config(text=f"{YEAR} is a leap year.")
                else:
                    result_1.config(text=f"{YEAR} is not a leap year.")
            else:
                result_1.config(text=f"{YEAR} is a leap year.")
        else:
            result_1.config(text=f"{YEAR} is not a leap year.")

    except ValueError:
        tkinter.messagebox.askokcancel(title = "Invalid input", message = "You can only use whole numbers.")
    
window = tk.Tk()
window.title("Leap year Checker")

window.resizable(0, 0)
window.geometry("500x350")

label_0 = tk.Label(window, text = "Leap Year Checker", font = ("Verdana", 20, "bold underline"), anchor = "center")
label_0.pack()

label_1 = tk.Label(window, text = "Enter the year: ", font = ("Times", 20), anchor = "center")
label_1.pack()

entry_1 = tk.Entry(window, text = "YYYY", width = 15)
entry_1.pack()

year = 0

button_1 = tk.Button(window, text = "Check", anchor = "e", command = check)
button_1.place(x = 320, y = 72)
result_1 = tk.Label(window, font = ("Times", 30, "bold"))
result_1.pack()

window.mainloop()

推荐阅读