Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
INF-TC2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Negre Benjamin
INF-TC2
Commits
f2223a70
Commit
f2223a70
authored
4 years ago
by
BenjaminNEGRE
Browse files
Options
Downloads
Patches
Plain Diff
BE4
parent
0215c970
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
seance4_4h/Exo1.py
+128
-0
128 additions, 0 deletions
seance4_4h/Exo1.py
with
128 additions
and
0 deletions
seance4_4h/Exo1.py
0 → 100644
+
128
−
0
View file @
f2223a70
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 13:31:24 2020
@author: negre
"""
import
random
from
tkinter
import
*
#EX1
'''
def tirage():
nb = random.randint(1, 100)
texteResultat.set(
'
Nombre :
'
+ str(nb))
if __name__ ==
'
__main__
'
:
# création de l
'
arbre de scène
racine = Tk() # Appel à une méthode de classe (et non un constructeur, cf slide cours #2)
racine.title(
'
Tirage aléatoire
'
)
racine.geometry(
'
300x100+400+400
'
)
# Les widgets de la scène
boutonLancer = Button(racine, text=
'
Tirage
'
)
boutonLancer.pack(side=RIGHT, padx=5, pady=5)
texteResultat = StringVar()
labelResultat = Label(racine, textvariable=texteResultat,fg=
"
red
"
)
labelResultat.pack(side=RIGHT, padx=7, pady=7)
boutonQuitter = Button(racine, text=
'
Quitter
'
)
boutonQuitter.pack(side=LEFT, padx=5, pady=5)
#Arbre de scène : 3 éléments
# association des commandes aux widgets
boutonLancer.config(command=tirage) # appel dit callback (pas de parenthèses)
boutonQuitter.config(command=racine.destroy) # idem
racine.mainloop() # affichage de l
'
interface jusqu
'
à quit
'''
from
formes
import
*
from
tkinter
import
colorchooser
class
ZoneAffichage
(
Canvas
):
def
__init__
(
self
,
parent
,
largeur
,
hauteur
):
Canvas
.
__init__
(
self
,
parent
,
width
=
largeur
,
height
=
hauteur
)
self
.
formeActuelle
=
"
Rectangle
"
self
.
__ListeFormes
=
[]
def
ajout_forme
(
self
,
x
,
y
):
if
self
.
formeActuelle
==
"
Rectangle
"
:
self
.
__ListeFormes
.
append
(
Rectangle
(
self
,
x
-
10
,
y
+
5
,
20
,
10
,
"
brown
"
))
elif
self
.
formeActuelle
==
"
Ellipse
"
:
self
.
__ListeFormes
.
append
(
Ellipse
(
self
,
x
-
5
,
y
-
10
,
25
,
25
))
def
delete_forme
(
self
,
x
,
y
):
for
f
in
self
.
__listeFormes
:
if
f
.
contient_point
(
x
,
y
)
==
True
:
f
.
effacer
(
self
)
def
selection_rectangle
(
self
):
self
.
formeActuelle
=
"
Rectangle
"
def
selection_ellipse
(
self
):
self
.
formeActuelle
=
"
Ellipse
"
def
supression_forme
(
self
,
x
,
y
):
try
:
i
=
next
(
i
for
i
in
range
(
len
(
self
.
__formes
)
-
1
,
-
1
,
-
1
)
if
self
.
__formes
[
1
].
contient_point
(
x
,
y
))
class
FenPrincipale
(
Tk
):
def
__init__
(
self
):
Tk
.
__init__
(
self
)
# L'initialisation de l'arbre de scène se fait ici
self
.
configure
(
bg
=
'
grey
'
)
barreOutils
=
Frame
(
self
)
barreOutils
.
pack
(
side
=
TOP
)
bouttonRectangle
=
Button
(
barreOutils
,
text
=
'
Rectangle
'
)
bouttonRectangle
.
pack
(
side
=
LEFT
,
padx
=
5
,
pady
=
5
)
bouttonEllipse
=
Button
(
barreOutils
,
text
=
'
Ellipse
'
)
bouttonEllipse
.
pack
(
side
=
LEFT
,
padx
=
5
,
pady
=
5
)
bouttonCouleur
=
Button
(
barreOutils
,
text
=
'
Couleur
'
)
bouttonCouleur
.
pack
(
side
=
LEFT
,
padx
=
5
,
pady
=
5
)
bouttonQuitter
=
Button
(
barreOutils
,
text
=
'
Quitter
'
)
bouttonQuitter
.
pack
(
side
=
LEFT
,
padx
=
5
,
pady
=
5
)
Canevas
=
ZoneAffichage
(
self
,
600
,
400
)
Canevas
.
pack
(
side
=
TOP
,
padx
=
10
,
pady
=
10
)
self
.
__canevas
.
bind
(
"
<ButtonRelease-1>
"
,
self
.
release_canevas
)
# Bind permet de relier les clics de souris sur le canevas
# Chaine de caract = commande ET release_canevas = commande renvoyant à la méthode décrite dessous
boutonRectangle
.
config
(
command
=
self
.
__canevas
.
selection_rectangle
)
boutonEllipse
.
config
(
command
=
self
.
__canevas
.
selection_ellipse
)
bouttonCouleur
.
config
(
command
=
self
.
__canevas
.
selection_couleur
)
self
.
__canevas
.
bind
(
"
<Button-1>
"
)
self
.
__canevas
.
bind
(
"
<ButtonRelease-1>
"
,
self
.
ctrl_release_canevas
)
self
.
__deplacement_en_cours
=
FALSE
self
.
__dernier_x
=
0
delf
.
__dernier_y
=
0
self
.
__canevas
.
bind
(
"
<ButtonRelease-1>
"
,
self
.
release_canevas
)
self
.
__canevas
.
bind
(
"
<M1-Motion>
"
,
self
.
drag_canevas
)
def
clic_canevas
(
self
,
event
):
self
.
__dernier_x
=
event
.
x
self
.
__dernier_y
=
event
.
y
def
control_clic_canevas
(
self
,
event
):
self
.
__canevas
.
suppression_forme
(
event
.
x
,
event
.
y
)
def
release_canvas
(
self
,
event
):
if
not
self
.
__deplacement_en_cours
:
self
.
__canvas
.
ajout_forme
(
event
.
x
,
event
.
y
)
else
:
self
.
__canevas
.
fin_deplacement
()
self
.
__deplacement_en_cours
=
False
def
ctrl_release_canevas
(
self
,
event
):
self
.
__canevas
.
delete_forme
(
event
.
x
,
event
.
y
)
if
__name__
==
"
__main__
"
:
fen
=
FenPrincipale
()
fen
.
mainloop
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment