单击按钮时没有任何反应

Nothing happens when I click the button

这是一个无法使用的生日计算器。代码是:

import datetime
import tkinter
from tkinter import *

t = Tk()
t.resizable(0, 0)
t.title("Birthday!!!")
t.geometry('380x200')

current_date = datetime.date.today().strftime('%Y-%m-%d')
current_date_lst = current_date.split('-')

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
l1 = Label(t, textvariable=v1).grid(row=1, column=1)
l2 = Label(t, textvariable=v2).grid(row=2, column=1)
l3 = Label(t, textvariable=v3).grid(row=4, column=1)

def starting():
    global v1, v2, v3, current_date, current_date_lst
    v1.set('❤Enter your birthday in yyyy-mm-dd format plz ❤')
    v2.set('❤And What is your name sweetie?❤')

    b_date = tkinter.Entry(t)
    b_date.grid(row=1, column=2)
    name = tkinter.Entry(t)
    name.grid(row=2, column=2)
    this = b_date.get()
    that = name.get()
    def haha():
        t.bind("<KeyRelease>", lambda e: show_string(b_date, name))
        def show_string(b_date, name):
            this.split( '-' )
            if len(this)==3:
                if (current_date_lst[1] == b_date[1] and current_date_lst[2] == b_date[2]) == False:
                    v3.set('Sorry, today is not your birthday:(')
                else:
                    age = int(current_date_lst[0]) - int(b_date[0])
                    v3.set("Congrats! {that} is {age} now!")
    def reset_now():
        result = b_date.get()
        length = len(result)
        b_date.delete(0, length)
        result2 = name.get()
        length2 = len(result2)
        name.delete(0, length2)
        v3.set('')      
    b2 = Button(text='Is it my Bday?', command=haha)
    b2.grid(row=3, column=1)
    b3 = Button(text='Reset', command=reset_now)
    b3.grid(row=3, column=2)

b1 = Button(text='Start', command=starting)
b1.grid(row=1, column=3)
mainloop()

每当我点击按钮 b2 时,它应该会显示结果,但正如我告诉过你的,实际上什么也没有发生。我尝试了不同的方法,但其中 none 有帮助。你认为我能做什么?

this = b_name.get()that = name.get()放在haha()里面,因为haha()函数运行时,v1、[=16的输入数据=] 可以得到。其次,您需要 运行 show_string() 函数,而不是 bind。将 show_string(b_name, name) 替换为 show_string(this, that),因为 b_namenameEntry,而不是字符串。

import datetime
import tkinter
from tkinter import *

t = Tk()
t.resizable(0, 0)
t.title("Birthday!!!")
t.geometry('380x200')

current_date = datetime.date.today().strftime('%Y-%m-%d')
current_date_lst = current_date.split('-')

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
l1 = Label(t, textvariable=v1).grid(row=1, column=1)
l2 = Label(t, textvariable=v2).grid(row=2, column=1)
l3 = Label(t, textvariable=v3).grid(row=4, column=1)

def starting():
    global v1, v2, v3, current_date, current_date_lst
    v1.set('❤Enter your birthday in yyyy-mm-dd format plz ❤')
    v2.set('❤And What is your name sweetie?❤')

    b_date = tkinter.Entry(t)
    b_date.grid(row=1, column=2)
    name = tkinter.Entry(t)
    name.grid(row=2, column=2)

    def haha():
        this = b_date.get()
        that = name.get()
        def show_string(b_date, name):
            nlist = this.split('-')
            if len(nlist)==3:
                if not (str(current_date_lst[1]) == str(nlist[1]) and 
                   str(current_date_lst[2]) == str(nlist[2])):
                   v3.set('Sorry, today is not your birthday:(')
                else:
                    age = int(current_date_lst[0]) - int(nlist[0])
                    v3.set(f"Congrats! {that} is {age} now!")
       show_string(this,that)
    def reset_now():
        result = b_date.get()
        length = len(result)
        b_date.delete(0, length)
        result2 = name.get()
        length2 = len(result2)
        name.delete(0, length2)
        v3.set('')      
    b2 = Button(text='Is it my Bday?', command=haha)
    b2.grid(row=3, column=1)
    b3 = Button(text='Reset', command=reset_now)
    b3.grid(row=3, column=2)

b1 = Button(text='Start', command=starting)
b1.grid(row=1, column=3)
mainloop()

编辑:我的错误是 str(current_date_lst[1]) == str(this[1]) and str(current_date_lst[2]) == str(this[2]),我现在已修复,因为 b_name 现在是字符串,而不是列表,并且 else 函数必须在 if len(nlist)==3 内。

你为什么不简单地执行你需要的任何东西而不绑定任何东西(至少你没有指定为什么这需要绑定到任何东西):

def haha():
    this.split('-')
    if len(this) == 3:
        if (current_date_lst[1] == b_date[1] and current_date_lst[2] == b_date[2]) == False:
            v3.set('Sorry, today is not your birthday:(')
        else:
            age = int(current_date_lst[0]) - int(b_date[0])
            v3.set("Congrats! {that} is {age} now!")

你也知道,对,这个:this.split('-') returns 一个列表,这意味着不将它分配给任何东西都没有任何作用,所以你可以尝试这样的事情(不要改变太多):

this = this.split('-')

还有你为什么不这样做呢:

if current_date == b_date.get():

一个简单的比较,不需要拆分任何东西,

我也注意到了这个问题:

b_date = tkinter.Entry(t)
b_date.grid(row=1, column=2)
name = tkinter.Entry(t)
name.grid(row=2, column=2)
this = b_date.get()
that = name.get()

你创建条目,然后将它们网格化并立即尝试获取它们的值,你认为这将如何工作?你需要在用户输入内容后使用 .get() 否则你只会得到一个空字符串,

还有:

b_date[1]

并且类似的行会引发错误,因为我很确定 tkinter 小部件没有属性 2

函数 haha 只是将 t 绑定到 KeyRelease。而且, show_string 只是在其中定义。您必须实际调用该函数。

    def haha():
        
        def show_string(b_date, name):
            this.split( '-' )
            if len(this)==3:
                if (current_date_lst[1] == b_date[1] and current_date_lst[2] == b_date[2]) == False:
                    v3.set('Sorry, today is not your birthday:(')
                else:
                    age = int(current_date_lst[0]) - int(b_date[0])
                    v3.set("Congrats! {that} is {age} now!")
        show_string(b_date.get(), name.get())

很好 none 的答案很有帮助,所以我决定自己解决它。

the_day = str(current_date_lst[14]) + str(current_date_lst[15])
the_month = str(current_date_lst[8]) + str(current_date_lst[9])
the_year = str(current_date_lst[2]) + str(current_date_lst[3])

def haha():
        this = str(b_date.get())
        that = name.get()
        def show_string():
            umm = this.split('-')
            if len(umm)==3:
                if the_month == umm[1] and the_day == umm[2]:
                    age = int(the_year) - int(umm[0])
                    tada = "Congrats! " + str(that) +  " is " +  str(age) +  " today!"
                    v3.set(tada)
                else:
                    v3.set('Nah, today is not your birthday:(')
            else:
                messagebox.showerror(title='Error', message='Enter ypu B-day completely')

添加这些行后,程序运行正常,不再有麻烦!

P.S: 是的,我知道变量的名字很傻