0% found this document useful (0 votes)
3 views4 pages

Python m3 Bit

The document discusses various programming concepts including drawing shapes with Turtle graphics, image processing techniques such as blurring and converting images to grayscale, and the differences between terminal-based and GUI-based programs. It also covers event-driven programming, object-oriented programming methods, and provides examples of GUI applications for temperature conversion and calculating the distance traveled by a bouncing ball. Additionally, it includes a Python program to determine the quadrant of a point based on its coordinates.

Uploaded by

alvinjohnmathew9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python m3 Bit

The document discusses various programming concepts including drawing shapes with Turtle graphics, image processing techniques such as blurring and converting images to grayscale, and the differences between terminal-based and GUI-based programs. It also covers event-driven programming, object-oriented programming methods, and provides examples of GUI applications for temperature conversion and calculating the distance traveled by a bouncing ball. Additionally, it includes a Python program to determine the quadrant of a point based on its coordinates.

Uploaded by

alvinjohnmathew9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM TO DRAW HEXAGON USING Blurring an Image

TURTLE # import the turtle modules ➔This algorithm resets each pixel’s color to the
import turtle average of the colors of the four pixels that
# Start a work Screen surround it.➔The function blur expects an
ws = [Link]() image as an argument and returnsa copy of that
# Define a Turtle Instance image with blurring.➔The function blur begins
Turtle = [Link]() its traversal of grid with position (1, 1) and ends
# executing loop 6 times for 6 sides with position (width, height).
for i in range(6): ➔means that the algorithm does not transform
# Move forward by 90 units the pixels on the image’s outer edges.
[Link](90) EVENT DRIVEN PROGRAMMING the flow of a
# Turn left the turtle by 300 degrees program depends upon the events, and
[Link](300)
programming which focuses on events is called
image processing function in Python
Event-Driven programming. We were only
>Converting an Image to Black and White
dealing with either parallel or sequential
models, but now we will discuss the
asynchronous model. The programming model
following the concept of Event-Driven
programming is called t Asynchronous model.
Working of Event-Driven programming depends
upon events happening in a program. Other
than this, it depends upon the program’s event
loops that always listen to a new incoming
event in the program. Once an event loop starts
in program, then only events will decide what
>Converting an Image to Grayscale will execute and in which order.
Black-and-white photographs are not really A Loop Pattern for Traversing a Grid:/
just black and white; they also contain various Row-major traversal in a two-dimensional grid.
shades of gray known as grayscale uses a nested loop structure to traverse a two-
. dimensional grid of pixels. Each data value in
grid is accessed with a pair of coordinates using
the form (<column>, <row>)

Copying an Image The Image class includes a


