Python Advance Workbook OWTP
Python Advance Workbook OWTP
Python Advance Workbook OWTP
Python (Advance)
www.azureskynet.com | +91-1244275997
1. GUI based login Panel
In this chapter, we will go through the concept of login and signup in a login
panel using GUI (Graphical user interface) and sending the email with
attachment to any mail id. For sending an email using this, first you must have a
mail id on Gmail because here we are going to use the Gmail’s protocol to send
the email. So here is the small app that we are going to create on this chapter.
#import the rest of the modules to allow the smtp server to send or
receive the attachment with the email
count, c=0,0
In the above main page we used 3 different widgets, i.e., Label, Entry and
Button. In the creating of this page we will first create a window manager in
which we will add the above widgets and further we will add the title to the
window and also add the icon to the window to make your window more
attractive. Now look on the code below to understand the code.
global nameEL
global pwordEL
global rootA
rootA=Tk()
#giving a title to your window
rootA.title('Login')
rootA.iconbitmap(r'20170115_144259.ico')
intruction=Label(rootA,text='Please Login\n')
nameL=Label(rootA,text='Username: ')
pwordL=Label(rootA,text='Password: ')
nameL.grid(row=1,sticky=W)
pwordL.grid(row=2,sticky=W)
#creating the entries to let the user enter the username and
password; also to setting up their positions using the grid
nameEL=Entry(rootA)
pwordEL=Entry(rootA,show='*')
nameEL.grid(row=1,column=1)
pwordEL.grid(row=2,column=1)
#creating the buttons to login, signup and delete user, also
setup their properties with the labels on them
loginB=Button(rootA,text='Login',relief=GROOVE,
command=CheckLogin)
loginB.grid(columnspan=2,sticky=W)
loginB=Button(rootA,text='Signup',relief=GROOVE,
command=Signup)
loginB.grid(columnspan=2,sticky=W)
rmuser.grid(column=2,sticky=W)
rootA.mainloop()
Now you can see in case of each button widget we have to pass a command and
that command is a function so we have to create functions for login, signup and
delete user as CheckLogin, Signup and DelUser respectively. Now let’s see what
we are gonna do in each function and how it will work when the button will be
pressed. Firstly let’s see what will happen when you will click the login button so
let’s start creating out first function, i.e. , CheckLogin
with open(creds) as f:
data=csv.reader(f) #reading credentials in csv format
try:
uname=line[0]
pword=line[1]
rootA.destroy()
r=Tk()
r.title(':D')
r.iconbitmap(r'20170115_144259.ico')
r.geometry('150x50')
rlbl.pack()
r.mainloop()
send()
pass
else:
r=Tk()
r.title(':D')
r.iconbitmap(r'20170115_144259.ico')
r.geometry('150x50')
rlbl.pack()
r.mainloop()
login()
As you can see in the above code, for the case when credentials got matched
window with message “Login successful” is called up and at the end send()
function is called. So our next step is to create the window to set up two
buttons one to compose a new email and other one to logout from your profile
and the window created by the function send() will look like.
Creating your profile window
Now to create you window for send() function let’s create a function using the
tkinter (for GUI).
global ro
ro=Tk()
ro.title('Send Message')
ro.iconbitmap(r'20170115_144259.ico')
new.grid(row=1,column=0,sticky=N)
new.grid(row=1,column=2,sticky=N)
ro.mainloop()
file=Label(root,text=root.filename)
file.grid(row=7,column=0,sticky=W)
pass
global count
global c
filename = root.filename
attachment=open(filename,'rb')
part=MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment;
filename="+filename)
msg.attach(part)
else:
pass
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("[email protected]","Abc@123")
try:
mail.sendmail("[email protected]",msg['To'],text)
print('email sent')
except:
mail.quit()
root.destroy()
if c >= 1:
r=Tk()
r.title(':D')
r.iconbitmap(r'20170115_144259.ico')
r.geometry('150x50')
rlbl.pack()
r.mainloop()
else:
r=Tk()
r.title(':D')
r.iconbitmap(r'20170115_144259.ico')
r.geometry('150x50')
rlbl.pack()
r.mainloop()
root.title('New Message')
root.iconbitmap(r'20170115_144259.ico')
To=Label(root,text="To:")
Subject=Label(root,text="Subject:")
text=Label(root,text="Message:")
To.grid(row=1,column=0,sticky=W)
Subject.grid(row=2,column=0,sticky=W)
text.grid(row=3,column=0,sticky=NW)
Toe=Entry(root,width=67)
Subjecte=Entry(root,width=67)
texte=Text(root,width=50,height=5)
Toe.grid(row=1,column=1,sticky=W)
Subjecte.grid(row=2,column=1,sticky=W)
texte.grid(row=3,column=1,sticky=W)
attach=Button(root,text="Attach",relief=GROOVE,
command=attach)
attach.grid(row=6,column=1,sticky=W)
send=Button(root,text="Send",relief=GROOVE,command=Sendmail)
send.grid(row=6,column=0,sticky=W)
file=Label(root,text='')
file.grid(row=7,column=0,sticky=W)
root.geometry('500x170')
root.mainloop()
ro.destroy()
r=Tk()
r.title(':D')
r.iconbitmap(r'20170115_144259.ico')
r.geometry('150x100')
rlbl.pack()
r.mainloop()
login()
The following window will be displayed when you will click on the logout
button.
Signing up for new user
Till now we have discussed and understood all about the login function and also
we have discussed about all the functionalities of the further actions that will
takes places in between this process. Now let’s move to our next function which
will be called when you will click on the signup button. So here is how you will
create a function to signup and the following window will be opened after the
button clicked.
global nameE
global roots
rootA.destroy()
roots.title("Signup")
roots.iconbitmap(r'20170115_144259.ico')
#this puts a label so just a piece of text saying 'please enter ...'
intruction.grid(row=0,column=0,sticky=E)
nameL.grid(row=1,column=0,sticky=W)
pwordL.grid(row=2,column=0,sticky=W)
password box :D
nameE.grid(row=1,column=1)
pwordE.grid(row=2,column=1)
signupButton=Button(roots,text='Signup',relief=GROOVE,
command=FSSignup)
signupButton.grid(columnspan=2,sticky=W)
roots.mainloop()
def FSSignup():
text
directory it is stored in
rootA.destroy()
new credentials
Condition check and Login/Signup
Now when you have done creating all the functions to validate or to create a
user or to delete the credentials. Now the final step is to validate the
credentials and accordingly call its first window when the program runs.
login()
else:
import curses,sys
Open command prompt and type the following command and press enter.
Before running this command please makes sure that you have a working
internet connection on your system.
sys.__stdout__ = sys.stdout
curses.initscr()
Activation of keypad
So when you have initialized a screen and set up the boundaries for your play
zone. Now it’s time to activate the keypad so as to control the direction of
your snake and let the snake eat his food and accordingly change his length.
So for that we will use the win object that we have created during the
creation of the screen.
curses.noecho()
win.border(0)
win.nodelay(1)
Initialization Parameters
So we have set up all the parameters to create a play zone and to display the
boundaries. Now let’s create new variables and initialize them with a value. So
create four variables first one to set up the initial direction to move your
snake, second one to initialize the value of your score, third one to set the
length and coordinates of your snake and the last one to set the initial
position for the food for your snake. Here we will use the addch method from
the win object to print the food on its coordinate and we’ll specify the
character that will be displayed in place of the food.
score = 0
win.border(0)
#this will print the score and the sting SNAKE on the top
event = win.getch()
key = -1
key = win.getch()
key = prevKey
continue
key = prevKey
if snake[0][0] == 0: snake[0][0] = 18
if snake[0][1] == 0: snake[0][1] = 58
# Exit if snake crosses the boundaries (use this when you want
to limit the play zone)
food = []
score += 1
else:
length decreases
curses.endwin()
input('')
So this was the way how you can create your own snake game and also you have
got the good hands on experience of working with the curses module. Similarly,
you can use curses in different way to create your own game to play and share
with your friends. I hope you all will do well and will create a more advance
games like this.