在组合框中组合两个选定的数字以创建日期(日和月)

Posted

技术标签:

【中文标题】在组合框中组合两个选定的数字以创建日期(日和月)【英文标题】:Combine two two selected numbers in a combobox to create a date (day and month) 【发布时间】:2022-01-04 04:13:04 【问题描述】:

我有两个名为 Day 和 Mounth 的组合框。通过选择两者的数字,我想将它们(日+月)合并为一个名为“日期”的元素,以获得日期。它们必须用句点“.”分隔,例如 26.12

我收到此错误:TypeError: 'str' object is not callable

day = StringVar()
month = StringVar()
date = StringVar()

def data():
    day= combo_day.get()
    month = combo_month.get()
    result = day + '.' + month
    return result


#Day
combo_day = ttk.Combobox(root, font=("Calibri", 11), width=6, textvariable=day)
combo_day['values'] = ("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31") 
combo_day.place(x=15, y=150)
combo_day.set("Day")

#Month
combo_month = ttk.Combobox(root, font=("Calibri", 11), width=6, textvariable=month)
combo_month['values'] = ("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12") 
combo_month.place(x=105, y=150)
combo_month.set("Month")

#insert in database
def add():

    data_value=data()
    db.insert(aaaa.get(), bbbb.get(), data_value())

【问题讨论】:

您是否考虑过使用tkcalendar 代替组合框? @jezza_99 是的,我使用了它,但是数字与“/”组合在一起,像 8 这样的单个数字以 08 的形式为我服务。所以我更喜欢从头开始创建两个简单的组合框。我不需要日历,只需要日期和月份的组合。你能帮我吗?谢谢 在你选择了日期和月份之后使用date = day.get() + '.' + month.get(),例如在一个按钮的回调中。 你为什么使用data_value(),因为data_value只是一个字符串? @acw1668 我该如何解决?我之前使用了你的建议。我不知道我是否拼写正确:) result = day + '.' + 月 【参考方案1】:

我建议将tkcalendar 视为 tkinter 中日期输入的一个选项,因为它易于使用并且将日期作为datetime 对象提供。然而,在不使用 tkcalendar 的情况下,做你想做的事是非常容易实现的。当两个组合框都被选中时,您只需向定义 dateStringVar 添加跟踪。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# StringVars, day and month lists
day = tk.StringVar()
month = tk.StringVar()
days = [str(i).rjust(2, "0") for i in range(1, 32)]
months = [str(i).rjust(2, "0") for i in range(1, 13)]

# Trace callback, return date if both fields filled out, None otherwise
def callback(*args):
    if day.get() in days and month.get() in months:
        date = f"day.get().month.get()"
    else:
        date = None
    print(date)

# Trace StringVars
day.trace_add('write', callback)
month.trace_add('write', callback)

# Day combobox
combo_day = ttk.Combobox(root, font=("Calibri", 11), width=6, textvariable=day, values=days)
combo_day.place(x=15, y=150)
combo_day.set("Day")

# Month combobox
combo_month = ttk.Combobox(root, font=("Calibri", 11), width=6, textvariable=month, values=months)
combo_month.place(x=105, y=150)
combo_month.set("Month")

root.mainloop()

编辑以澄清以及如何在定义后将date 添加到数据库中:

您需要将其作为回调函数,以便在更改组合框时更新date 的值。您必须在callback 函数内将date 插入到数据库中,否则它会在您启动GUI 时尝试将date 写入数据库,而当您的用户选择日期时不会。如果您不明白为什么需要为此使用函数,您应该阅读更多关于 event-driven programming 的信息。

【讨论】:

谢谢,你很客气,但有一件事我没有回复。 “日期”我想将它插入数据库。我注意到您在函数中编写了它。我是否仍然可以将“日期”保存在数据库中,或者必须将日期排除在函数之外,以便我可以调用它并将其插入数据库中? @DavidTorres0101 我已经编辑了我的答案,以便为您提供更多信息 谢谢。但是作为 Python 新手,我发现您的答案在某些地方有点困难。我通过添加更多有用的东西来更改我的代码。我想坚持我的代码。除了您对我和问题的读者都有教育目的的友好回复之外,您能帮我编写代码吗?请?我当然会投票给你的答案

以上是关于在组合框中组合两个选定的数字以创建日期(日和月)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 sql 语句中保存组合框中的选定项目和文本框中的长数字?

Vba代码显示组合框中选定的数据

在组合框中书写

根据数据绑定组合框中的选定项目从访问数据库中删除

选定的前景色未显示在 Access 组合框中

如何在vfp中的选定组合框中填充列表框?