Skip to content

Commit

Permalink
simplify app exemples
Browse files Browse the repository at this point in the history
  • Loading branch information
rasql committed Dec 29, 2019
1 parent 51a925e commit 49b2cc0
Show file tree
Hide file tree
Showing 61 changed files with 477 additions and 229 deletions.
2 changes: 1 addition & 1 deletion docs/basic/style3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self):
Label('Widget style', font='Arial 24')

Label('widget state')
c = Checkbox('active;disabled;focus;pressed;selected;'
c = Checkbutton('active;disabled;focus;pressed;selected;'
'background;readonly;alternate;invalid;hover')

# tk.Frame(App.stack[-1], width=100, height=100, borderwidth=1, relief='solid').grid()
Expand Down
139 changes: 102 additions & 37 deletions docs/button/button.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,64 +221,129 @@ Pressing the 3 buttons one after another writes this to the console::
:download:`button3.py<button3.py>`


Radiobutton
Checkbutton
-----------

A **radiobutton** lets you choose among a number of mutually
exclusive choices. Radiobuttons are used together in a set and
are a good option when the number is fairly small (about 3-5).
A **checkbutton** is like a regular button,
with a label and a callback function,
but it also holds and displays a binary state.

.. image:: radio1.png
The ``Checkbutton`` object has the following attributes:

Each ``Radiobutton`` object has a
* **parent** - the parent object
* **text** - the label to display
* **command** - the callback function
* **variable** - the variable holding the state value
* **onvalue** - the value when in the ON state
* **offvalue** - the value when in the OFF state

* parent
* text to display
* common variable (StringVar, IntVar)
* specific value
* shared callback function
Here is an example of 3 checkbuttons

.. literalinclude:: radio1.py
.. image:: check1.png

:download:`radio1.py<radio1.py>`
The callback function ``cb`` writes this to the console::

The radiobutton part consists of 7 lines and has a lot of repetitions such as:
--- languages ---
English 1
German 0
French fluent

* the beginning ``tk.Radiobuttion(root, text=``
* the shared variable ``variable=lang``
* the common callback function ``command=cb``
We notice that the default offvalue is ``0`` and the default onvalue is ``1``.
In our case:

A better Radiobutton
--------------------
* var0 toggles between ``0`` and ``1``
* var1 toggles between ``barely`` and ``1``
* var2 toggles between ``0`` and ``fluent``

.. literalinclude:: check1.py

:download:`check1.py<check1.py>`

Now let us rewrite this program by using lists.

.. literalinclude:: check2.py

:download:`check2.py<check2.py>`

A better Checkbutton class
--------------------------

It's time now to define a new and better ``Checkbutton`` class which can do everything in one line::

Now let's redefine the ``Radiobutton`` class to make everything in just one line::
Checkbutton('English;German;French', 'print(self.selection)')

Radiobutton('English;German;French', 'print(self.item)')
* the items are declared as a **semicolon-separated list**
* the command is a string to be evaluated in the Checkbutton environment
* the items are available in ``self.items``
* the selected items are available in ``self.selection``
* the selection states are available in ``self.val``

* the items are declared as a semicolon-separated list
* the command is a string to be evaluated in the Radiobutton environment
* the selected value is available in ``self.item``
* the selection index is available in ``self.val`` (could be used as a list index)
This is the result written to the console for three consecutive selections::

.. literalinclude:: radio2.py
['French']
['German', 'French']
['English', 'German', 'French']

:download:`radio2.py<radio2.py>`
.. literalinclude:: check3.py

:download:`check3.py<check3.py>`

Now let's see how this class is defined

.. literalinclude:: tklib.py
:pyobject: Radiobutton
:pyobject: Checkbutton

Entry
-----

An **entry** widget presents the user an empty field
where he can enter a text value.

.. image:: entry1.png

Each ``Entry`` object has these options:

* **parent** - the parent object
* **textvariable** - the text variable to hold the entered string
* **width** - the numbers of characters
* **show** - to indicate ``*`` for passwords

The item string is split at the semicolons into a regular list.
The common variable is a ``IntVar`` object.
Each radiobutton has an integer value (0, 1, 2, ...).
The callback function finds the selected item by looking up this integer value in the items list.
They do not have a ``text`` or ``image`` option.
You have to use a label widget instead.

Let's look at another exemple.
This time we add another language (Italian) and initialize the default button to 2 (French).
.. literalinclude:: entry1.py

.. image:: radio3.png
:download:`entry1.py<entry1.py>`

.. literalinclude:: radio3.py

:download:`radio3.py<radio3.py>`
A better Entry class
--------------------

It's time now to define a new and better ``Entry`` class
which can do everything in one line::

Entry('First name:', 'print(self.var.get())')

This new class has the attributes:

* **label** - to automatically add label in front of the entry field
* **cmd** - to execute a command string when hitting the Return key
* **val** - a default value

.. image:: entry2.png

The command function evaluates the expression entered and
displays the result in the following label widget::

Entry('Enter expression', 'App.res["text"] = eval(self.var.get())')
App.res = Label('Result')

.. literalinclude:: entry2.py

:download:`entry2.py<entry2.py>`

Now let's see how this class is defined

.. literalinclude:: tklib.py
:pyobject: Entry

Binary file added docs/button/check1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions docs/button/check1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import tkinter as tk

root = tk.Tk()

var0 = tk.StringVar()
var1 = tk.StringVar()
var2 = tk.StringVar()

var0.set('1')
var1.set('0')
var2.set('0')

def cb():
print('--- languages ---')
print('English', var0.get())
print('German', var1.get())
print('French', var2.get())

tk.Checkbutton(root, text='English', variable=var0, command=cb).pack()
tk.Checkbutton(root, text='German', variable=var1, offvalue='barely', command=cb).pack()
tk.Checkbutton(root, text='French', variable=var2, onvalue='fluent' command=cb).pack()

root.mainloop()
20 changes: 20 additions & 0 deletions docs/button/check2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import tkinter as tk

root = tk.Tk()

texts = ['English', 'German', 'French']
vars = [tk.StringVar(), tk.StringVar(), tk.StringVar()]

for var in vars:
var.set('0')

def cb():
print('--- languages ---')
for i, s in enumerate(texts):
print(s, vars[i].get())

tk.Checkbutton(root, text=texts[0], variable=vars[0], command=cb).pack()
tk.Checkbutton(root, text=texts[1], variable=vars[1], command=cb).pack()
tk.Checkbutton(root, text=texts[2], variable=vars[2], command=cb).pack()

root.mainloop()
6 changes: 6 additions & 0 deletions docs/button/check3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Create checkbuttons."""
from tklib import *

app = App()
Checkbutton('English;German;French', 'print(self.selection)')
app.run()
Binary file added docs/button/entry1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/button/entry1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import tkinter as tk
root = tk.Tk()

name = tk.StringVar()
tk.Label(text='Name').pack()
tk.Entry(root, textvariable=name, width=10).pack()

password = tk.StringVar()
tk.Label(text='Password').pack()
tk.Entry(root, textvariable=password, show='*').pack()

root.mainloop()
Binary file added docs/button/entry2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions docs/button/entry2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Create entry fields."""
from tklib import *
app = App('Entry')

Label('Entry fields', font='Arial 24')
Entry('First name:', 'print(self.var.get())', 'James')
Entry('Last name:', 'print(self.var.get())')
Entry('Password:', 'print(self.var.get())', show='*')

Entry('Enter expression', 'App.res["text"] = eval(self.var.get())')
App.res = Label('Result')

app.run()
14 changes: 0 additions & 14 deletions docs/button/radio1.py

This file was deleted.

Binary file added docs/button/radio4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/canvas/canvas1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 7 additions & 13 deletions docs/canvas/canvas1.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
"""Create a canvas and add lines and rectangles."""
from tklib import *
app = App('Canvas with lines and rectangles')

class Demo(App):
def __init__(self):
super().__init__()
Label("Canvas", font="Arial 24")
c = Canvas(width=600, height=300, background='lightblue')
c.create_line(10, 10, 200, 200, fill='red')
c.create_line(20, 10, 210, 200, fill='blue', width=3)
c.create_rectangle(100, 200, 150, 250, fill='green', width=2)
c.create_rectangle(300, 100, 580, 250, fill='yellow', width=5)

c = Canvas(width=600, height=300, background='lightblue')

c.create_line(10, 10, 200, 200, fill='red')
c.create_line(20, 10, 210, 200, fill='blue', width=3)
c.create_rectangle(100, 200, 150, 250, fill='green', width=2)
c.create_rectangle(300, 100, 580, 250, fill='yellow', width=5)

Demo().run()
app.run()
Binary file modified docs/canvas/canvas2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 9 additions & 15 deletions docs/canvas/canvas2.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
"""Draw lines, ovals and text on a canvas."""
from tklib import *
app = App('Canvas with ovals and text')

class Demo(App):
def __init__(self):
super().__init__()
Label("Canvas", font="Arial 24")
c = Canvas(width=600, height=300, background='lightblue')
c.create_line(10, 10, 200, 200, fill='red')
c.create_line(20, 10, 210, 200, fill='blue', width=3)
c.create_oval(100, 200, 150, 250, fill='green', width=2)
c.create_oval(300, 100, 580, 250, fill='yellow', width=5)
c.create_text(300, 150, text="Python", font='Arial 48')
c.create_text(300, 250, text="Canvas", font='Zapfino 72')

c = Canvas(width=600, height=300, background='lightblue')

c.create_line(10, 10, 200, 200, fill='red')
c.create_line(20, 10, 210, 200, fill='blue', width=3)
c.create_oval(100, 200, 150, 250, fill='green', width=2)
c.create_oval(300, 100, 580, 250, fill='yellow', width=5)
c.create_text(300, 150, text="Python", font='Arial 48')
c.create_text(300, 250, text="Canvas", font='Zapfino 72')

Demo().run()
app.run()
Binary file modified docs/canvas/canvas4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 5 additions & 9 deletions docs/canvas/canvas4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
"""Place polygons on the canvas."""
from tklib import *
app = App('Draw a polygon')

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

Demo().run()
app.run()
Binary file modified docs/canvas/canvas5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 9 additions & 14 deletions docs/canvas/canvas5.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
"""Place random circles on the canvas."""
from tklib import *
app = App('Random circles')

class Demo(App):
def __init__(self):
super().__init__()
Label("Random Circles", font="Arial 24")
w, h = 600, 300
c = Canvas(width=w, height=h, background='lightblue')

w, h = 600, 300
c = Canvas(width=w, height=h, background='lightblue')

for i in range(50):
x = random.randint(0, w)
y = random.randint(0, h)
r = random.randint(10, 100)
c.create_oval(x, y, x+r, y+r)
for i in range(50):
x = random.randint(0, w)
y = random.randint(0, h)
r = random.randint(10, 100)
c.create_oval(x, y, x+r, y+r)

Demo().run()
app.run()

0 comments on commit 49b2cc0

Please sign in to comment.