I am making a program for a pet (like Pou, if you know it), and I want to resize the pet when resizing the window. It's made with Tkinter, on a canvas, and his body, ears and other things are polygons (made with c.create_oval
and c.create_polygon
). I already make it so it will resize with the window, but the code is not keeping the pet's ratio and there are other problems. All my code:
from tkinter import HIDDEN, NORMAL, Tk, Canvas
def resize_to(m, curr_n):
curr_m = int(curr_n / 400 * m)
return curr_m
def toggle_eyes():
current_color = c.itemcget(eye_left, 'fill')
new_color = c.body_color if current_color == 'white' else 'white'
current_state = c.itemcget(pupil_left, 'state')
new_state = NORMAL if current_state == HIDDEN else HIDDEN
c.itemconfigure(pupil_left, state=new_state)
c.itemconfigure(pupil_right, state=new_state)
c.itemconfigure(eye_left, fill=new_color)
c.itemconfigure(eye_right, fill=new_color)
def blink():
toggle_eyes()
root.after(250, toggle_eyes)
root.after(3000, blink)
def toggle_pupils():
if not c.eyes_crossed:
c.move(pupil_left, 10, -5)
c.move(pupil_right, -10, -5)
c.eyes_crossed = True
else:
c.move(pupil_left, -10, 5)
c.move(pupil_right, 10, 5)
c.eyes_crossed = False
def toggle_tongue():
if not c.tongue_out:
c.itemconfigure(tongue_tip, state=NORMAL)
c.itemconfigure(tongue_main, state=NORMAL)
c.tongue_out = True
else:
c.itemconfigure(tongue_tip, state=HIDDEN)
c.itemconfigure(tongue_main, state=HIDDEN)
c.tongue_out = False
def cheeky(event):
toggle_tongue()
toggle_pupils()
hide_happy(event)
root.after(1000, toggle_tongue)
root.after(1000, toggle_pupils)
return
def show_happy(event):
if (20 <= event.x <= 350) and (20 <= event.y <= 350):
c.itemconfigure(cheek_left, state=NORMAL)
c.itemconfigure(cheek_right, state=NORMAL)
c.itemconfigure(mouth_happy, state=NORMAL)
c.itemconfigure(mouth_normal, state=HIDDEN)
c.itemconfigure(mouth_sad, state=HIDDEN)
c.happy_level = 10
return
def hide_happy(event):
c.itemconfigure(cheek_left, state=HIDDEN)
c.itemconfigure(cheek_right, state=HIDDEN)
c.itemconfigure(mouth_happy, state=HIDDEN)
c.itemconfigure(mouth_normal, state=NORMAL)
c.itemconfigure(mouth_sad, state=HIDDEN)
return c
def sad():
if c.happy_level == 0:
c.itemconfigure(mouth_happy, state=HIDDEN)
c.itemconfigure(mouth_normal, state=HIDDEN)
c.itemconfigure(mouth_sad, state=NORMAL)
else:
c.happy_level -= 1
root.after(5000, sad)
root = Tk()
root.title("Petey")
c = Canvas(root, width=400, height=400)
c.configure(bg='dark blue', highlightthickness=0)
c.body_color = 'SkyBlue1'
body = c.create_oval(35, 20, 365, 350, outline=c.body_color, fill=c.body_color)
ear_left = c.create_polygon(75, 80, 75, 10, 165, 70, outline=c.body_color, fill=c.body_color)
ear_right = c.create_polygon(255, 45, 325, 10, 320, 70, outline=c.body_color, fill=c.body_color)
foot_left = c.create_oval(65, 320, 145, 360, outline=c.body_color, fill=c.body_color)
foot_right = c.create_oval(250, 320, 330, 360, outline=c.body_color, fill=c.body_color)
eye_left = c.create_oval(130, 110, 160, 170, outline='black', fill='white')
pupil_left = c.create_oval(140, 145, 150, 155, outline='black', fill='black')
eye_right = c.create_oval(230, 110, 260, 170, outline='black', fill='white')
pupil_right = c.create_oval(240, 145, 250, 155, outline='black', fill='black')
mouth_normal = c.create_line(170, 250, 200, 272, 230, 250, smooth=1, width=2, state=NORMAL)
mouth_happy = c.create_line(170, 250, 200, 282, 230, 250, smooth=1, width=2, state=HIDDEN)
mouth_sad = c.create_line(170, 250, 200, 232, 230, 250, smooth=1, width=2, state=HIDDEN)
tongue_main = c.create_rectangle(170, 250, 230, 290, outline='red', fill='red', state=HIDDEN)
tongue_tip = c.create_oval(170, 285, 230, 300, outline='red', fill='red', state=HIDDEN)
cheek_left = c.create_oval(70, 180, 120, 230, outline='pink', fill='pink', state=HIDDEN)
cheek_right = c.create_oval(280, 180, 330, 230, outline='pink', fill='pink', state=HIDDEN)
def update():
w = root.winfo_height()
h = root.winfo_width()
c.coords(body, resize_to(35, h), resize_to(20, w), resize_to(365, h), resize_to(350, w))
c.coords(ear_left, resize_to(75, h), resize_to(80, w), resize_to(75, h), resize_to(10, w), resize_to(165, h), resize_to(70, w))
c.coords(ear_right, resize_to(255, h), resize_to(45, w), resize_to(325, h), resize_to(10, w), resize_to(320, h), resize_to(70, w))
c.coords(foot_left, resize_to(65, h), resize_to(320, w), resize_to(145, h), resize_to(360, w))
c.coords(foot_right, resize_to(250, h), resize_to(320, w), resize_to(330, h), resize_to(360, w))
c.coords(eye_left, resize_to(130, h), resize_to(110, w), resize_to(160, h), resize_to(170, w))
c.coords(pupil_left, resize_to(140, h), resize_to(145, w), resize_to(150, h), resize_to(155, w))
c.coords(eye_right, resize_to(230, h), resize_to(110, w), resize_to(260, h), resize_to(170, w))
c.coords(pupil_right, resize_to(240, h), resize_to(145, w), resize_to(250, h), resize_to(155, w))
c.coords(mouth_normal, resize_to(170, h), resize_to(250, w), resize_to(200, h), resize_to(272, w), resize_to(230, h), resize_to(250, w))
c.coords(mouth_happy, resize_to(170, h), resize_to(250, w), resize_to(200, h), resize_to(282, w), resize_to(230, h), resize_to(250, w))
c.coords(mouth_sad, resize_to(170, h), resize_to(250, w), resize_to(200, h), resize_to(232, w), resize_to(230, h), resize_to(250, w))
c.coords(tongue_main, resize_to(170, h), resize_to(250, w), resize_to(230, h), resize_to(290, w))
c.coords(tongue_tip, resize_to(170, h), resize_to(285, w), resize_to(230, h), resize_to(300, w))
c.coords(cheek_left, resize_to(70, h), resize_to(180, w), resize_to(120, h), resize_to(230, w))
c.coords(cheek_right, resize_to(280, h), resize_to(180, w), resize_to(330, h), resize_to(230, w))
root.update()
root.after(1, update)
c.pack(fill="both", expand=True)
c.bind('<Motion>', show_happy)
c.bind('<Leave>', hide_happy)
c.bind('<Double-1>', cheeky)
c.happy_level = 10
c.eyes_crossed = False
c.tongue_out = False
root.after(1000, update)
root.after(1000, blink)
root.after(5000, sad)
root.mainloop()
Is there a math formula, a function or anything?
EDIT: thanks for your answer, but how do I center my pet?
CodePudding user response:
Does modifying your update method like this fixes your ratio problem ?
def update():
w = root.winfo_height()
h = root.winfo_width()
new_size=w
if h < w:
new_size=h
c.coords(body, resize_to(35, new_size), resize_to(20, new_size), resize_to(365, new_size), resize_to(350, new_size))
c.coords(ear_left, resize_to(75, new_size), resize_to(80, new_size), resize_to(75, new_size), resize_to(10, new_size), resize_to(165, new_size), resize_to(70, new_size))
c.coords(ear_right, resize_to(255, new_size), resize_to(45, new_size), resize_to(325, new_size), resize_to(10, new_size), resize_to(320, new_size), resize_to(70, new_size))
c.coords(foot_left, resize_to(65, new_size), resize_to(320, new_size), resize_to(145, new_size), resize_to(360, new_size))
c.coords(foot_right, resize_to(250, new_size), resize_to(320, new_size), resize_to(330, new_size), resize_to(360, new_size))
c.coords(eye_left, resize_to(130, new_size), resize_to(110, new_size), resize_to(160, new_size), resize_to(170, new_size))
c.coords(pupil_left, resize_to(140, new_size), resize_to(145, new_size), resize_to(150, new_size), resize_to(155, new_size))
c.coords(eye_right, resize_to(230, new_size), resize_to(110, new_size), resize_to(260, new_size), resize_to(170, new_size))
c.coords(pupil_right, resize_to(240, new_size), resize_to(145, new_size), resize_to(250, new_size), resize_to(155, new_size))
c.coords(mouth_normal, resize_to(170, new_size), resize_to(250, new_size), resize_to(200, new_size), resize_to(272, new_size), resize_to(230, new_size), resize_to(250, new_size))
c.coords(mouth_happy, resize_to(170, new_size), resize_to(250, new_size), resize_to(200, new_size), resize_to(282, new_size), resize_to(230, new_size), resize_to(250, new_size))
c.coords(mouth_sad, resize_to(170, new_size), resize_to(250, new_size), resize_to(200, new_size), resize_to(232, new_size), resize_to(230, new_size), resize_to(250, new_size))
c.coords(tongue_main, resize_to(170, new_size), resize_to(250, new_size), resize_to(230, new_size), resize_to(290, new_size))
c.coords(tongue_tip, resize_to(170, new_size), resize_to(285, new_size), resize_to(230, new_size), resize_to(300, new_size))
c.coords(cheek_left, resize_to(70, new_size), resize_to(180, new_size), resize_to(120, new_size), resize_to(230, new_size))
c.coords(cheek_right, resize_to(280, new_size), resize_to(180, new_size), resize_to(330, new_size), resize_to(230, new_size))
root.update()
root.after(1, update)