Skip to content

Commit

Permalink
fixed intro chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
rasql committed Dec 27, 2019
1 parent 67232e1 commit 29a9cae
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 84 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# tk-tutorial
Create OOP applications using the Tk interface.
Introduction to Tk
==================

You can find the online tutorial here:
https://tk-tutorial.readthedocs.io/
Tk is a **graphical user interface** (GUI) library. It allows to create windows, buttons and all the other graphical elements.

This tutorial shows how to use **object-oriented programming** (OOP)
for making applications with the **Tk** framework.

You can find the online tutorial here: https://tk-tutorial.readthedocs.io

More tutorials
--------------

* https://opencv-tutorial.readthedocs.io
* https://pygame.readthedocs.io
* https://pymunk-tutorial.readthedocs.io
* https://tk-tutorial.readthedocs.io
* https://cocos2d-tutorial.readthedocs.io
3 changes: 2 additions & 1 deletion docs/basic/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Basic widgets
* Button
* Label
* Entry
* Frame
* Frame

4 changes: 2 additions & 2 deletions docs/concept/concept.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ Automatic placement

Widgets are placed automatically with the ``grid`` method.

.. autoclass:: concept4.Demo

.. image:: concept4.png

.. literalinclude:: concept4.py
File renamed without changes
8 changes: 1 addition & 7 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
Welcome to this Tk tutorial!
============================

* https://opencv-tutorial.readthedocs
* https://pygame.readthedocs.io
* https://pymunk-tutorial.readthedocs.io
* https://tk-tutorial.readthedocs.io
* https://cocos2d-tutorial.readthedocs.io


.. toctree::
:maxdepth: 2
:caption: Contents:

intro/intro
basic/basic
concept/concept
event/event
listbox/listbox
menu/menu
Expand Down
36 changes: 36 additions & 0 deletions docs/intro/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import tkinter as tk
import tkinter.ttk as ttk

class Label(ttk.Label):
"""Create a Label object."""
def __init__(self, text='Label', **kwargs):
super().__init__(App.stack[-1], text=text, **kwargs)
self.grid()

class Button(ttk.Button):
"""Create a Button object."""
def __init__(self, text='Button', **kwargs):
super().__init__(App.stack[-1], text=text, **kwargs)
self.grid()

class App:
"""Define the application class."""
stack = []

def __init__(self):
self.root = tk.Tk()
self.root.title('App')
App.stack.append(self.root)

Label('New Label')
Label()

Button('New Button')
Button()

def run(self):
"""Run the main loop."""
self.root.mainloop()

if __name__ == '__main__':
App().run()
119 changes: 72 additions & 47 deletions docs/intro/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Introduction to Tk
Tk is a **graphical user interface** (GUI) library. It allows to create windows, buttons and all the
other graphical elements.
This tutorial shows how to use **object-oriented programming** (OOP)
for making applications using the **Tk** framework.
for making applications with the **Tk** framework.


Our first program
Expand Down Expand Up @@ -73,46 +73,68 @@ This is the result:

.. literalinclude:: intro2.py

Labels
------

Labels are used to add passive text to the window.
We define a new ``Label()`` class which is added automatically to the current
context which is stored in the class variable ``App.stack[-1]``.
For all placement of widgets we are going to use the ``grid()`` method:
Classic and new themed widgets
------------------------------

.. literalinclude:: tklib.py
:pyobject: Label
The elements of a graphical user interface are called **widgets**.
In Tk there are two generations of widgets:

This is a screen capture of the result.
* the classic ``tk`` widgets
* the new **themed** ``ttk`` widgets

.. image:: intro3.png
The new themed widgets can be found in the submodule ``tkinter.ttk``.
We import the classic and the new themed widgets with this import statement::

import tkinter as tk
import tkinter.ttk as ttk

This creates a **classic label** and a new **themed label**::

tk.Label(self.root, text='tk.Label').pack()
ttk.Label(self.root, text='ttk.Label').pack()

This creates a **classic button** and a new **themed button**::

tk.Button(self.root, text='tk.Button').pack()
ttk.Button(self.root, text='ttk.Button').pack()

This is a screen capture of the result.
The new themed widgets have a gray background and the buttons have uniform size.

.. image:: intro3.png

:download:`intro3.py<intro3.py>`

.. literalinclude:: intro3.py

Defined our own widget class
----------------------------

Buttons
-------
We are now going to define our own Tk widget classes.
They have the following advantages:

Buttons can be clicked and are used to execute a command associated with them.
The following demo creates 4 buttons::
* the **text** option has a default (Label, Button)
* the **parent** object is automatically set (root)
* all **keyword** arguments are passed on (kwargs)
* the **themed** version is used when available (ttk)

This is the new ``Label`` class:

