본문 바로가기
프로그램

[파이썬] 문제 : tkinter 이미지 축소,확대 (event 인수전달)

by 오디세이99 2023. 5. 22.
728x90
반응형
from tkinter import *
from tkinter.filedialog import *

def func_open():
    filename = askopenfilename(parent=window, filetypes=(("GIF 파일","*.gif"), ("모든 파일","*.*")))
    photo = PhotoImage(file=filename)
    pLabel.configure(image=photo)
    pLabel.image = photo
    
def func_exit():
    window.quit()
    window.destroy()
    
def keyPressHandler(event, photo):
    if (event.keycode==38):         #아스키코드 38 는 ↑ 키
        # print('Up',photo)
        photo_new = photo.zoom(2, 2)    # x, h
        # photo = photo1._PhotoImage__photo.zoom(2,2)
    elif (event.keycode==40):       #아스키코드 40 는 ↓ 키
        # print('Down')
        photo_new = photo.subsample(2, 2)
        # photo = photo1._PhotoImage__photo.subsample(2,2)
    pLabel.configure(image = photo_new)
    pLabel.image = photo_new
    
window = Tk()
window.geometry("500x500")
window.title("몀화 감상하기")

photo = PhotoImage()
pLabel = Label(window, image=photo)
pLabel.pack(expand = 1, anchor=CENTER)

mainMenu = Menu(window)
window.config(menu=mainMenu)
fileMenu = Menu(mainMenu)
mainMenu.add_cascade(label="파일",menu=fileMenu)
fileMenu.add_command(label="파일 열기", command=func_open)
fileMenu.add_separator()
fileMenu.add_command(label="프로그램 종료", command=func_exit)

window.bind("<KeyPress>",lambda event: keyPressHandler(event, pLabel.image))

window.mainloop()

728x90
반응형

댓글