python-3.x - 需要从 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()
解决方案
推荐阅读
- django - getaddrinfo: xxURL 从代理 gai_error = 0 获取结果
- python - Sort a list from a file
- java - 黄瓜变量参数单独存储
- javascript - UTC 日期时间到 ES6 中的完整日期
- c# - 为登录到 SSH.NET 中的设备时自动显示的提示提供输入
- python - pandas 数据框到现有的 excel 工作表并用 openpyxl 换行列文本
- javascript - 如何使用 Mongoose ref 连接创建嵌套对象?
- c# - 保留文本框的先前值,直到表单关闭
- python - 有一个意外的关键字参数
- javascript - 存储的图像保存为“八位字节流”而不是 image/jpeg(firebase 和 ReactNative)