python tkinter事件无法显示图像

时间:2019-12-31 02:58:55

标签: python image tkinter bind

按箭头键(向左和向右)时,我试图显示不同的文本和图像。我的代码成功显示文本,但没有图像。以下是我的代码:

def show_url(event, tk_win, message, pic):

    # show message on top
    print(message)
    lab = tk.Label(tk_win, text=message)
    lab.grid(row=0, column=0)

    # display image in bottom
    print('pic already input')
    canvas = tk.Canvas(tk_win, width=600, height=600)
    image = Image.open(pic)
    image = ImageTk.PhotoImage(image)
    canvas.create_image(0, 0, anchor=tk.NW, image=image)
    canvas.grid(row=1, column=0)

url1 = 'www.google.com'
url2 = 'www.youtube.com'

pic1 = 'input my pic1'
pic2 = 'input my pic2'

# start tkinter
win = tk.Tk()  

# arguements input in function 
arg1 = win, url1, pic1
arg2 = win, url2, pic2

# press left and right arrow keys to start event
win.bind('<Left>',  lambda event, arg=arg1: show_url(event, *arg))
win.bind('<Right>', lambda event, arg=arg2: show_url(event, *arg))

win.mainloop()

这里有什么问题吗?

0 个答案:

没有答案