Python and Tkinter mini-project (OddEven)

Python and Tkinter mini-project (OddEven)

Python and Tkinter project for beginners

Python project for a beginner

gui development slide 0.png

Today we will create a simple mini project using Python and Tkinter. Seeing your output in the terminal is boring, so we will create a GUI window where we display our output.

Let's create a GUI window first and import the Tkinter library.

from tkinter import *

root = Tk()
root.title('Tkinter GUI - Sailendra')
root.geometry('436x400')
root.configure(background="powder blue") 

#Body of the program

root.mainloop()

Now create four frames for heading, body widgets, button, and output. We are creating this because we don't want to put all widgets in the same root window, later it will difficult for us to manage.

Creating four Frames

# Frames to seperate the content
heading_frame = Frame(root, borderwidth=1, relief=SUNKEN, padx=10, pady=5, bg="powder blue")
heading_frame.grid(row=0, column=0, ipadx=50, pady=8, padx=10) 

content_frame = Frame(root, borderwidth=1, relief=SUNKEN, padx=10, pady=10, bg="powder blue")
content_frame.grid(row=1, column=0, pady=20)

botton_frame = Frame(root, borderwidth=1, relief=SUNKEN, padx=10, pady=10, bg="powder blue")
botton_frame.grid(row=2, column=0, pady=20) 

output_frame = Frame(root, borderwidth=1, relief=SUNKEN, padx=10, bg="powder blue")
output_frame.grid(row=3, column=0, pady=20)

Create a Label for heading

#Create a label for heading
heading_label = Label(heading_frame, text="Odd or Even", font=("times new roman", "26", "bold"), bg="powder blue")
heading_label.grid(padx=(100, 0))

Create a label for the entry widget

#Create a label for entry widget
entry_label = Label(content_frame, text="Enter Number: ", font="Courier 20", bg="powder blue")
entry_label.grid(row=1, column=0)

Create Input box to enter the number

#Create Input box to enter number
entry_number = Entry(content_frame, font="Courier 20", width=10)
entry_number.grid(row=1, column=1)

Create display button

#Create display button
display_button = Button(botton_frame, text="Check", command=check_number, font="Courier 20 bold", bg='powder blue')
display_button.grid(row=0, column=0, pady=10)

It's time to create a function to perform some operations and display the result/output. write down this function after root.configure()

Create a function to perform operations and display value

display_number = Label(root)

# Create a function to peform operations and display value
def check_number():
    user_input = int(entry_number.get())

    global display_number
    display_number.destroy()

    if (user_input %2 == 0):
        display_number = Label(output_frame, text= str(user_input) + " is Even Number.\n", font="Courier 15", bg="powder blue")
        display_number.grid(row=1, column=0, pady=(25, 0))

    else:
        display_number = Label(output_frame, text=str(user_input) + " is Odd Number.\n", font="Courier 15", bg="powder blue")
        display_number.grid(row=2, column=0, pady=(25, 0))

We have successfully created this project. Now go for the next projects.

Happy Coding :-)

If you found any mistakes or logical/syntax error then please let me know, or you can reach out to me at TWITTER

sailendra signature1.png