Sie sind auf Seite 1von 3

LABORATORY #2

IT System Security Lab


University of Petroleum and Energy Studies
Lab : Definition, loop, branches and GUI programming in python
Date (Assign) : Practice Lab Date : 28 January to 01 January
Last Date (Checking) : Practice Lab Last Date (Late Checking) :
Date of Solution to be posted : Total Time : 2 hrs.
Week : First Lab (A or B) :
_________________________________________________________________________________________________
INSTRUCTIONS:
1. Unprofessional conduct, such as an abuse of USF computer privileges (unauthorized access), or a Violation of academic integrity
(plagiarism or fraud), will result in the student receiving a failing grade.
2. Work in singles.
3. Late submission will be evaluated from half of total marks.
4. After the date, when solutions will be posted, Zero marks will be assigned
5. Platform:
Python Either on window platform or Linux platform
____________________________________________________________________________________________________________________

1. Definitions:
Use of definition in python is quite comparable to use of functions in C or C++ language. For example, if we start writing a hello
program using definitions then code will look like:
>>> def hello():
print 'Hello'
>>> hello()
# Program to print random numbers
>>> import random
>>> def random_number():

for i in range(3):
print random.random()
>>> random_number()
2. Branches and Loopss
If, if –else and if- elif are the braches that you can use in python. for and while loops can be used in python.
>>> x=5
>>> if x<5:
print 'less than five'
elif x>5:
print 'greater than five'
else:
print 'both are equal'
# Program to print greatest of three numbers
>>> x=4
>>> y=5
>>> z=6
>>> if x>y & x>z:
print 'x is greatest'
elif y>x & y>z:
print 'y is greatest'
else:
print 'z is greatest'
# Some examples of for and while loops
>>> for i in range(3):
print i
>>> for i in range(0,3,2):
print i
>>> for i in range(-4,3,2):
print i
>>> x=['hello','yello','mello']
>>> for data in x:
print data
>>> x=1
>>> y=6
>>> while y>x:
print y
x=x+1
>>> num=5
>>> first=0
>>> second=1
>>> while num>0:
third=first+second
print third
first=second
second=third

num=num-1

Let us start with graphical user interface in python. This is practice exercise. So you people will try to execute code written here and will make
modification in existing code to design new graphical patterns. There are two ways to execute programs: one you can use command prompt and
second you can save code in a text file with extension ‘.py’. For execution, wither you can use ‘F5’ key or on command prompt got to python
folder and type ‘python program_name.py’. Let us start now. First, we will try on command prompt and then using file method.
Question #1: Suppose you want to write a program for following output:

Solution:

>>> from Tkinter import *


>>> root=Tk()
>>> labelfont=('times',20,'bold')
>>> widget=Label(root,text='')
>>> root.mainloop()

Question#2: Write a program to execute three popup windows with text and buttons and look like as followos:

Solution:
Copy the following code in a text file and press F5 or execute it from command prompt.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Gui1.py
from Tkinter import *
import sys

win1=Toplevel()
win2=Toplevel()

Button(win1,text='Spam',command=sys.exit).pack()
Button(win2,text='Spam',command=sys.exit).pack()

Label(text='Popups').pack()
win1.mainloop()

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Question #3: Write a graphical user interface program to execute following frame format:

Question #4: Write a graphical user interface program to execute following frame format:

i.e. when you click with of two button on frame. It will print a message on python command prompt screen.

Help:
b = tkinter.Button(toolbar, text="open", width=6, command=callback)
b.pack(side=tkinter.LEFT, padx=2, pady=2)

__________________________________

Das könnte Ihnen auch gefallen