【问题标题】:How do I get the Value of Tkcalendar/Date Entry如何获取 Tkcalendar/Date Entry 的值
【发布时间】:2020-11-07 04:42:57
【问题描述】:

您好,我正在构建一个 GUI,应该可以在其中插入存储在 sqllite3 数据库中的您的姓名和生日。 我使用 Tkcalendar 模块让人们选择它的生日。 我的问题是我不知道如何读出日期。 我希望有人可以帮助我!

    import sqlite3
verbindung = sqlite3.connect("Datenbanken/geb3.db")
zeiger = verbindung.cursor()
zeiger.execute("""CREATE TABLE IF NOT EXISTS people3(vorname VARCHAR(20), nachname VARCHAR(30), geburtstag DATE );""")

from tkinter import *
from tkcalendar import Calendar, DateEntry

root = Tk()



def speichern():
    zeiger.execute("""INSERT INTO people3 VALUES (?,?,?) """, (eingabefeldvorname.get(), eingabefeldnachname.get(), eingabefelddatum.get()))
    



Label(root, text = "vorname").pack()
eingabefeldvorname=StringVar()
eingabefeld = Entry(root, textvariable = eingabefeldvorname, bg ="red").pack()

Label(root, text = "nachname").pack()
eingabefeldnachname = StringVar()
eingabefeld = Entry(root, textvariable = eingabefeldnachname, bg ="green").pack()

Label(root, text = "Geburtsdatum")


eingabefelddatum= DateEntry(root, width = 20, textvariable = eingabefelddatum, bg ="yellow").pack()

konpf2 = Button(root, text = "speichern", command = speichern).pack()
root.mainloop()



zeiger.execute("SELECT * FROM people3")
inhalt = zeiger.fetchall()
print(inhalt)


def listeleeren():
     zeiger.execute("DELETE FROM people3") 

listeleeren()


verbindung.commit()
verbindung.close()```

【问题讨论】:

  • 你读过documentation吗? DateEntry 有一个get_date() 方法。
  • 另外,eingabefelddatum= DateEntry(root, width = 20, textvariable = eingabefelddatum, bg ="yellow").pack() 没有意义,eingabefelddatum 不能同时是 DateEntry 和文本变量(StringVar)
  • DateEntry 只是一个带有下拉日历的条目,因此如果您能够检索到此人的姓名,那么您可以对生日做同样的事情(您会得到日期作为字符串,而不是日期时间)。
  • 我相信 eingabefelddatum.selection_get() 应该可以完成这项工作

标签: python tkinter visual-studio-code module tkcalendar


【解决方案1】:

要获取 DataEntry 的值,函数 get_date() 起作用。

参考:https://pypi.org/project/tkcalendar/#dateentry-widget

您也可以使用<<DateEntrySelected>> 事件,每次用户用鼠标选择日期时都会生成该事件。

import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar, DateEntry

def dateentry_view():
    def print_sel(e):
        print(cal.get_date())

    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
    cal = DateEntry(top, width=12, background='darkblue',
                foreground='white', borderwidth=2)
    cal.pack(padx=10, pady=10)
    cal.bind("<<DateEntrySelected>>", print_sel)

root = tk.Tk()
ttk.Button(root, text='DateEntry', command=dateentry_view).pack(padx=10, pady=10)
root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 2021-07-11
    • 2018-05-02
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多