Leap Year Checker GUI Application made with Python (Tkinter Module).

in #python3 years ago

I am a newbie on programming world and I am trying to learn Python as it is the easiest and very strong programming language. I've learnt all basic theories and the programming syntax and then I have solved some problems from urionlinejudge. Now I'am trying to learn GUI Appliations and I am trying to learn Tkinter module of Python.

Previously I posted a Calculator GUI application that I made with python and tkinter module of python. I made another GUI which is a Leap Year Checker.

In this GUI Application, I keep the background color light blue. There are a frame which is light blue in color, two text boxes and a button. After putting the year into the first text box, you need to press the button. The button has a command leap_func. After pressing the button, the function will work and you will find the year is leap year or not in the second textbox.

Let's jump to the codes.

Importing, Naming and Sizing

image.png

import tkinter as tk

win = tk.Tk()
win.title('Leap Year Checker')
win.geometry('400x400+400+100')
win.resizable(0,0)


Function/s

image.png

def leap_func():
    text_entry_number = int(text_entry.get(1.0, 'end'))
    length = len(str(text_entry_number))
    try:
        if length>=4 and str(text_entry_number)[-1] == '0' and str(text_entry_number)[-2] == '0':
            if text_entry_number % 400 == 0:
                text_result.delete(1.0, 'end')
                text_result.insert(1.0, f'{text_entry_number} is a leap year with 366 days!')

            else:
                text_result.delete(1.0, 'end')
                text_result.insert(1.0, f'{text_entry_number} is not a leap year!')
        elif text_entry_number % 4 == 0:
            text_result.delete(1.0, 'end')
            text_result.insert(1.0, f'{text_entry_number} is a leap year with 366 days!')
        else:
            text_result.delete(1.0, 'end')
            text_result.insert(1.0, f'{text_entry_number} is not a leap year!')
    except Exception as e:
        text_result.insert(1.0, e)


Frame, Button and Textarea

frame_top = tk.Frame(win, height=400, width=400, bg='light blue')
frame_top.pack(expand=True, fill=tk.BOTH)
text_entry = tk.Text(frame_top, width=20, height=1, bd=4, font='arial 10 bold')
text_entry.place(x=90, y=40)

check_btn = tk.Button(frame_top, text='Check', width = 10, bd=4, bg='white', font='arial 10 bold', command=leap_func)
check_btn.place(x=250, y=38)

text_result = tk.Text(frame_top, width=44, height=12, wrap='word')
text_result.place(x=20, y=85)


win.mainloop()


So the full code is :-

import tkinter as tk

win = tk.Tk()
win.title('Leap Year Checker')
win.geometry('400x400+400+100')
win.resizable(0,0)

def leap_func():
    text_entry_number = int(text_entry.get(1.0, 'end'))
    length = len(str(text_entry_number))
    try:
        if length>=4 and str(text_entry_number)[-1] == '0' and str(text_entry_number)[-2] == '0':
            if text_entry_number % 400 == 0:
                text_result.delete(1.0, 'end')
                text_result.insert(1.0, f'{text_entry_number} is a leap year with 366 days!')

            else:
                text_result.delete(1.0, 'end')
                text_result.insert(1.0, f'{text_entry_number} is not a leap year!')
        elif text_entry_number % 4 == 0:
            text_result.delete(1.0, 'end')
            text_result.insert(1.0, f'{text_entry_number} is a leap year with 366 days!')
        else:
            text_result.delete(1.0, 'end')
            text_result.insert(1.0, f'{text_entry_number} is not a leap year!')
    except Exception as e:
        text_result.insert(1.0, e)

frame_top = tk.Frame(win, height=400, width=400, bg='light blue')
frame_top.pack(expand=True, fill=tk.BOTH)
text_entry = tk.Text(frame_top, width=20, height=1, bd=4, font='arial 10 bold')
text_entry.place(x=90, y=40)

check_btn = tk.Button(frame_top, text='Check', width = 10, bd=4, bg='white', font='arial 10 bold', command=leap_func)
check_btn.place(x=250, y=38)

text_result = tk.Text(frame_top, width=44, height=12, wrap='word')
text_result.place(x=20, y=85)


win.mainloop()


Output





Thank You