Python Project — Decimal to Binary Number Converter

Rinu Gour
PythonFlood
Published in
4 min readSep 5, 2023

--

What are Decimal Numbers?

In the decimal number system, the numbers are represented with base 10. It is also called the base-10 number system which consists of 10 digits, such as 0,1,2,3,4,5,6,7,8,9.

What are Binary Numbers?

In our computer systems, the Binary Number System is used to manipulate and store all kinds of data, including numbers, words, videos, graphics and music. Binary numbers contain only two distinct digits,0(Zero) and 1(One).

Python Decimal to Binary Number Converter Project

About Python Decimal to Binary Number Converter Project

Converting Decimal numbers into Binary Numbers requires a basic knowledge of Python. But in this project we will make a Python program using the tkinter module to build the interface of application to convert Decimal Number to Binary Number and Vice-versa.

Prerequisites For Python Decimal to Binary Number Converter Project

First, we will install the required library in our system using a pip installer.

pip install tkinter

tkinter-This library will help us in creating a GUI window for our program.

Download Python Decimal to Binary Number Converter Project

Please download the source code of Python Decimal to Binary Number Converter Project: Python Decimal to Binary Number Converter Project Code.

We start with importing library as:

Importing the tkinter library and messagebox widget:

from tkinter import *
from tkinter import messagebox

tkinter-This library will help us in creating a GUI window for our program.
messagebox-MessageBox widget is used to display the messages in the Python applications.

Now we have to initialise GUI window:

myroot=Tk()
myroot.geometry("350x200")
myroot.title("Decimal to Binary Convertor")
myroot.config(bg="#97FFFF")

myroot-It is the name of our GUI window.
Tk()-Initialised tkinter which means GUI window created.
geometry()-This method provides the length and breadth to the GUI window.
title()-This method gives title to the window
confg()-This method set the configuration of the window.

lb1=Label(myroot,text="Decimal",font=('Arial,15,bold'),bg="#97FFFF")
lb1.grid(row=0,column=0)
lb2=Label(myroot,text="Binary",font=('Arial,15,bold'),bg="#97FFFF")
lb2.grid(row=1,column=0)

Label()- It is used to display one line or more than one line of text.
text-It is used to display text on label.
font-It is a style in which font is written.
bg-It is the background Colour of label.
grid()-It is used to set the position by defining row and column.

dec1=IntVar()
txt1=Entry(myroot,textvariable=dec1)
txt1.grid(row=0,column=1)
bin1=StringVar()
txt2=Entry(myroot,textvariable=bin1)
txt2.grid(row=1,column=1)

dec1=It is an integer type variable that stores the data where we can set an Integer value and can retrieve it.
bin1-It is a variable that holds a string data where we can set text value and can retrieve it.
Entry-It is used to create an input field and to display a single line of text.
txtvariable-It is used to retrieve the current text from entry widget variable.

btn=Button(myroot,text="Convert into\nBinary",font=('Arial,15,bold'),command=dectobin,relief=FLAT,bg="#97FFFF")
btn.grid(row=0,column=3)


btn=Button(myroot,text="Convert into\nDecimal",font=('Arial,15,bold'),command=bintodec,relief=FLAT,bg="#97FFFF")
btn.grid(row=2,column=3)

Button()-It is a button used to display on our window.
command-It is used as a function of button when it is clicked.

myroot.mainloop()

myroot.mainloop()-It is simply a method in the main window that executes what we wish to execute in an application.

Function to Convert Decimal to Binary:

In this we are using python built-in function to convert Decimal Number into Binary Number and replacing the “0b” with blank space-

def dectobin():
if dec1.get()==0:
messagebox.showerror(message="Enter a Number")
else:
bin1.set(str(bin(dec1.get()).replace("0b","")))

Function to Convert Binary to Decimal:

In this, we are using python int() function to convert Binary Number to Decimal Number-

def bintodec():
bin2=bin1.get()
try:
dec2=int(bin2,2)
dec1.set(dec2)
except:
messagebox.showerror(message="Enter a Number")

Python Decimal to Binary Number Converter Project Output:

decimal to binary converter output
python decimal number to binary number converter output

Conclusion

We have successfully created an application that converts Decimal Number to Binary Number and Vice-versa. There are various methods available for conversion like built-in method of python, recursion,int() etc. We have used two different methods for conversion; First is Built-in method of python to convert Decimal to Binary, Second is int() method to convert Binary to Decimal. The interface of application is created using the tkinter module. You can change the interface according to your need using this module.

--

--