Изменение размера изображения не работает на PhotoImage в tkinter

1

Я пытаюсь сделать код для отображения меньших векторных изображений ветра поверх фонового изображения карты. Единственная проблема заключается в том, что векторы ветра больше, чем требуется, и я хочу уменьшить их. Я попробовал subsample() но по какой-то причине это не работает, и изображение остается такого же размера.

Предварительный код для этого заключается в следующем:

from tkinter import *
root = Tk()
root.title("Click me!")
def next_image(event):
    #toggle between image2 and image3
    global toggle_flag
    global x, y, photo2, photo3
    if toggle_flag == TRUE:
        # display photo2, same center x, y
        canvas1.create_image(x, y, image=photo2)
        toggle_flag = False
    else:
        canvas1.create_image(x, y, image=photo3)
        toggle_flag = True

toggle_flag = True
# pick three GIF image files you have in your working directory
# image1 is larger than image2 and image3
image1 = "Map.png"
photo1 = PhotoImage(file=image1)
image2 = "icons8-wind-speed-43-47-50.png"
photo2 = PhotoImage(file=image2)
photo2.subsample(50)
image3 = "icons8-wind-speed-103-107-50.png"
photo3 = PhotoImage(file=image3)
photo3.subsample(50)
# make canvas the size of image1/photo1
width1 = photo1.width()
height1 = photo1.height()
canvas1 = Canvas(width=width1, height=height1)
canvas1.pack()
# display photo1, x, y is center (anchor=CENTER is default)
x = (width1)/2.0
y = (height1)/2.0
canvas1.create_image(x, y, image=photo1)
canvas1.bind('<Button-1>', next_image)  # bind left mouse click
root.mainloop()

Я использую Python 3.6 в Windows 10 и пакет tkinter по умолчанию. Пожалуйста помоги.

Результат выглядит следующим образом; Изображение 174551

Теги:
python-3.x
image
tkinter

1 ответ

0
Лучший ответ

Это ответ, который я пришел принять. Благодаря j_4321.

from tkinter import *
root = Tk()
root.title("Click me!")
def next_image(event):
    #toggle between image2 and image3
    global toggle_flag
    global x, y, photo2, photo3
    if toggle_flag == TRUE:
        # display photo2, same center x, y
        canvas1.create_image(x, y, image=photo2)
        toggle_flag = False
    else:
        canvas1.create_image(x, y, image=photo3)
        toggle_flag = True

toggle_flag = True
# pick three GIF image files you have in your working directory
# image1 is larger than image2 and image3
image1 = "Map.png"
photo1 = PhotoImage(file=image1)
image2 = "icons8-wind-speed-43-47-50.png"
photo2 = PhotoImage(file=image2)
photo2 = photo2.subsample(50)
image3 = "icons8-wind-speed-103-107-50.png"
photo3 = PhotoImage(file=image3)
photo3 = photo3.subsample(50)
# make canvas the size of image1/photo1
width1 = photo1.width()
height1 = photo1.height()
canvas1 = Canvas(width=width1, height=height1)
canvas1.pack()
# display photo1, x, y is center (anchor=CENTER is default)
x = (width1)/2.0
y = (height1)/2.0
canvas1.create_image(x, y, image=photo1)
canvas1.bind('<Button-1>', next_image)  # bind left mouse click
root.mainloop()

Ещё вопросы

Сообщество Overcoder
Наверх
Меню