Skip to content

Commit

Permalink
fix text chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
rasql committed Dec 27, 2019
1 parent 29a9cae commit 348f2c5
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 170 deletions.
92 changes: 75 additions & 17 deletions docs/canvas/canvas.rst
Original file line number Diff line number Diff line change
@@ -1,54 +1,112 @@
Canvas
======

The **Canvas** widget can be used to draw lines, shapes, and text
to create complex drawings and graphs.
The origin (0, 0) of the canvas is in the top left corner.
It has the keyword options:

.. automodule:: canvas1
:members:
* background = background color
* borderwidth
* height
* width

This is a screen capture of the above program.

Draw lines and rectangles
-------------------------

To draw lines and rectangles, use these **create** methods:

* create_line()
* create_rectangle()

.. image:: canvas1.png

.. literalinclude:: canvas1.py

:download:`canvas1.py<canvas1.py>`


Create text
-----------

.. automodule:: canvas2
:members:
Text can be added to a canvas with this function:

This is a screen capture of the above program.
* create_text()

.. image:: canvas2.png

.. literalinclude:: canvas2.py

:download:`canvas2.py<canvas2.py>`


Painting using ovals
--------------------

.. automodule:: canvas3
:members:

This is a screen capture of the above program.
Small ovals can be used to paint with the mouse,
by binding a callback function to the mouse movement.

.. image:: canvas3.png

.. literalinclude:: canvas3.py

:download:`canvas3.py<canvas3.py>`


Polygons
--------

.. automodule:: canvas4
:members:
We can add our own methods to the Canvas class.
For example we can define a method to add a polygon.

This is a screen capture of the above program.
.. literalinclude:: tklib.py
:pyobject: Canvas.polygon

.. image:: canvas4.png

.. literalinclude:: canvas4.py

:download:`canvas4.py<canvas4.py>`


Random circles
--------------

.. automodule:: canvas5
:members:
The following program places circles of random size at random locations.

.. image:: canvas5.png

.. literalinclude:: canvas5.py

:download:`canvas5.py<canvas5.py>`


Canvas configuration
--------------------

.. image:: canvas6.png

.. literalinclude:: canvas6.py

:download:`canvas6.py<canvas6.py>`


Canvas configuration with tree view
-----------------------------------

.. image:: canvas7.png

.. literalinclude:: canvas7.py

:download:`canvas7.py<canvas7.py>`


Drawing shapes with the mouse
-----------------------------

.. image:: canvas9.png

This is a screen capture of the above program.
.. literalinclude:: canvas9.py

.. image:: canvas5.png
:download:`canvas9.py<canvas9.py>`
5 changes: 2 additions & 3 deletions docs/canvas/canvas1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Canvas", font="Arial 24")

c = Canvas(width=600, height=300, background='lightblue')
Expand All @@ -13,5 +13,4 @@ def __init__(self):
c.create_rectangle(100, 200, 150, 250, fill='green', width=2)
c.create_rectangle(300, 100, 580, 250, fill='yellow', width=5)

if __name__ == '__main__':
Demo().run()
Demo().run()
3 changes: 1 addition & 2 deletions docs/canvas/canvas2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ def __init__(self):
c.create_text(300, 150, text="Python", font='Arial 48')
c.create_text(300, 250, text="Canvas", font='Zapfino 72')

if __name__ == '__main__':
Demo().run()
Demo().run()
7 changes: 3 additions & 4 deletions docs/canvas/canvas3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Painting using ovals", font="Arial 24")
self.c = Canvas(600, 300)
self.c = Canvas(width=600, height=300, background='lightblue')
self.c.bind('<B1-Motion>', self.paint)

def paint(self, event):
Expand All @@ -14,5 +14,4 @@ def paint(self, event):
x1, y1 = event.x+1, event.y+1
self.c.create_oval(x0, y0, x1, y1, fill='blue')

if __name__ == '__main__':
Demo().run()
Demo().run()
7 changes: 3 additions & 4 deletions docs/canvas/canvas4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Polygons", font="Arial 24")
self.c = Canvas(600, 300)
self.c = Canvas(width=600, height=300, background='lightblue')
self.c.polygon(150, 150, 100, 6, fill='blue')
self.c.polygon(450, 150, 80, 8, fill='red', width=5)

if __name__ == '__main__':
Demo().run()
Demo().run()
6 changes: 2 additions & 4 deletions docs/canvas/canvas5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Place random circles on the canvas."""
from tklib import *
import random

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Random Circles", font="Arial 24")

w, h = 600, 300
Expand All @@ -16,5 +15,4 @@ def __init__(self):
r = random.randint(10, 100)
c.create_oval(x, y, x+r, y+r)

if __name__ == '__main__':
Demo().run()
Demo().run()
5 changes: 2 additions & 3 deletions docs/canvas/canvas6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Canvas configuration", font="Arial 24")

Spinbox('width', 'App.c["width"]=self.val.get()', inc=50, to=1000)
Expand All @@ -18,5 +18,4 @@ def __init__(self):

App.c = Canvas()

if __name__ == '__main__':
Demo().run()
Demo().run()
5 changes: 2 additions & 3 deletions docs/canvas/canvas7.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Canvas configuration", font="Arial 24")

App.stack[-1]=Frame()
Expand All @@ -19,5 +19,4 @@ def __init__(self):
if len(v)>2:
tree.insert('', 'end', text=k, values=v[-1])

if __name__ == '__main__':
Demo().run()
Demo().run()
Binary file added docs/canvas/canvas9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions docs/canvas/canvas9.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Draw lines."""
"""Draw shapes with the mouse."""
from tklib import *

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Drawing shapes", font="Arial 24")

Spinbox('width', 'App.c["width"]=self.val.get()', inc=50, to=1000)
Expand All @@ -27,5 +27,4 @@ def __init__(self):

App.c.create_rectangle(20, 20, 150, 100)

if __name__ == '__main__':
Demo().run()
Demo().run()
28 changes: 18 additions & 10 deletions docs/event/event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ Events

Tk events can come from various sources including:

* mouse button presses
* mouse mouvement
* mouse (move, button)
* keyboard
* entering/leaving widgets
* widget (enter/leave)

Each widget can bind functions and methods to events::
Each widget can bind a **handler** method to an event::

widget.bind(event, handler)

Expand All @@ -32,12 +31,15 @@ Here is an exemple which prints mouse **Button** and **Motion** events to the st
"""Callback function."""
App.status['text'] = event

.. autoclass:: event1.Demo

This is a screen capture of the above program.

.. image:: event1.png

:download:`event1.py<event1.py>`

.. literalinclude:: event1.py


Write events to a Text widget
-----------------------------
Expand All @@ -49,15 +51,18 @@ just by changing the callback function::
"""Callback function."""
App.txt.insert('end', str(event) + '\n')

.. autoclass:: event2.Demo

This is a screen capture of the above program.

.. image:: event2.png

:download:`event2.py<event2.py>`

Detecting Enter, Leave and Return events
----------------------------------------
.. literalinclude:: event2.py


Enter, Leave and Return events
------------------------------

The following program detects these events::

Expand All @@ -66,8 +71,11 @@ The following program detects these events::
App.root.bind('<Return>', self.cb)
App.root.bind('<Configure>', self.cb)

.. autoclass:: event3.Demo

This is a screen capture of the above program.

.. image:: event3.png
.. image:: event3.png

:download:`event3.py<event3.py>`

.. literalinclude:: event3.py
6 changes: 2 additions & 4 deletions docs/event/event2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Write event to Text widget."""

"""Write Button and Mouse events to a Text widget."""
from tklib import *

class Demo(App):
"""Write Button and Mouse events to a Text widget."""
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Button and Motion events", font="Arial 24")
App.txt = Text(scroll='y')

Expand Down
2 changes: 1 addition & 1 deletion docs/event/event3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Demo(App):
"""Write Enter, Leave and Return events to a Text widget."""
def __init__(self):
super(Demo, self).__init__()
super().__init__()
Label("Enter, Leave and Return events", font="Arial 24")

App.txt = Text()
Expand Down

0 comments on commit 348f2c5

Please sign in to comment.