4

我想要一个用于输入日期的输入框,以便用户只能以一种方式输入。

输入前的输入框看起来像这样(不带下划线)__/__/____,并且/在用户输入日期时不会删除 's。

谢谢

4

2 回答 2

14

感谢@Bryan Oakley 的主要提示,我想出了这个工作代码。

(更新:投入了几年的编程经验和一些最近的读者对改进代码的敏锐眼光。)

from __future__ import print_function

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk


class DateEntry(tk.Frame):
    def __init__(self, master, frame_look={}, **look):
        args = dict(relief=tk.SUNKEN, border=1)
        args.update(frame_look)
        tk.Frame.__init__(self, master, **args)

        args = {'relief': tk.FLAT}
        args.update(look)

        self.entry_1 = tk.Entry(self, width=2, **args)
        self.label_1 = tk.Label(self, text='/', **args)
        self.entry_2 = tk.Entry(self, width=2, **args)
        self.label_2 = tk.Label(self, text='/', **args)
        self.entry_3 = tk.Entry(self, width=4, **args)

        self.entry_1.pack(side=tk.LEFT)
        self.label_1.pack(side=tk.LEFT)
        self.entry_2.pack(side=tk.LEFT)
        self.label_2.pack(side=tk.LEFT)
        self.entry_3.pack(side=tk.LEFT)

        self.entries = [self.entry_1, self.entry_2, self.entry_3]

        self.entry_1.bind('<KeyRelease>', lambda e: self._check(0, 2))
        self.entry_2.bind('<KeyRelease>', lambda e: self._check(1, 2))
        self.entry_3.bind('<KeyRelease>', lambda e: self._check(2, 4))

    def _backspace(self, entry):
        cont = entry.get()
        entry.delete(0, tk.END)
        entry.insert(0, cont[:-1])

    def _check(self, index, size):
        entry = self.entries[index]
        next_index = index + 1
        next_entry = self.entries[next_index] if next_index < len(self.entries) else None
        data = entry.get()

        if len(data) > size or not data.isdigit():
            self._backspace(entry)
        if len(data) >= size and next_entry:
            next_entry.focus()

    def get(self):
        return [e.get() for e in self.entries]


if __name__ == '__main__':        
    win = tk.Tk()
    win.title('DateEntry demo')

    dentry = DateEntry(win, font=('Helvetica', 40, tk.NORMAL), border=0)
    dentry.pack()

    win.bind('<Return>', lambda e: print(dentry.get()))
    win.mainloop()
于 2012-11-06T03:35:08.103 回答
0

我发现了一个非常简单的解决方案。我希望有人能发现它有用。

self.entry1=tk.Entry(self)
self.entry1.insert(END, "    /    /        ")
self.entry1.config(fg="grey")

def datemask(self, event):
if len(self.entry1.get()) is 2:
    self.entry1.insert(END,"/")

elif len(self.entry1.get()) is 5:
    self.entry1.insert(END,"/")

elif len(self.entry1.get()) is 11:
    self.entry1.delete(10, END)

绑定入口小部件

self.entry1.bind('<KeyRelease>', self.datemask)
于 2020-05-14T19:07:06.993 回答