Apr-18-2020, 08:03 PM
Hi everyone,
I've found this code to enter the date and that I'd like to use in tkinter but I'm having several issue:
This is how I'm using it after importing it
I cannot access to the entry input I'm trying this way:
But it gives me this error "birth_year = self.birthday.get('year')
TypeError: get() takes 1 positional argument but 2 were given". I've no idea why this is happening.
Also I'd like to edit a little the code and I was wondering if it could be possible to:
1. Po put a little bit of space between the three boxes and add a "/" between them?
2. Also right now it is possible to input incorrect date like 31 November ecc. How could I fix this?
Thank you all for your help!
I've found this code to enter the date and that I'd like to use in tkinter but I'm having several issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import tkinter as tk from datetime import datetime class DateEntry(tk.Frame): def __init__( self , parent, * * kwargs): years = kwargs.pop( 'years' , ( 1900 , 9999 )) super ().__init__(parent, * * kwargs) vcmd = ( self .register( self ._validate), '%W' , '%V' , '%v' , '%P' , '%S' ) for name, text, v1, v2 in (( 'day' , 'DD' , 1 , 31 ), ( 'month' , 'MM' , 1 , 12 ), ( 'year' , 'YYYY' , years[ 0 ], years[ 1 ])): e = tk.Entry( self , name = name, width = len (text) + 2 , justify = "center" ) e.pack(side = tk.LEFT) e.insert( 0 , text) e._valid = ( len (text), v1, v2) e.config(validate = "all" , validatecommand = vcmd) def get( self ): data = {} for entry in [ self .nametowidget(child) for child in self .children]: text = entry.get() data[entry.winfo_name()] = int (text) if text.isdigit() else None return data def _validate( self , widget, cmd, validate, value, text): # get this entry reference w = self .nametowidget(widget) # Clear entry or do nothing if cmd in ( 'focusin' , 'forced' ) or value = = '': if not value.isdigit(): w.delete( 0 , tk.END) # Set the 'validate' option again after edit w.after_idle(w.config, { 'validate' : validate}) return True # process key elif cmd = = 'key' and value.isdigit(): # get from this entry the valid parameter l, v1, v2 = w._valid # get the startswith chars if YYYY if v1 > 1 and len (value) < l: l2 = len (value) v1, v2 = int ( str (v1)[:l2]), int ( str (v2)[:l2]) # allow leading zero in DD / MM elif v1 = = 1 and len (value) = = 1 and int (value) = = 0 : return True # return True if all valid else False return all ((text.isdigit(), v1 < = int (value) < = v2, len (value) < = l)) # else return False return False |
1 2 3 4 |
self .birthday = StringVar( self ) self .birthday. set ('') self .birthday = DateEntry( self , years = ( 1935 , 2020 )) self .birthday.place(relx = 0.22 , rely = 0.16 , height = 25 , width = 100 ) |
1 2 3 4 5 |
for key in self .birthday.keys(): birth_year = self .birthday.get( 'year' ) birth_month = self .birthday.get( 'month' ) birth_day = self .birthday.get( 'day' ) birth_date = datetime.datetime(birth_year, birth_month, birth_day) |
TypeError: get() takes 1 positional argument but 2 were given". I've no idea why this is happening.
Also I'd like to edit a little the code and I was wondering if it could be possible to:
1. Po put a little bit of space between the three boxes and add a "/" between them?
2. Also right now it is possible to input incorrect date like 31 November ecc. How could I fix this?
Thank you all for your help!