1
from tkinter import *
root=Tk()
root.geometry("400x300")
def e_input():
    print(e1.get())
l1=Label(root,text="Enter here:")
l1.place(x=30,y=40)
e1=Entry(root,bd=2,width=25)
e1.place(x=100,y=40)
b1=Button(root,text="Enter",command=e_input)
b1.place(x=250,y=40)
root.mainloop()

In this code, the user manually has to enter the slashes after every 2 characters. help me out to make that thing by default, Image attached as to how I needed it to be... enter image description here

Hafeezh
  • 61
  • 6
  • The image you show doesn't have slashes after _every_ two characters. It only has a slash after the first two and then the next two characters. – Bryan Oakley Aug 27 '20 at 18:01
  • I am sorry, actually, I was planning to take input from the user for hours minutes, and second, in that context, I wrote that I needed python to automatically put slashes after every 2 characters. – Hafeezh Aug 27 '20 at 18:06
  • but, you get the point I needed python to automatically put slashes after every two characters – Hafeezh Aug 27 '20 at 18:07
  • Ok, so you do not want what the picture shows. Instead, you want `21/04/20/20`. Is that correct? – Bryan Oakley Aug 27 '20 at 18:18
  • @Bryanoakley is there a way to do it, while the user types in the entry itself? I had asked a Q before and got no proper answers – Delrius Euphoria Aug 27 '20 at 18:25
  • @BryanOakley Correct!! – Hafeezh Aug 27 '20 at 18:35
  • actually original it was supposed to be 14:04:02 but I don't know how did I lose my track. and Now it became 21/04/20/20 – Hafeezh Aug 27 '20 at 18:38
  • this is a small project wherein we are supposed to take in input from the user for the time but with the automatic colon being the respectively places – Hafeezh Aug 27 '20 at 18:39
  • What it you take the input and press a button and then after that colon gets added on? i asked a similar Q [here](https://stackoverflow.com/questions/62119551/auto-add-dashes-to-entryboxes-in-tkinter) but didnt get any proper answer out – Delrius Euphoria Aug 27 '20 at 18:49
  • 1
    Really a good question – Hafeezh Aug 27 '20 at 18:56
  • 1
    an idea sparked in my head what if we use for loop(inside a while loop, label.bind(''), and after every 2 characters a colon is given to the str. I am not sure will this work or not, just thought of it. – Hafeezh Aug 27 '20 at 19:00
  • sorry entry.bind not label.bind – Hafeezh Aug 27 '20 at 19:01

1 Answers1

0

In my example we expand the Entry widget to handle your date format. The validatecommand makes sure that we are entering numbers and that the text matches the format of the regular expression. The key bind handles inserting slashes in the proper position.

import tkinter as tk, re

class DateEntry(tk.Entry):
    def __init__(self, master, **kwargs):
        tk.Entry.__init__(self, master, **kwargs)
        vcmd = self.register(self.validate)
        
        self.bind('<Key>', self.format)
        self.configure(validate="all", validatecommand=(vcmd, '%P'))
        
        self.valid = re.compile('^\d{0,2}(\\\\\d{0,2}(\\\\\d{0,4})?)?$', re.I)
             
    def validate(self, text):
        if ''.join(text.split('\\')).isnumeric():
            return not self.valid.match(text) is None
        return False
    
    def format(self, event):
        if event.keysym != 'BackSpace':
            i = self.index('insert')
            if i in [2, 5]:
                if self.get()[i:i+1] != '\\':
                    self.insert(i, '\\')
        

class Main(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        
        DateEntry(self, width=10).grid(row=0, column=0)
        

if __name__ == "__main__":
    root = Main()
    root.geometry('800x600')
    root.title("Date Entry Example")
    root.mainloop()
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • Thanks, Mate, your code did work, I am very new to python recently I learned about classes and this (tkinter module). This will work perfectly for the day/month/year, but what if I needed to change it to time (for example 14:20:04) how should I change it – Hafeezh Aug 27 '20 at 19:11
  • @Hafeezh ~ Ask a time question and I will answer it – OneMadGypsy Aug 27 '20 at 19:11
  • self.valid = re.compile('^\d{0,2}(\\\\\d{0,2}(\\\\\d{0,4})?)?$', re.I) , this few codes of line went above my head, would surly google it out!! – Hafeezh Aug 27 '20 at 19:13
  • [link](https://stackoverflow.com/questions/63622880/how-to-make-python-automaticlly-put-colon-in-the-format-of-time-hhmmss) – Hafeezh Aug 27 '20 at 19:21
  • Hey @MichaelGuidry can you take a look at [this](https://stackoverflow.com/questions/62119551/auto-add-dashes-to-entryboxes-in-tkinter) and maybe suggest a similar solution there?im not into OOP much though – Delrius Euphoria Aug 27 '20 at 20:00