Draw pumpkins
Clement_7057
Posted messages
5
Status
Member
-
Clement_7057 -
Clement_7057 -
Here's the translation into natural English:
Hi I have this code that is supposed to draw pumpkins, but for pumpkins of size 1 and 2 it doesn’t work. I’ve been stuck on this for 4 hours and can’t find a way to fix it. Here’s the code:
from tkinter import * from random import * tailles=[0,50,100,200] def dessine_citrouille(x,y,taille): can.delete(ALL) global tailles if taille==1: taille=tailles[1] elif taille==2: taille=tailles[2] else: taille=tailles[3] #bouche can.create_polygon(x-taille/2, y+taille/2, x-1/4*taille, y+taille/2, x-3/8*taille, y+3/4*taille, outline="black", fill="yellow") can.create_polygon(x-taille/4, y+taille/2, x, y+taille/2, x-1/8*taille, y+3/4*taille, outline="black", fill="yellow") can.create_polygon(x, y+taille/2, x+taille/4, y+taille/2, x+1/8*taille, y+3/4*taille, outline="black", fill="yellow") can.create_polygon(x+taille/4, y+taille/2, x+taille/2, y+taille/2, x+3/8*taille, y+3/4*taille, outline="black", fill="yellow") #eye right can.create_polygon(x-taille/2, y-taille/8, x-3/4*taille, y-taille/4, x-1/4*taille, y-taille/4,outline="black", fill="black") #eye left can.create_polygon(x+taille/2, y-taille/8, x+3/4*taille, y-taille/4, x+1/4*taille, y-taille/4,outline="black", fill="black") #crest of the pumpkin can.create_polygon(x, y-taille, x+1/8*taille, y-9/8*taille, x-1/8*taille, y-9/8*taille,outline="black", fill="green") #left nostril can.create_oval(x-1/8*taille, y+1/7*taille, x-1/7*taille, y+1/8*taille,outline='black',fill='orange') #right nostril can.create_oval(x+1/8*taille, y+1/7*taille, x+1/7*taille, y+1/8*taille,outline='black',fill='orange') fen=Tk() w,h=fen.winfo_screenwidth(),fen.winfo_screenheight() can=Canvas(fen,height=h,width=w,bg='white') can.pack() b=Button(can,text='citrouille', command=dessine_citrouille(randint(10,w),randint(10,h),randint(1,3)), bg='orangered') b.place(x=1,y=640) b2=Button(can,text='quitter',command=fen.destroy,bg='black',fg='white') b2.place(x=1,y=560) fen.mainloop()Also the button doesn’t work. Thanks for your help.
3 answers
-
Hello,
Please use the < code python> tag around the copied/pasted source code, otherwise it is unreadable.
Besides, "it doesn't work" is very vague. Is there an error message? An incorrect result? No result at all?
Xavier -
Good evening,
And furthermore the button doesn't work
Your problem is exactly there, and it's a recurring mistake by beginners.
b=Button(can,text='citrouille', command=dessine_citrouille(randint(10,w),randint(10,h),randint(1,3)), bg='orangered')
The button's command will be executed when the script loads.
You can modify the dessine_citrouille function and place the randint inside, which does not change the flow of the script.
def dessine_citrouille(): x = randint(10, w) y = randint(10,h) taille = choice((50,100,200)) # rest of the function code
And so for the button.
b=Button(can,text='citrouille', command=dessine_citrouille, bg='orangered')
-
No error messages and no result.
But finally after 2 more hours, I created a drawing function that calls draw pumpkin and it works
Thanks anyway for your help.