본문 바로가기
프로그램

[파이썬] UI (Tkinter) Popup Window / Image View

by 오디세이99 2022. 9. 22.
728x90
반응형

Button의 처음 보이지 않도록 하기(pack_forget)

보이지 않는 Button 보이기(pack)

Popup 윈도우 띄우기 (Toplevel)

Popup 윈도우 canvas에 image 보이기(pack(expand=YES, fill=BOTH) , mainloop  : 이 코드 없으면 image 가 안보임)

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("750x400")

def open_popup():
    top= Toplevel(win)
    top.geometry("400x400")
    top.title("Child Window")

    canvas = Canvas(top, width=230, heigh=230, bg='white', bd=2)
    canvas.pack(expand=YES,fill=BOTH)   # 이 코드가 있으면 popup에서 image가 보이지 않음
    
    img=PhotoImage(file="e:/RnD/지식IN/PLCT00005467.png")    
    canvas.create_image(100,100, image=img, anchor='nw')

    canvas.create_text(10,10, anchor="nw", text="Hello, world")
    
    top.mainloop()   # 이 코드가 있으면 popup에서 image가 보이지 않음


def hide(x):
    x.pack_forget()    

    
def show(x):
    x.pack()    
    

Label(win, text=" Click the Button to Open the Popup Window", font=('Helvetica 14 bold')).pack(pady=20)

bt1 = ttk.Button(win, text= "Open", command=open_popup)
bt1.pack_forget()

bt2 = ttk.Button(win, text= "Show button", command=lambda: show(bt1))
bt2.pack()


win.mainloop()

 

show button을 클릭하면 'Open' 버튼이 보입니다.

 

Popup 윈도우가 Open 되고, image가 Canvas에 보입니다.

728x90
반응형

댓글