Validate two fields

Post here if you need help with creating a Graphical User Interface in Python.

Validate two fields

Postby iFunction » Mon Aug 22, 2016 5:24 pm

Hi there,

I'm trying to make an GUI that sets the time and date on a Raspberry Pi. I have two entry fields that set the hours and minutes, and I am trying to set it so they are validated. However, I am getting a little stuck trying to work out how both fields are to be validated at the same time, do I actually need two functions, one for each field, I was hoping to just use the one function otherwise it kind of makes it pointless having the function. Here is the code, it is not complete yet obviously, but currently it runs:
Code: Select all
#!/usr/bin/python3

# test.py
# sandbox for testing python scripts

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        master.title("Set Time")
       
        # Frame to hold
        self.intro = tk.Frame(master)
        self.buttons = tk.Frame(master)
        self.eventdate = tk.Frame(self.buttons)
        self.eventtime = tk.Frame(self.buttons)
       
        self.description = tk.Label(self.intro, text="Please set date and time")
        self.description.grid()
       
        # Date Picker
        self.daylabel = tk.Label(self.eventdate,text="Day")
        self.daylst = list(range(1,32))
        self.dayvar = tk.StringVar()
        self.daydrop = tk.OptionMenu(self.eventdate,self.dayvar,*self.daylst)
       
        self.monthlabel = tk.Label(self.eventdate,text="Month")
        self.monthlst = ['January',
                         'February',
                         'March',
                         'April',
                         'May',
                         'June',
                         'July',
                         'August',
                         'September',
                         'October',
                         'November',
                         'December']
        self.monthvar = tk.StringVar()
        self.monthdrop = tk.OptionMenu(self.eventdate,self.monthvar,*self.monthlst)
       
        self.yearlabel = tk.Label(self.eventdate,text="Year")
        self.yearlst = list(range(2016,2021))
        self.yearvar = tk.StringVar()
        self.yeardrop = tk.OptionMenu(self.eventdate,self.yearvar,*self.yearlst)
       
        # Time Picker
        self.hourlabel = tk.Label(self.eventtime,text="Hours")
        vcmd = master.register(self.validate)
        self.hourentry = tk.Entry(self.eventtime, validate="key", validatecommand=(vcmd, '%P'))
        self.ok_button = tk.Button(master, text="OK", command=lambda: self.update("ok"))
       
       
        self.minlabel = tk.Label(self.eventtime,text="Mins")
        vcmd = master.register(self.validate)
        self.minentry = tk.Entry(self.eventtime, validate="key", validatecommand=(vcmd, '%P'))
        self.ok_button = tk.Button(master, text="OK", command=lambda: self.update("ok"))
       
       
        # LAYOUT
        self.intro.grid(row=0)
        self.buttons.grid(row=1)
       
        # Dropdown and entrybox frame
        self.eventdate.grid(row=0)
        self.eventtime.grid(row=1)
       
        # Date dropdowns frame
        self.daylabel.grid(row=0, column=0)
        self.daydrop.grid(row=1, column=0)
        self.monthlabel.grid(row=0, column=1)
        self.monthdrop.grid(row=1, column=1)
        self.yearlabel.grid(row=0, column=2)
        self.yeardrop.grid(row=1, column=2)
       
        # Time entry frame
        self.hourlabel.grid(row=0, column=0)
        self.hourentry.grid(row=1, column=0)
        self.minlabel.grid(row=0, column=1)
        self.minentry.grid(row=1, column=1)
       
        # Ok and Cancel buttons frame
        self.ok_button.grid(row=2, column=0)
#        self.cancel_button.grid(row=2, column=1)

    def validate(self, new_value):
        if not new_value:
            return True
       
       
        try:
            self.entered_number = int(new_value)
            return True
        except ValueError:
            return False
       
    def update(self, method):
        if method == "ok":
            print(self.entered_number)
        else:
            print("Goodbye")
           
        self.hourentry.delete(0, tk.END)
        self.minentry.delete(0, tk.END)

       
if __name__ == "__main__":
    root = tk.Tk()
    app = Application(root)
    app.mainloop()



It's fairly obvious from the code how it is designed to work, but at the moment, I can't figure out a simple way of using the one validate function.
Any help or pointers on this would be very much appreciated.

Kind regards
iFunc
iFunction
 
Posts: 41
Joined: Mon Apr 04, 2016 2:33 pm

Re: Validate two fields

Postby jestoy0514 » Tue Aug 30, 2016 10:46 am

Hi I am just a little confuse, do you want to validate whether the value of hour and minutes is in range of 24 for hour and in range of 60 for minutes using only one function?
jestoy0514
 
Posts: 10
Joined: Tue Jun 07, 2016 6:50 am

Re: Validate two fields

Postby Larz60+ » Tue Aug 30, 2016 11:27 am

Hello,

If you are attempting to verify that the format, vs the actual time is correct,
you can convert to a datetime format and easily determine if the format is correct for
any date, or timezone, including test for leap year and daylight savings etc.

see https://pymotw.com/2/datetime/ for a full set of functions that will do this.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm


Return to GUI

Who is online

Users browsing this forum: Google [Bot] and 2 guests