Tutorial 5 - Python - / Graphical interfaces with Tkinter /


Exercise 1

We want to create an interface that displays an oval.

4 buttons to move this oval

1 button to exit the program.




- First you have to create a canvas (grey background) in which you will create an oval.

For this oval you must specify the color, the thickness of the border, and the 4 coordinates (upper left corner and lower right corner, as for the rectangles)

example: oval1 = canvas.create_oval(x1,y1,x2,y2,y2,width=1,fill='green')



- The 4 coordinates are 4 variables that can (exceptionally) be used in global when using the functions

- The 4 buttons must use 4 functions (without argument)

- These functions must change the coordinate values and then display the oval again.

- To re-display the oval to another position, you only need to reassign new coordinates to it:

  canvas.coords(monovale, x1, y1, x2, y2)

- Then (and only if the program is already running...) add the possibility to move the oval with the keyboard arrows (in addition to the buttons)

You need to use the "bind" method on an object as in this example:

    window.bind("<Down>", move_down)

. The first argument is the keyboard key (<Left>, <Right>, <Up>, <Down>)

. The second is the action to execute, one of the 4 moving functions, except that... « bind » only accepts methods and not functions...

Solution: either put everything in object, or add a dummy argument when declaring the function (replaces the "self")

The function call, on the other hand, must be made without argument.





Exercice 2



Do a simple calculator (which works ! ) like this :