首页 > 解决方案 > 需要从 2 个日历函数中获取值,找到差异并将其存储在标签小部件中

问题描述

我在这里尝试了大多数可能的事情,但可以让它发挥作用。任何帮助将不胜感激。

我希望用户选择日期,然后获取这些值,将其存储在 a 和 b 中,找到差异并将其存储在 y 中。

但实际发生的是,当我调用 cal_fun1 时,它甚至在允许用户选择之前就获得了 b 的值。

我怎样才能改变这个?我还需要将值存储在 L3 中,作为差异。

from tkinter import *
from tkcalendar import Calendar, DateEntry


    def pay_cal():

        def cal_fun():
            t = Toplevel()
            global cal
            cal = Calendar(t, year=2019, month=6, day=1, foreground='Blue', background='White', selectmode='day')
            cal.pack()
            cal.bind("<<CalendarSelected>>",lambda e: c.set(cal.get_date()))#make use of the virtual event to dynamically update the text variable c


        def cal_fun1():
            t = Toplevel()
            global cal1, y
            cal1 = Calendar(t, foreground='Blue', background='White', selectmode='day')
            cal1.pack()
            cal1.bind("<<CalendarSelected>>", lambda e: d.set(cal1.get_date()) ) # make use of the virtual event to dynamically update the text variable c
            a=cal.selection_get()
            b=cal1.selection_get()
            y =a-b
            print(y)    #this is only for testing the output

        sub_win = Tk()
        sub_win.geometry('400x500+600+100')
        sub_win.title('Payout Calculator')
        c = StringVar() #create a stringvar here - note that you can only create it after the creation of a TK instance
        c.set(0)
        d = StringVar()  # create a stringvar here - note that you can only create it after the creation of a TK instance
        d.set(0)
        y = StringVar()




        l1 = Button(sub_win, text= 'Check-In Date:', command= cal_fun)
        chck_in_date = Label(sub_win, textvariable=c)
        l1.grid(row=1)
        chck_in_date.grid(row=1, column=2)



        l2 = Button(sub_win, text='Can Date:', command=cal_fun1)
        q = Label(sub_win, textvariable=d)
        l2.grid(row=2)
        q.grid(row=2, column=2)



        total_days = Label(sub_win, textvariable= y.get())
        L3 = Label(sub_win, text="Total Days")
        L3.grid(row=3)
        total_days.grid(row=3, column=2)




        sub_win.mainloop()

    pay_cal()

标签: python-3.xtkintercalendarnested-function

解决方案


推荐阅读