Do you love programming? Which language do you love to code in? I love a lot, actually, I am quite fond of programming. My favourite language is Python. But, you know what simply programming the normal syntax code doesn’t make it interesting with time. Your programming should be interactive and we can make our codes interactive by using graphics. So, in the post, I, would love to bring to you the basics of Python Programming Graphics.
Today’s computer applications aren’t limited to the type of textual input and output that we’ve been using thus far . Most applications that you simply are conversant in probably have a Graphical interface (GUI). This gives visual elements like windows, icons, buttons and menus.
Interactive graphics programming generally become confusing. Even entire textbooks are also dedicate to the intricacies of graphics. Industrial-strength GUI applications are also usually developed employing a dedicated graphics programming framework. Python though comes with its own standard GUI module called Tkinter.
Even so, taking the time to find out Tkinter would detract from the more fundamental task of learning the principles of programming and style that are the main target of this book.
There is a ” graphics.py ” library in Python created by John Zelle. This library is open source and is available as a free Python module file. This graphics library, also makes it easier to write down simple graphics programs.
Python Graphics Programming
To use the methods in graphics.py one has to import it. In every Python Graphics program you create. Also, Graphics.py isn’t a component standard Python distribution. It gets save as graphics.py within the folder. Where you’ll save your Python Graphics programs.
Generally, any Python Graphics program requires one to do these four tasks namely :
- Importing the ” graphics.py ” library.
- Then, creating a GraphWin Object
- Hereafter, paint the output of graphics functions on the declared window object
- Finally, closing the opened GraphWin Object
Graphics Programming in Python
from graphics import *
The next thing is to make a window area where the output of Python Graphics program are going to display.
Window_object= GraphWin(text-title, width, height) display = GraphWin(' Circle' of Rainbow, 200, 200)
cir= Circle(Point(200, 200, 10) cir.draw(display)
This code will draw a circle of radius 10 pixels at center 200, 200
Now, we need to close the GraphWin Object
# Celsius to Fahrenheit conversion using " graphics.py " library ..... from graphics import * def main(): wins = GraphWin("Celsius Converter", 350, 250) wins.setCoords(0.0, 1.0, 3.0, 4.0) #GUI Text(Point(1,3), "Temp in Celsius : ").draw(win) Text(Point(1,1), "Temp in Fahrenheit : ").draw(win) input = Entry(Point(2,4), 6) input.setText("0.0") input.draw(win) output = Text(Point(2,1),"") output.draw(win) button = Text(Point(1.5,2.0),"Convert It") button.draw(win) Rectangle(Point(1,1.5), Point(2,2.5)).draw(win) while True : win.getMouse() # waiting for a click # Conversions cel = eval(input.getText()) Fah = 9.0/5.0 * cel + 32 # Output output.setText("%0.1f" % Fah) main()
# Program for Rainbow Colours Concentric Circles graphics import * def main(): col_arr=["violet","indigo","blue","green","yellow","orange","red"] work = GraphWin('Rainbow Circle', 200, 200) x = work.getWidth()/2 y = work.getHeight()/2 i=0 while i <= len(col_arr): cir=Circle(Point(x, y), 11 +11*i) cir.setOutline(col_arr[i]) cir.setWidth(4) cir.draw(work) i+=1 message = Text(Point(work.getWidth()/2, 250), 'Click to Exit') message.draw(work) work.getMouse() work.close() main()
As we have seen that Graphics generally make our programming codes interactive. Thus, for GUI works we need to implement Graphics. So, here I brought to you information about the ” graphics.py ” library built by John Zelle. Thus, with this article, I have hope to have cleared your many doubts. So, get on with the tricks !!!