class Demo(App):
def __init__(self):
super(Demo, self).__init__()
Label('Button demo', font='Arial 24')
Button('Start', 'print("Start")')
Button('Stop', 'print("Stop")')
Button('Self', 'print(self)')
Button('Destroy', 'self.destroy()')
.. literalinclude:: intro.py
:pyobject: Label

.. automodule:: intro5
:members:
This is the new ``Button`` class:

This is a screen capture of the above program.
.. literalinclude:: intro.py
:pyobject: Button


Buttons
-------

Buttons can be clicked and are used to execute a command associated with them.
The following demo creates 4 buttons.

.. image:: intro5.png

Expand All @@ -121,55 +143,58 @@ This is a screen capture of the above program.
* The **Self** button prints the button object string to the console
* The **Destroy** button removes the button from the window

:download:`intro5.py<intro5.py>`

.. literalinclude:: intro5.py


Radiobuttons
------------

Radiobuttons are active elements which can be clicked and execute actions. Only one button is active at any one time.
Radiobuttons are active elements which can be clicked and execute actions.
Only one button is active at any one time.

.. automodule:: intro6
:members:
.. image:: intro6.png

This is a screen capture of the above program.
:download:`intro6.py<intro6.py>`

.. image:: intro6.png
.. literalinclude:: intro6.py


Checkbuttons
------------

Checkbuttons are active elements which can be clicked and execute actions. Multiple checkbuttons can be selected simultaneously.
Checkbuttons are active elements which can be clicked and execute actions.
Multiple checkbuttons can be selected simultaneously.

.. automodule:: intro7
:members:
.. image:: intro7.png

This is a screen capture of the above program.
:download:`intro7.py<intro7.py>`

.. image:: intro7.png
.. literalinclude:: intro7.py


Entry fields
------------

Entry **entry** field presents the user with a single line text field where he can enter a string value.
Entry **entry** field presents the user with a single line text field
where he can enter a string value.

.. automodule:: intro8
:members:
.. image:: intro8.png

This is a screen capture of the above program.
:download:`intro8.py<intro8.py>`

.. literalinclude:: intro8.py

.. image:: intro8.png

Combobox
--------

A **combobox** combines a list of choices with an entry. The user can select from the list, but he can also enter directly a value.

.. automodule:: intro9
:members:

This is a screen capture of the above program.
A **combobox** combines a list of choices with an entry.
The user can select from the list, but he can also enter directly a value.

.. image:: intro9.png

:download:`intro9.py<intro9.py>`

.. literalinclude:: intro9.py
Binary file added docs/intro/intro3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 5 additions & 12 deletions docs/intro/intro3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ class App:
"""Define the application class."""
def __init__(self):
self.root = tk.Tk()
self.root.title('App Demo')
tk.Label(self.root, text='Placing multiple widgets').pack()
self.root.title('App')

tk.Label(self.root, text='Label').pack()
ttk.Label(self.root, text='Label').pack()
tk.Label(self.root, text='tk.Label').pack()
ttk.Label(self.root, text='ttk.Label').pack()

tk.Button(self.root, text='Button').pack()
ttk.Button(self.root, text='Button').pack()

tk.Radiobutton(self.root, text='Radiobutton').pack()
ttk.Radiobutton(self.root, text='Radiobutton').pack()

tk.Checkbutton(self.root, text='Checkbutton').pack()
ttk.Checkbutton(self.root, text='Checkbutton').pack()
tk.Button(self.root, text='tk.Button').pack()
ttk.Button(self.root, text='ttk.Button').pack()

def run(self):
"""Run the main loop."""
Expand Down
4 changes: 0 additions & 4 deletions docs/intro/intro5.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Create buttons."""

from tklib import *

class Demo(App):
Expand All @@ -10,9 +9,6 @@ def __init__(self):
Button('Stop', 'print("Stop")')
Button('Self', 'print(self)')
Button('Destroy', 'self.destroy()')
Button('root', 'print(App.root)')
Button('parent', 'print(App.stack[-1])')
Button('geometry', 'print(App.root.geometry())')

if __name__ == '__main__':
Demo().run()
Binary file modified docs/intro/intro6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions docs/intro/intro6.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Create radio buttons."""

from tklib import *

class Demo(App):
Expand All @@ -9,9 +8,6 @@ def __init__(self):

Label('Select your favorite programming language')
Radiobutton('Python;Perl;Ruby;Java;C++', 'print(self.item)')

Label('Select your favorite day of the week')
Radiobutton('Mon;Tue;Wed;Thu;Fri', 'print(self.item)')

if __name__ == '__main__':
Demo().run()
Binary file modified docs/intro/intro7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions docs/intro/intro7.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ def __init__(self):

Label('Select your favorite languages')
Checkbox('Python;Perl;Ruby;Java;C++', 'print(self.selection)')

Label('Select your working days')
Checkbox('Mon;Tue;Wed;Thu;Fri', 'print(self.selection)')

if __name__ == '__main__':
Demo().run()

0 comments on commit 29a9cae

Please sign in to comment.