mauro50
Messages postés3Date d'inscriptionvendredi 26 mars 2010StatutMembreDernière intervention 2 mai 2010
-
1 mai 2010 à 12:16
mauro50
Messages postés3Date d'inscriptionvendredi 26 mars 2010StatutMembreDernière intervention 2 mai 2010
-
2 mai 2010 à 14:42
Bonjour,je crée actuellement un mastermind en python dans le cadre d'un projet. Je voudrais savoir si il est possible de changer la couleur du fond d'écran de ma fenêtre tkinter, et si il est possible de créer des bouton rond
Je vous laisse mon code:
def mastermind():
global ligneCourante,billeCourante,combiOrdi
ligneCourante,billeCourante=0,0
combiOrdi=detOrdi(couleur)
numLigne[ligneCourante].configure(text=str(ligneCourante+1))
resultat[ligneCourante].configure(text="Cliquez sur une couleur pour commencer.",font=("Comic sans ms",11))
def testerChoix(essai):
global ligneCourante,billeCourante
boutonOk[ligneCourante].configure(state=DISABLED)
boutonEffacer[ligneCourante].configure(state=DISABLED)
if testCombi(combiOrdi,essai)==True:
resultat[ligneCourante].configure(text="Bravo !! Victoire en "+str(ligneCourante+1)+" coups !")
elif ligneCourante==11:
resultat[ligneCourante].configure(text="Perdu !")
for i in range(4):
ligneSolution.itemconfigure(billeSolution[i],fill=combiOrdi[i])
else:
approx=detApprox(combiOrdi,essai)
resultat[ligneCourante].configure(text=str(approx[0])+" bien placée(s), "+str(approx[1])+" mal placée(s)",font=("comic sans ms",10))
ligneCourante+=1
billeCourante=0
numLigne[ligneCourante].configure(text=ligneCourante+1)
def detOrdi(couleur):
combiAleatoire=[""]*4
for i in range(4):
combiAleatoire[i]=couleur[random.randint(0,7)]
return combiAleatoire
for i in range(4):
lRes[i]=listeRes[i]
cOrdi[i]=combiOrdi[i]
for i in range(4):
if lRes[i]==cOrdi[i] and lRes[i]!="":
bP+=1
cOrdi[i]=""
lRes[i]=""
for i in range(4):
for x in range(4):
if lRes[i]!="" and lRes[i]==cOrdi[x]:
mP+=1
cOrdi[x]=""
lRes[i]=""
break
return [bP,mP]
def effacerLigne():
global billeCourante
boutonOk[ligneCourante].configure(state=DISABLED)
if billeCourante==1:
boutonEffacer[ligneCourante].configure(state=DISABLED)
billeCourante-=1
ligne[ligneCourante].itemconfigure(bille[ligneCourante][billeCourante],fill="grey")
def changeCouleur(i):
global billeCourante
if billeCourante<4:
ligne[ligneCourante].itemconfigure(bille[ligneCourante][billeCourante],fill=couleur[i])
essai[billeCourante]=couleur[i]
billeCourante+=1
boutonEffacer[ligneCourante].configure(state=ACTIVE)
if billeCourante==4:
boutonOk[ligneCourante].configure(state=ACTIVE)
def quitter():
fram.quit()
fram.destroy()
def rejouer():
boutonOk[ligneCourante].configure(state=DISABLED)
boutonEffacer[ligneCourante].configure(state=DISABLED)
for i in range(4):
ligneSolution.itemconfigure(billeSolution[i],fill="grey")
for i in range(12):
resultat[i].configure(text="")
numLigne[i].configure(text="")
for x in range(4):
ligne[i].itemconfigure(bille[i][x],fill="grey")
mastermind()
ligneSolution=Canvas(fram,bg="DimGrey",height=20,width=76)
ligneSolution.grid(row=14,column=2)
billeSolution=[""]*4
x0,y0,x1,y1=3,3,20,20
for i in range(4):
billeSolution[i]=ligneSolution.create_oval(x0,y0,x1,y1,fill="grey19")
x0+=19
x1+=19
boutonCouleur=[""]*8
for i in range(8):
boutonCouleur[i]=Button(fram,bg=couleur[i],height=1,width=2,command=lambda arg=i: changeCouleur(arg))
boutonCouleur[i].grid(row=15,column=5+i)
didoux95
Messages postés845Date d'inscriptionmardi 25 avril 2006StatutMembreDernière intervention 1 août 20172 1 mai 2010 à 23:23
Salut,
Oui, tu peux tout à fais changer la couleur du fond de ton application. Il te suffit d'utiliser l'attribut "bg" (aussi appelé "background"). Par exemple : (tk.)myFrame.configure(bg="red") ou encore tk.)myFrame.configure(bg="#FF0000")
En ce qui concerne des boutons rond.. Je ne pense pas que Tkinter en contienne par défaut.. En revanche tu peux les créer ! Un tk.Canvas de la taille de ton bouton, tu y dessines la forme que tu veux, et tu lui ajoutes un "bind("", myFunction)" pour qu'il se passe quelque chose quand on clique dessus (ce n'est qu'une solution parmis, surement beaucoup d'autre ..).
mauro50
Messages postés3Date d'inscriptionvendredi 26 mars 2010StatutMembreDernière intervention 2 mai 20101 2 mai 2010 à 14:03
Merci pour ton aide, cependant sa m'a l'air assez compliqué pour les bouton vu que je dois crée 8 boutons,je sais pas si il existe un moyen assez simple pour gagner du temps, sinon je me contenteré de mes boutons carrés ^^
didoux95
Messages postés845Date d'inscriptionmardi 25 avril 2006StatutMembreDernière intervention 1 août 20172 2 mai 2010 à 14:31
Salut,
L'explication est plus longue que le codage..
Par exemple :
import Tkinter as tk #ou "import tkinter as tk" selon ta version de python
myButton1 = tk.Canvas(root)
myButton1.create_oval(5, 5, 15, 15, fill='white')
myButton1.bind("", lambda event: myFunction(event))
myButton1.place(x=15, y=15, width=20, height=20)
myButton2 = tk.Canvas(root)
myButton2.create_oval(5, 5, 15, 15, fill='white')
myButton2.bind("", lambda event: myFunction(event))
myButton2.place(x=45, y=45, width=20, height=20)
def myFunction (event) :
#Ca t'affiche le composants sur lequel l'utilisateur a cliqué ..
#Après à toi de choisir ce qu'il faut faire suivant le bouton cliqué !
print(event.widget)
Et c'est tout ! Enfin .. c'est tout pour 2 boutons (: