In this code packet, I will show you how to create a real-time chat application using python.
CHAT APPLICATION USING PYTHON
In this code packet, We will learn how to develop a chat application using Python.
Few packages are we using in our program that is:
1. socket package
2. Tkinter package
3. time package
4. threading package
Aim of this Code Packet-
We are going to create a chat between two systems here. After the event of a button triggered, the chat will be started between two systems. For this, we are going to create an attractive GUI first.
Installing Package which is required for our project-
pip install socket
first, we have to import such packages. This is the important step of Python programming.
import socket import time import threading from Tkinter import *
socket - socket is used to create an object and create a connection between two users.
time - time managing(waiting for code execution )
threading - To create a single thread in which we are managing the two jobs at the same time.
Tkinter - This library is used to create Frontend (GUI).
root = Tk() root.geometry('300x500') root.config(bg="white")
I am initializing the Tkinter window here, by creating Tk() class and the variable name is root. After initializing the window we specify the geometry of GUI. Then we configure that we want a white background for our GUI window.
We have some widgets in our code.
1. start chat button -
startchatimage=PhotoImage(file='C:\codespeedy\start.png') buttons=Button(root,image=startchatimage,command=func,borderwidth=0) buttons.place(x=90,y=10)
This button will be used to connect two users or two systems for chat.
We are declaring one variable named startchatimage, this variable holds the image which is passed in Photoimage().
The parameters of the Button() class are -
Tkinter class instance
And then we placed the button using place() class with the parameters x and y which are coordinators where you place the button on a window.
2. Message Box -
message=StringVar() messagebox=Entry(root,textvariable=message,font=('calibre',10,'normal'),border=2,width=32) messagebox.place(x=10,y=444)
This is the widget where all the messages will appear from both the users by creating the Entry widget.
parameters of the Entry widget -
Tkinter class instance
we place the message box at the respective coordinates of x and y.
3. send Message Button -
sendmessageimg=PhotoImage(file=r"C:\codespeedy\send.png") sendmessagebutton=Button(root, image=sendmessageimg,command=threadsendmsg, borderwidth=0) sendmessagebutton.place(x=260,y=440)
This button is pressed when the user wants to send a message.
parameters are the same as the start button.
After execution of this code, message will pop up on a Listbox from the two systems.
Tkinter class instance
After placing all the widgets we will close the mainloop.
So, here we are done with our frontend. let's see the backend process.
def func(): t.threading.Thread(target=recv) t.start()
We create one button named live chat. We saw their code in the above section. When we press the live chat button the func() function is called automatically. In the func() function we are creating a thread by the Thread method.
def recv(): listensocket=socket.socket() port=3050 mac=99 ip=socket.gethostname() print(ip) listensocket.bind(('',port)) listensocket.listen(mac) (clientsocket,address)=listensocket.accept() while True: sendmessage=clientsocket.recv(1024).decode() if not sendmessage=="": time.sleep(5) lstbx.insert(0,"client",+sendmessage)
We declare one listensocket variable in which we assign the instance of the socket, we are declaring another variable in which we store the port number of the user-system, mac max number of connection, in ip variable, With whom we want to connect that system internet protocol address we will assign to the ip variable.
By using bind, We bind another user system to our system with the help of an assigned port number.
def threadsendmsg(): th=threading.Thread(target=sendmsg()) th.start()
We create one button sendmsgbutton. We saw their code in the above section. When we press that button the threading() function is called automatically. In the threading() function we are creating a thread by the Thread method.
def sms(): global m if m==0: m=socket.socket() hostname="DESKTOP-KB5008876" port=4050 m.connect((hostname,port)) msg=messagebox.get() lstbx.insert(0,"You :",+msg) m.send(msg.encode())
At the start, we declare one variable m and initialize it with 0.
In the sms() function we declare that the m variable is global and then we are check that, is that m variable has containing 0 value or not. If yes then if part will be executed.
Here, we are done with our backend.
Submitted by Anita Suresh Lawate (Anitacodespeedy)
Download packets of source code on Coders Packet