clone method, Method clone builds and
returns a new image with same attributes as
original one, but with an empty string as
filename.
TERMINAL BASED PROGRAMS : The terminal- The Structure of Any GUI Program
based program prompts the user for user from breezypythongui import EasyFrame
inputs and other program dependant values. class <class name>(EasyFrame) :
•After the user enters his inputs, the program def __init__(self):
responds by computing and displaying the EasyFrame._init_(self <optional args>)
results. Program then terminates execution. <code to set up widgets>
Terminal-based user interface : has several <code to define event-handling methods>
obvious effects on its users:•The user is # Instantiates and pops up the window.
constrained to reply to a definite sequence of If __ name__==”__ main":
prompts for inputs.•Once an input is entered, <class name>().mainloop()
there is no way to back up and change it.
•To obtain results for a different set of input
data, the user must run the program again and
all of the inputs must be re-entered.
These problems for users can be solved by
converting the interface to a GUI.
The GUI-Based PROGRAM
→ The GUI-based version of the program
displays a window that contains various
components, also called widgets (eg: Button, Turtle Operations
Textbox..) →A GUI program is event driven,
that it is inactive until the user clicks a button
or selects a menu option.→ A title bar at the
top of the window. •This bar contains the title
of the program, “Tax Calculator.” • Three
colored disks. Each disk is a command
button.•The user can use the mouse to click
the left disk to quit the program • the middle
disk to minimize the window, or the right disk
to zoom the window.• The user can also move
the window around the screen by holding left turtle attributes
mouse button on title bar and dragging mouse.
Accessor Method: This method is used to
access the state of the object i.e, the data
hidden in the object can be accessed from this
method. However, this method cannot change
the state of the object, it can only access the
data hidden. We can name these methods with
the word get.
Mutator Method: This method is used to
mutate/modify the state of an object i.e, it Drawing Two-Dimensional Shapes
alters the hidden value of the data variable. It
can set the value of a variable instantly to a
new value. This method is also called as update
method. Moreover, we can name these
methods with the word se
Qn. Write a GUI-based program that allows self._CelsValue = Entry(self, font = font, fg =
the user to convert temperature values "red", justify = "center", width = 13,
between degrees Fahrenheit and degrees textvariable = self._celsVar)
Celsius. The interface should have labeled self._CelsValue.grid(row = 1, column = 1)
entry fields for these two values. These # The command buttons
components should be arranged in a grid self._button = Button(self, font = font,
where the labels occupy the first row and the text = " >>>> ", command =
corresponding fields occupy the second row. self._FahrValueN)
At start-up, the Fahrenheit field should font = [Link](family = "Arial", size =
contain 32.0, and the Celsius field should 15)
contain 0.0. The third row in window contains self._button.grid(row = 2, column = 0,
two command buttons, labeled >>>> and columnspan = 1)
<<<<. When the user presses the first button, self._button = Button(self, font = font,
the program should use the data in the text = " <<<< ", command = self._CelsValue)
Fahrenheit field to compute Celsius value, font = [Link](family = "Arial", size =
which should be o/p to Celsius field. Second 15)
button should perform inverse function. self._button.grid(row = 2, column = 1,
from tkinter import * columnspan = 1)
import [Link] #### From here down is not to be indented
class _TemperatureConversion(Frame): #### Just indented to keep it in the window
def __init__(self): def _fahrenheitToCelsius(FahrValue):
#Sets up the window and widget Fahr = FahrValue
Frame.__init__(self) Cels = ((Fahr - 32) * 5) / 9 print(Cels)
[Link]("Temperature Conversion") def _celsiusToFahrenheit(CelsValue):
[Link](0, weight = 5) Cels = CelsValue Fahr = ((Cels * 9) / 5) +
[Link](0, weight = 5) 32
[Link]("300x300") print(Fahr)
[Link](0,0) def main():
[Link](rowspan = 1, columnspan = 1) _TemperatureConversion().mainloop()
# Calculates the Fahrenheit to Celsius FahrValue = float(input("Enter a Fahrenheit
conversion value to convert: "))
font = [Link](family = "Arial FConvert = fahrenheitToCelsius(FahrValue)
Black", size = 15) CelsValue = float(input("Enter a Celsius value
self._fahrLabel = Label(self, font = font, text to convert: "))
= " Fahrenheit ") CConvert = celsiusToFahrenheit(CelsValue)
self._fahrLabel.grid(row = 0, column = 0)
self._fahrVar = DoubleVar() Object Instantiation and the turtle Module
font = [Link](family="Arial", size=13) ➔ Before use a Turtle object, must create them.
self._FahrValue = Entry(self, font = font, fg = ➔ That is create an instance of the object’s
"blue", justify = "center", width = 13, class.
textvariable = self._fahrVar, text = "32.0",) ➔ The process of creating an object is called
self._FahrValue.grid(row = 1, column = 0)
➔ instantiation .
# Calculates the Celsius to Fahrenheit from turtle import Turtle
conversion
t = Turtle()
font = [Link](family = "Arial
➔ A window is created with the turtle’s icon is
Black", size = 15)
located at the home
self._celsLabel = Label(self, font = font, text =
position (0, 0) in the center of the window,
" Celsius ")
facing east and ready to
self._celsLabel.grid(row = 0, column = 1)
draw.
self._celsVar = DoubleVar()
font= [Link](family = "Arial",size=13) ➔ The user can resize the window in the usual
manner.
Write a Python program to find the quadrant Qn. A bouncy program is defined as follows –
of a point, say (x,y). The program computes and displays total
# for initialization of coordinates distance traveled by a ball, given 3 inputs—the
X, y = map(int, list(input(“Insert the value for initial height from which it is dropped, its
variable X and Y : “).split(“ “))) bounciness index, and number of bounces.
# find true condition of first quadrant Given the inputs write a GUI-based program to
If x > 0 and y > 0: compute total distance traveled.
Print(“point (“, x, “,”, y, “) lies in the First from tkinter import *
quadrant”) master = Tk()
# find second quadrant [Link]("Bouncy")
Elif x < 0 and y > 0: def BouncyCalc():
Print(“point (“, x, “,”, y, “) lies in the Second z = float(b_index.get()) **
quadrant”) (float(num_bounce.get()) + 1)
# To find third quadrant return float(height_e.get()) * (1 + (2 * (z /
Elif x < 0 and y < 0: (float(b_index.get()) - 1))))
Print(“point (“, x, “,”, y, “) lies in the Third Label(master, text="Initial Height").grid(row=0)
quadrant”) Label(master, text = "Bounciness
# To find Fourth quadrant Index").grid(row = 1)
Elif x > 0 and y < 0: Label(master, text = "Number of
Print(“point (“, x, “,”, y, “) lies in the Fourth Bounces").grid(row = 2)
quadrant”) height_e = Entry(master)
# To find does not lie on origin b_index = Entry(master)
Elif x == 0 and y == 0: num_bounce = Entry(master)
Print(“point (“, x, “,”, y, “) lies at the origin”) height_e.insert(10,"0.0")
# On x-axis b_index.insert(10,"0.0")
Elif y == 0 and x != 0: num_bounce.insert(10, "0")
Print(“point (“, x, “,”, y, “) on x-axis”) height_e.grid(row = 0, column = 1)
# On y-axis b_index.grid(row = 1, column = 1)
Elif x == 0 and y != 0: num_bounce.grid(row = 2, column = 1)
Print(“point (“, x, “,”, y, “) on at y-axis”) calc_button = Button(master, text = "Calculate",
command = BouncyCalc())
calc_button.grid(row = 3, column = 1)
mainloop()

You might also like