How to get date from tkcalendar in tkinter gui?

873 Views Asked by At

I am trying to create an employee web app. I have created the home page, login page and application page. The application page gets the details of the candidates who want to apply for a job.

I have used tkcalendar module for selecting 'expected start date'. The GUI works fine but I am not able to get the value of the date selected.

I tried to use get_date() but it shows the following error:

self.date = self.choose_date.get_date()
AttributeError: 'Button' object has no attribute 'get_date'

This is the code I executed.

import tkinter as tk
from tkinter import ttk
import mysql.connector
from tkcalendar import *

class ABCApp(tk.Tk):
    BKGR_IMAGE_PATH = 'images\\bg5.png'

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)
        self.geometry("1500x750")

        main_frame = tk.Frame(self,width=200,height=50,highlightbackground="black",highlightthickness=1,background = "#e6ffe6")
        main_frame.pack(side='top', fill='both', expand='True')

        main_frame.grid_rowconfigure(0, weight=1)
        main_frame.grid_columnconfigure(0, weight=1)

        self.bkgr_image = tk.PhotoImage(file=self.BKGR_IMAGE_PATH)

        self.frames = {}
        for F in (HomePage,LogIn,PersonalPage,ApplicationPage):
            frame = F(main_frame, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')

        self.show_frame(HomePage)

    def show_frame(self,container):
        frame = self.frames[container]
        frame.tkraise()

class BasePage(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)

        label_bkgr = tk.Label(self, image=controller.bkgr_image)
        label_bkgr.place(x=0,y=0)  # Center label w/image.


class HomePage(BasePage):

    def __init__(self, parent, controller):
        super().__init__(parent, controller)

        label = ttk.Label(self, text='Home Page', font =("Helvetica",20))
        label.pack(padx=10, pady=10)

        button1 = ttk.Button(self, text="Log In",
                             command=lambda: controller.show_frame(LogIn))
        button1.pack()



class LogIn(BasePage):

    def __init__(self,parent,controller):
        super().__init__(parent, controller)

        login_frame = tk.Frame(self, width=200, height=50, background="white")
        login_frame.grid(row=400, column=20, padx=500, pady=250)

        self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
        self.label_title.grid(row=0, column=20, padx=10, pady=10)

        self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
        self.label_username.grid(row=50, column=20, padx=10, pady=10)

        self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
        self.entry_username.grid(row=50, column=30, padx=10, pady=10)

        self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
        self.label_password.grid(row=60, column=20, padx=10, pady=10)

        self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
        self.entry_password.grid(row=60, column=30, padx=10, pady=10)

        self.login_button = tk.Button(login_frame, text="Log In",command=lambda: [self.submit(),controller.show_frame(PersonalPage)],font=("Helvetica", 20),bg="white")
        self.login_button.grid(row=70, column=25, padx=10, pady=10)

    def submit(self):

        self.u_name = self.entry_username.get()
        self.p_word = self.entry_password.get()

        employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
        cursor_variable = employee.cursor()

        cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
        employee.commit()

        employee.close()

class PersonalPage(BasePage):

    def __init__(self, parent, controller):
        super().__init__(parent, controller)

        personal_frame = tk.Frame(self, width=200, height=100, background="white")
        personal_frame.grid(row=50, column=150, padx=300, pady=100)

        personal_details_frame = tk.Frame(personal_frame,width =150,height =50,background="#e6ffe6")
        personal_details_frame.grid(row =55,column =0,padx = 50,pady = 50)

        self.label_title = tk.Label(personal_details_frame, text="Personal Details",font=("Helvetica", 20),bg ="#e6ffe6")
        self.label_title.grid(row=10, column=0, sticky='W')

        self.label_name = tk.Label(personal_details_frame, text="Name",font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_name.grid(row=15, column=0, padx=10, pady=10, sticky='W')

        self.entry_name = tk.Entry(personal_details_frame,bd=3, font=("Helvetica", 12), width=50)
        self.entry_name.grid(row=15, column=10, padx=10, pady=10, sticky='W')

        self.label_email = tk.Label(personal_details_frame, text="Email id", font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_email.grid(row=30, column=0, padx=10, pady=10, sticky='W')

        self.entry_email = tk.Entry(personal_details_frame, bd=3, font=("Helvetica", 12), width=50)
        self.entry_email.grid(row=30, column=10, padx=10, pady=10, sticky='W')

        self.label_mobilenumber = tk.Label(personal_details_frame, text="Mobile Number",font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_mobilenumber.grid(row=45, column=0, padx=10, pady=10, sticky='W')

        self.entry_mobile = tk.Entry(personal_details_frame,bd=3, font=("Helvetica", 12), width=50)
        self.entry_mobile.grid(row=45, column=10, padx=10, pady=10, sticky='W')

        self.label_address = tk.Label(personal_details_frame, text="Address",font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_address.grid(row=60, column=0, padx=10, pady=10, sticky='W')

        self.text_address = tk.Text(personal_details_frame,bd=3, font=("Helvetica", 12), width=50, height=5)
        self.text_address.grid(row=60, column=10, padx=10, pady=10, sticky='W')

        self.button_next = tk.Button(personal_details_frame,text = "Next",font = ("Helvetica",12),bg ="#b3ccff",command=lambda: [self.personal_next(),controller.show_frame(ApplicationPage)])
        self.button_next.grid(row = 70,column =20,padx=10,pady=10,sticky='W')

    def personal_next(self):
        self.name = self.entry_name.get()
        self.email = self.entry_email.get()
        self.mobile = self.entry_mobile.get()
        self.address = self.text_address.get("1.0","end-1c")

        employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
        cursor_variable = employee.cursor()

        cursor_variable.execute("INSERT INTO personal VALUES ('" + self.name + "','" + self.email + "','" + self.mobile + "','" + self.address + "')")
        employee.commit()

        employee.close()

class ApplicationPage(BasePage):

    def __init__(self, parent, controller):
        super().__init__(parent, controller)

        application_frame = tk.Frame(self, width=200, height=100, background="white")
        application_frame.grid(row=50, column=150, padx=300, pady=100)

        application_details_frame = tk.Frame(application_frame, width=150, height=50, background="#e6ffe6")
        application_details_frame.grid(row=55, column=0, padx=50, pady=50)

        self.label_title = tk.Label(application_details_frame, text="Application Details", font=("Helvetica", 20),bg="#e6ffe6")
        self.label_title.grid(row=10, column=0, sticky='W')

        self.label_position = tk.Label(application_details_frame, text="Applied for Position", font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_position.grid(row=75, column=0, padx=10, pady=10, sticky='W')

        self.list_box1 = tk.Listbox(application_details_frame, width=50, height=3, font=("Helvetica", 12))
        self.list_box1.insert(1, "Database Admin")
        self.list_box1.insert(2, "Network Admin")
        self.list_box1.insert(3, "Deployment Admin")
        self.list_box1.grid(row=75, column=10, padx=10, pady=10, sticky='w')

        self.label_date = tk.Label(application_details_frame, text="Expected Start Date", font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_date.grid(row=90, column=0, padx=10, pady=10, sticky='W')

        self.choose_date = tk.Button(application_details_frame, text="Choose Available Date", command=lambda: self.open_calendar(), width=49, bg="white",font=("Helvetica", 12), anchor='w')
        self.choose_date.grid(row=90, column=10, padx=10, pady=10, sticky='w')

        self.label_proficiency = tk.Label(application_details_frame, text="Proficient Language", font=("Helvetica", 12),bg ="#e6ffe6")
        self.label_proficiency.grid(row=120, column=0, padx=10, pady=10, sticky='W')

        self.radio = tk.IntVar()
        self.radio_button1 = tk.Radiobutton(application_details_frame, text="Java", variable=self.radio, value=1, font=("Helvetica", 12),bg ="#e6ffe6")
        self.radio_button1.grid(row=120, column=10, padx=10, pady=10, sticky='w')
        self.radio_button2 = tk.Radiobutton(application_details_frame, text="SQL", variable=self.radio, value=2, font=("Helvetica", 12),bg ="#e6ffe6")
        self.radio_button2.grid(row=135, column=10, padx=10, pady=10, sticky='w')
        self.radio_button3 = tk.Radiobutton(application_details_frame, text="C", variable=self.radio, value=3, font=("Helvetica", 12),bg ="#e6ffe6")
        self.radio_button3.grid(row=150, column=10, padx=10, pady=10, sticky='w')

        self.submit_button = tk.Button(application_details_frame,text = "Submit",command = self.submit(),font = ("Helvetica",12),bg ="#b3ccff")
        self.submit_button.grid(row = 220,column =10,padx=10,pady=10,sticky='W')

    def submit(self):

        self.position = self.list_box1.get(tk.ANCHOR)
        self.date = self.choose_date.get_date()

        employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
        cursor_variable = employee.cursor()

        cursor_variable.execute("INSERT INTO application VALUES ('" + self.position + "','"+self.date+"')")
        employee.commit()

        employee.close()

    def open_calendar(self):
        self.toplevel1 = tk.Toplevel(self)
        self.calendar_variable = Calendar(self.toplevel1, selectmode="day", year=2021, month=4, day=10)
        self.calendar_variable.pack()
        self.toplevel1.mainloop()


app = ABCApp()
app.mainloop()

Any help is appreciated.

1

There are 1 best solutions below

7
On
  • First the following line:
self.submit_button = tk.Button(application_details_frame,text = "Submit",command = self.submit(),font = ("Helvetica",12),bg ="#b3ccff")

will execute self.submit() immediately. It should be:

self.submit_button = tk.Button(application_details_frame, text="Submit", command=self.submit, font=("Helvetica",12), bg="#b3ccff")
  • Second as the error said, self.choose_date is a tk.Button not tkcalendar.Calendar. Better create an instance variable of tk.StringVar and associate it to the tkcalendar.Calendar widget. Then you can use this instance variable to get the selected date:
class ApplicationPage(BasePage):
    def __init__(self, parent, controller):
        ...
        self.selected_date = tk.StringVar()

    def submit(self):
        self.position = self.list_box1.get(tk.ANCHOR)
        self.date = self.selected_date.get()  # get the selected date
        ...

    def open_calendar(self):
        ...
        self.calendar_variable = Calendar(self.toplevel1, selectmode="day", year=2021, month=4, day=10,
                                          textvariable=self.selected_date)
        ...