Module 4 Python Integration Primer
Module 4 Python Integration Primer
root=Tk()
root.title("My window")
root.geometry("400x300")
root.wm_iconbitmap('image.ico')
root.mainloop()
root=Tk()
id=c.create_line(50,50,200,50,200,150,width=4, fill="white")
c.pack()
root.mainloop()
root=Tk()
root.title("My frame")
f.pack()
root.mainloop()
Widgets
Create a push button and bind it with an event handler Pg.588 Pb. 9
from tkinter import *
def buttonClick(self):
root=Tk()
f.propagate(0)
f.pack()
b.pack()
b.bind("<Button-1>", buttonClick)
root.mainloop()
Create a push button and bind it with an event handler using command option Pg.589 Pb.
10
from tkinter import *
class MyButton:
def __init__(self,root):
self.f.propagate(0)
self.f.pack()
self.b.pack()
def buttonClick(self):
root=Tk()
mb=MyButton(root)
root.mainloop()
create 3 push buttons and change the background of the frame according to the button
clicked by the user pg 591 Pb. 13
from tkinter import *
class MyButton:
def __init__(self,root):
self.f.propagate(0)
self.f.pack()
self.b1.pack()
self.b2.pack()
self.b3.pack()
def buttonClick(self,num):
if num==1:
self.f["bg"]='red'
if num==2:
self.f["bg"]='green'
if num==3:
self.f["bg"]='blue'
root=Tk()
mb=MyButton(root)
root.mainloop()
class MyButton:
def __init__(self,root):
self.f.propagate(0)
self.f.pack()
self.b1.pack(side=LEFT, padx=10,pady=15)
self.b2.pack(side=LEFT, padx=10,pady=15)
self.b3.pack(side=RIGHT, padx=10,pady=15)
self.b4.pack(side=RIGHT, padx=10,pady=15)
def buttonClick(self,num):
if num==1:
self.f["bg"]='red'
if num==2:
self.f["bg"]='green'
if num==3:
self.f["bg"]='blue'
if num==4:
self.f["bg"]='white'
root=Tk()
mb=MyButton(root)
root.mainloop()
Label in Python
from tkinter import *
root = Tk()
root.geometry('250x150')
label_frame.pack(expand='yes', fill='both')
label1.place(x=0, y=5)
label2.place(x=0, y=35)
label3.place(x=0, y=65)
root.mainloop()
Textbox in Python
from tkinter import *
ws = Tk()
ws.title('Welcome')
ws.geometry('400x300')
ws.config(bg='blue')
text_box.pack(expand=True)
text_box.insert('end', message)
text_box.config(state='disabled')
ws.mainloop()
master = Tk()
master.geometry("175x175")
v = StringVar(master, "1")
mainloop()
Checkbutton
from tkinter import *
root = Tk()
root.geometry("300x200")
w.pack()
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
variable = Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
variable = Checkbutton2,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
variable = Checkbutton3,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
Button1.pack()
Button2.pack()
Button3.pack()
mainloop()
Networking in Python
Interconnection of Computers is called as network
Sharing of resources
Server-client
Requirements: Hardware, Software, Protocol
TCP/IP: transmission Control Protocol/ Internet Protocol
Socket:
Logical connecting point between a server and client is called Socket.
Establishing connection between server and client through socket is called Socket
Programming
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
URL: Uniform Resource Locator represents the address that is specified to access some information
or resource on Internet
Sceheme=protocol
Port=port number
import urllib.parse
url='https://vcet.edu.in/computer-engineering/'
tpl=urllib.parse.urlparse(url)
print(tpl)
Path= /computer-engineering/
Parameters=
import urllib.request
file=urllib.request.urlopen("https://www.python.org/")
print(file.read())
import urllib.request
try:
file=urllib.request.urlopen("https://python.org/")
content=file.read()
except urllib.error.HTTPError:
exit()
f=open('myfile.html','wb')
f.write(content)
f.close()
import urllib.request
url = "https://www.iitb.ac.in/sites/www.iitb.ac.in/files/styles/gallery_item/public/
GoldenOriole.jpeg"
urllib.request.urlretrieve(url, "myimage.jpeg")
img = Image.open(r"myimage.jpeg")
img.show()
Server1.py
import socket
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.bind(('', port))
# allow maximum 1 connection to
# the socket
s.listen(1)
# connection
c, addr = s.accept()
msg = "Bye!"
c.send(msg.encode())
c.close()
Client1.py
import socket
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
msg = s.recv(1024)
while msg:
print('Received:' + msg.decode())
msg = s.recv(1024)
s.close()
TCP/IP server reads a file name from the client and reads its content
Message.bin
with open('message.bin','wb') as f:
mes=mes.encode()
f.write(mes)
server1.txt
import socket
# take the server name and port name
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.bind(('', port))
# the socket
s.listen(1)
# connection
c, addr = s.accept()
fname=c.recv(1024)
fname=str(fname.decode())
f=open(fname, 'rb')
content=f.read()
c.send(content)
f.close()
except FileNotFoundError:
c.close()
client1
import socket
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
s.send(filename.encode())
content=s.recv(1024)
print(content.decode())
s.close()
s.close()
Send email
import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("sender_email_id", "sender_email_id_password")
# message to be sent
message = "Message_you_need_to_send"
s.quit()
Authentication Error
Database connectivity
DBMS:
Advantages:
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
+--------------------+
| Database |
+--------------------+
| employee_db |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
+-------+----------+------+-----+---------+-------+
+-------+----------+------+-----+---------+-------+
+-------+----------+------+-----+---------+-------+
Database changed
+-------+----------+------+-----+---------+-------+
+-------+----------+------+-----+---------+-------+
| eno | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
+------+-------+------+
+------+-------+------+
| 1 | ABC | 1000 |
| 2 | XYA | 2000 |
+------+-------+------+
+------+-------+------+
| 1 | ABC | 1000 |
| 2 | XYA | 3000 |
| 3 | PQR | 3000 |
+------+-------+------+
+------+-------+------+
+------+-------+------+
| 2 | XYA | 4000 |
| 3 | PQR | 3000 |
+------+-------+------+
import mysql.connector
conn = mysql.connector.connect(
cursor = conn.cursor()
cursor.execute(str)
cursor.close()
conn.close()
import mysql.connector
conn = mysql.connector.connect(
cursor = conn.cursor()
try:
cursor.execute(str2)
cursor.execute(str3)
except:
rows=cursor.fetchall()
print(row)
cursor.close()
conn.close()
Python program to retrieve and display all rows from employee table pb 1 pg 666
import mysql.connector
conn = mysql.connector.connect(
cursor = conn.cursor()
row=cursor.fetchone()
print(row)
row=cursor.fetchone()
cursor.close()
conn.close()
import mysql.connector
conn = mysql.connector.connect(
cursor = conn.cursor()
rows=cursor.fetchall()
print(row)
cursor.close()
conn.close()
import mysql.connector
def update_rows(eno):
conn = mysql.connector.connect(
cursor = conn.cursor()
args=(eno)
try:
except:
finally:
rows=cursor.fetchall()
print(row)
cursor.close()
conn.close()
update_rows(x)
import mysql.connector
def delete_rows(eno):
conn = mysql.connector.connect(
cursor = conn.cursor()
args=(eno)
try:
except:
finally:
rows=cursor.fetchall()
print(row)
cursor.close()
conn.close()
delete_rows(x)
Introduction to DJANGO
Install Django
pip install django
Creating a Project
Lets’ check how to create a basic project using Django after you have
installed it in your pc.
To initiate a project of Django on Your PC, open Terminal
and Enter the following command
django-admin startproject projectName
A New Folder with name projectName will be created. To
enter in the project using terminal enter command
cd projectName
Python manage.py runserver
Now visit http://localhost:8000/ ,