While working with students and students, I noticed that when studying a programming language, working with graphics is of great interest. Even those students who missed the Fibonacci numbers on assignments, and it would seem that they would have lost interest in learning the language, became more active on topics related to graphics.
Therefore, I propose to practice writing a small graphical program in Python using tkinter (a cross-platform library for developing a graphical user interface in Python).
The code in this article is written for Python 3.5.
')
Task : writing a program for drawing on the canvas of arbitrary size of circles of different colors.
Not difficult, perhaps the program is “childish”, but I think for a vivid illustration of what tkinter can do it.
I want to talk first about how to specify the color. Of course convenient for the computer way. For this, tkinter has a special tool that can be run this way:
from tkinter import * window = Tk() colorchooser.askcolor()
Explanation of the code:- rom tkinter import * - import of the library, or rather all its methods, as indicated by an asterisk (*);
- window = Tk () - creating a window tkinter;
- colorchooser.askcolor () - opens a color selection window and returns a tuple of two values: a tuple of three elements, the intensity of each RGB color, and a string. color in hexadecimal system.
Note: as stated in the comments below - the asterisk does not import everything, it will be safer to writefrom tkinter import colorchooser
It is possible to use English color names to determine the color of the drawing. Here I want to note that not all of them are supported.
It says that you can use the colors “white”, “black”, “red”, “green”, “blue”, “cyan”, “yellow”, “magenta” without any problems. But I still experimented, and you will see further what came of it.
In order to draw in Python you need to create a canvas. For drawing, the
x and
y coordinate system is used, where the point (0, 0) is in the upper left corner.
In general, enough introductions - let's start.
from random import * from tkinter import * size = 600 root = Tk() canvas = Canvas(root, width=size, height=size) canvas.pack() diapason = 0
Explanation of the code:- from random import * - import all methods of the random module;
- from tkinter import * - you already know this;
- size variable is needed later;
- root = Tk () - create a window;
- canvas = Canvas (root, width = size, height = size) - create a canvas using the value of the size variable (that's what you need);
- canvas.pack () - an instruction to position the canvas inside the window;
- The variable diapason will be needed later for use in the loop condition.
Next, create a loop:
while diapason < 1000: colors = choice(['aqua', 'blue', 'fuchsia', 'green', 'maroon', 'orange', 'pink', 'purple', 'red','yellow', 'violet', 'indigo', 'chartreuse', 'lime', ''
Explanation of the code:- while diapason <1000: - with a precondition that says that the cycle will be repeated until the variable diapason reaches 1000.
colors = choice(['aqua', 'blue', 'fuchsia', 'green', 'maroon', 'orange', 'pink', 'purple', 'red','yellow', 'violet', 'indigo', 'chartreuse', 'lime', ''
Create a list for supposedly random selection of color circles. Note that one of the colors is written in the format '' # f55c4b '' - the color code in the hexadecimal system.
Here I want to stay on the choice of color. I wanted to add as many color choices as possible, so I used
the color table in English . But I soon realized that many English titles are not supported - the program stopped working. Therefore, the definition of color in the hexadecimal system would be a more suitable option for this purpose.
x0 = randint (0, size) and y0 = randint (0, size) - a random selection of
x and
y coordinates within a canvas of size.
d randint (0, size / 5) is an arbitrary choice of the size of a circle, limited by size / 5.
canvas.create_oval (x0, y0, x0 + d, y0 + d, fill = colors) - as a matter of fact, we draw circles, at points with coordinates x0 and y0, with vertical and horizontal dimensions x0 + d and y0 + d, fill with color, which is selected randomly from the list of colors.
root.update () - update () - handles all tasks in the queue. Usually, this function is used during “heavy” calculations, when it is necessary that the application remains responsive to user actions.
Without this, circles will eventually appear, but the process of their appearance will not be visible to you. Namely, it gives the charm of this program.
diapason + = 1 - cycle step, counter.
The result is such a picture:

I didn’t like that some empty spaces are formed on the right and at the top, so I changed the condition of the while diapason <2000 or 3000 cycle a little. So the canvas turned out to be more filled.
You can also make the loop infinite:
while True: colors = choicecolors = choice(['aqua', 'blue', 'fuchsia', 'green', 'maroon', 'orange', 'pink', 'purple', 'red','yellow', 'violet', 'indigo', 'chartreuse', 'lime']) x0 = randint(0, size) y0 = randint(0, size) d = randint(0, size/5) canvas.create_oval(x0, y0, x0+d, y0+d, fill=colors ) root.update()
This is how it happens:
instagram.com/p/8fcGynPlEcI think we could still play around with the speed of drawing circles or their movement on the canvas. It was possible to increase the choice of colors. Set a condition for stopping an infinite loop, for example by pressing the spacebar. These are all tasks for future programs.
The students still asked, is it possible to run it as a screensaver on the Windows desktop? Not yet found how this could be done.
Sources:
Documentation for python
http://www.ilnurgi1.ru/docs/python/modules/tkinter/colorchooser.htmlPython Tkinter courseHelpers:
Introduction to Tkinter
http://habrahabr.ru/post/133337Creating a GUI in Python using the Tkinter library. Programming for beginners
http://younglinux.info/book/export/html/48Table of color names in English
http://www.falsefriends.ru/english-colors.htm