Créer des graphiques scientifiques avec Tkinter
Créer des graphiques scientifiques avec Tkinter
(./page-python-beginner-course-documentation)
(./advanced-python-course-documentation-
French
ieoo-c
rdja
tcu
usdl-g-ounmentation-
t-rg(./
espa
fra
se
caIn)
-ac
tta
eb
uhe
dert-rea-rpepgay-p
drrnpb-e(./api
python
Install Tkinter
Tkinter is installed by default, if not, run the following command:
In Python 3:
Python 2, Python 3
The modules are not the same depending on your version of Python. If the following message appears
during the execution of your script:
The called module is not the right one for your python version.
Python 2 Python 3
Tkinter → tkinter
Tix → [Link]
ttk → [Link]
tkMessageBox → [Link]
tkColorChooser→ [Link]
tkFileDialog→ [Link] dialog
tkCommonDialog→ [Link]
tkSimpleDialog→ [Link]
tkFont → [Link]
Tkdnd → [Link]
ScrolledText → [Link]
Hello world
Here is the code for your first hello world
# coding: utf-8
window = Tk()
Hello World
[Link]()
[Link]()
The buttons
The buttons allow for an action to be offered to the user. In the example below, he is
propose to close the window.
exit button
Button(window, text='Close', command=[Link])
[Link]()
The labels
Labels are spaces intended for writing text. Labels are often used to describe a
widget like an input
# label
Label(fenetre, text='Default text', bg='yellow')
[Link]()
Entry / input
entry
StringVar()
set value to "default text"
Entry(fenetre, textvariable=string, width=30)
[Link]()
Checkbox
Checkboxes allow the user to check an option.
check button
button = Checkbutton(window, text="New?")
[Link]()
Radio buttons
Radio buttons are checkboxes that are in a group and in this group only one
element can be selected.
radiobutton
StringVar()
Radiobutton(window, text="Yes", variable=value, value=1)
bouton2 = Radiobutton(fenetre, text="Non", variable=value, value=2)
Radiobutton(window, text="Maybe", variable=value, value=3)
[Link]()
[Link]()
[Link]()
The lists
Lists allow retrieving a value selected by the user.
list
list = Listbox(window)
[Link](1, "Python")
[Link](2, "PHP")
[Link](3, "jQuery")
[Link](4, "CSS")
[Link](5, "Javascript")
[Link]()
Canvas
A canvas (canvas, painting in French) is a space in which you can draw or write.
what do you want:
canvas
Canvas(fenetre, width=150, height=120, background='yellow')
line1 = canvas.create_line(75, 0, 75, 120)
line2 = canvas.create_line(0, 60, 150, 60)
canvas.create_text(75, 60, text="Target", font="Arial 16 italic", fill="blue")
[Link]()
If you want to change the coordinates of an element created in the canvas, you can use the
methodcoords.
[Link](element)
You can find other useful methods by executing the following command:
printdir(Canvas())
You visit the following page infohost ([Link]
[Link]
Scale
The scale widget allows you to retrieve a numeric value via a scroll.
DoubleVar()
Scale(window, variable=value)
[Link]()
Frames
Frames are containers that allow for the separation of elements.
white
# frame 1
Frame1 = Frame(window, borderwidth=2, relief=GROOVE)
[Link](side=LEFT, padx=30, pady=30)
frame 2
Frame2 = Frame(window, borderwidth=2, relief=GROOVE)
[Link](side=LEFT, padx=10, pady=10)
frame 3 in frame 2
Frame3 = Frame(Frame2, bg="white", borderwidth=2, relief=GROOVE)
[Link](side=RIGHT, padx=5, pady=5)
Adding labels
Label(Frame1, text="Frame 1").pack(padx=10, pady=10)
Label(Frame2, text="Frame 2").pack(padx=10, pady=10)
Label(Frame3, text="Frame 3",bg="white").pack(padx=10, pady=10)
PanedWindow
Lepanedwindow is a container that can hold as many panels as needed arranged.
horizontally or vertically.
p = PanedWindow(window, orient=HORIZONTAL)
[Link](side=TOP, expand=Y, fill=BOTH, pady=2, padx=2)
Panel 1
Panel 2
Panel 3
[Link]()
Spinbox
Laspinbox offers the user to choose a number.
LabelFrame
The labelframe is a frame with a label.
The alerts
To be able to use the alerts of your OS, you can import the moduletkMessageBox(Python
2).
For python 3:
from [Link] import *
Usage example:
defcallback():
Title 1
Title 2
else:
Title 3
Title 4
Action
showinfo()
show warning()
showerror()
askquestion()
askokcancel()
askyesno()
askretrycancel()
Menu bar
It is possible to create a menu bar like this:
defalert():
Alert: Bravo!
menubar = Menu(window)
[Link](menu=menubar)
print dir(Button())
Standard attributes
It is possible to change the value of attributes present on the widgets.
Place widgets
It is possible to place the widgets using the parameter side:
side=TOP high
left
side=BOTTOM : base
right
Example:
Another example:
Units of measurement
If you specify a dimension using an integer, the unit used will be 'pixels'. However, it is...
possible to change the unit:
inches
mm : millimeter
c : centimeter
Dimension options
height Height of the widget.
width Widget width.
padx, pady Additional space around the widget. X for horizontal and V for vertical.
border width Border size.
Width of the rectangle when the widget has focus.
selectborderwidth: Width of the three-dimensional border around the selected widget.
wrap length Maximum number of lines for widgets in 'word wrapping' mode.
Color options
It is possible to indicate a color value by its name in English: "white", "black", "red",
"yellow", etc. or by its hexadecimal code: #000000, #00FFFF, etc.
background (or bg) background color of the widget.
foreground (or fg) foreground color of the widget.
active background background color of the widget when it is active.
activeForeground foreground color of the widget when the widget is active.
foreground color of the widget when the widget is disabled.
Background color of the highlight region when the widget has focus.
highlight color foreground color of the highlighted area when the widget is focused
select background Background color for selected items.
selectforeground foreground color for selected elements.
The cursor
You can change the appearance of your cursor:
The relief
You can change the relief on your elements:
FLAT
RAISED
SUNken
GROOVE
RIDGE
Insert an image
To integrate an image, you can create a canvas and add it inside like this:
PhotoImage(file="my_photo.png")
defrecupere():
showinfo("Alert", [Link]())
StringVar()
[Link]("Value")
entry = Entry(window, textvariable=value, width=30)
[Link]()
The function askopenfilename returns the path of the file you selected with the name of
this one.
/home/olivier/my_photo.png
Retrieve a text file and display it
Open your document
file = open(filename, "r")
content = [Link]()
[Link]()
The events
You can retrieve user actions through events.
For each widget, you can bind an event, for example to say when
the user presses such a key, do this.
def keyboard(event):
touch = [Link]
print(touch)
To remove the event binding, you can use the methods unbind or unbind_all.
Here is an example where you can move a square with the arrow keys:
function called when the user presses a key
def keyboard(event):
globalcoords
[Link]
iftouche == "Up":
(coords[0], coords[1] - 10)
Down
coords = (coords[0], coords[1] + 10)
Right
coords = (coords[0] + 10, coords[1])
Left
coords = (coords[0] -10, coords[1])
change of coordinates for the rectangle
[Link](rectangle, coords[0], coords[1], coords[0]+25, coords[1]+25)
canvas creation
Canvas(window, width=250, height=250, bg='ivory')
initial coordinates
coords = (0, 0)
creation of the rectangle
canvas.create_rectangle(0,0,25,25,fill="violet")
adding the bond on the keyboard keys
canvas.focus_set()
[Link]("<Key>", keyboard)
canvas creation
[Link]()
[Link]
([Link] - [Link]
([Link] - develop
[Link] - sebsauvage
([Link]
ie=UTF8&camp=1642&creative=6746&creativeASIN=2746071711&linkCode=as2&tag=pythondjango-
21&linkId=bdf2387af4f5ebddbf6a49ab0369c185)
([Link]
ie=UTF8&camp=1642&creative=6746&creativeASIN=1797866478&linkCode=as2&tag=pythondjango-
21&linkId=1bbb923046b465218bd107d69482d5)
([Link]
ie=UTF8&camp=1642&creative=6746&creativeASIN=2409012264&linkCode=as2&tag=pythondjango-
21&linkId=635e1fd05fa3fa8a13d66632af388dd8)
Python beginner
Tuples (./page-learn-tuples-tuple-python)
Dictionaries (./page-learn-dictionary-python)
Functions (./page-learn-create-function-in-python)
Exceptions (./page-learn-exceptions-except-python-beginner-course)
Decorators (./page-decorateurs-decorator-python-cours-debutants)
Iterators/Generators
python)
Advanced Python
Virtualenv (./page-virtualenv-python-virtual-environment)
Debug (./debugger-debug-python-ipython-pdb-pdbpp-course-
tutorial-script
FTP (./page-python-ftp-create-copy-view-folder-files)
Database (./page-database-data-base-data-query-sql-mysql-postgre-
sqlite
Django
Queryset (./page-django-query-set-queryset-manager)
Django Signals
tutorial-example
Xadmin (./page-django-xadmin-bootstrap-twitter-theme-export-xls-csv-xml-
python-web-plugin
Raspberry Pi
Static IP (./page-raspberry-pi-static-ip-address)
Site