当使用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,这样当按下check按钮时,clear(。当我运行代码并按下检查按钮时,它会遇到一个无穷大的错误。

我寻找解决方案,但我想问题的措辞是唯一的问题,为什么我没有得到确切的解决方案。

因此,你能建议我如何在按下该按钮时向该按钮添加另一个功能吗?这样它将清除初始输出并显示下一个输出,同时在给定按下按钮的条件时不会出现无穷大的错误。

PS。因为我是个初学者。我很乐意听到一些建议和技巧,让我的代码更干净、更高效。

只需制作一个标签,并使用主代码中的.config() method进行更改,而不是result_1, ...

result = tk.Label(window, text = '' , font = ("Times", 30, "bold"))
result.pack() # This is not under function

然后更改函数check()

def check():
global result
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.configure(text=f"{YEAR} is a leap year.")
else:
result.configure(text=f"{YEAR} is not a leap year.")
else:
result.configure(text=f"{YEAR} is a leap year.")
else:
result.configure(text=f"{YEAR} is not leap year.")

您可以利用.config()。它允许配置指定小部件的所有参数。您可以打包不带任何文本的标签,然后使用.config(text=...)。此外,最好在try:块内部进行计算,因为无论何时引发ValueError,都不会定义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()

最新更新