- 论坛徽章:
- 3
|
以前没做过gui编程 ,现在要写一个小应用,用了tkinter,就是 触发按钮时,在界面上显示图片,写到这儿卡住了,图片怎么都不出来- # -*- coding:utf-8 -*-
- from Tkinter import *
- from PIL import Image, ImageTk
- root = Tk()
- image_frame = Frame(root)
- image_file = Image.open("F:\\Pie.jpg")
- im = ImageTk.PhotoImage(image_file)
- image_label = Label(image_frame,image = im).pack(side = LEFT,padx = 5)
- image_frame.pack()
- root.mainloop()
复制代码 这样显示图片是没问题的
然后改由按钮触发,显示图片- # -*- coding:utf-8 -*-
- from Tkinter import *
- from PIL import Image, ImageTk
- root = Tk()
- image_frame = Frame(root)
- def create_image_label():
- image_file = Image.open("F:\\Pie.jpg")
- im = ImageTk.PhotoImage(image_file)
- image_label = Label(image_frame,image = im)
- image_label.grid(row = 3, column = 0, sticky = NW, pady = 8, padx = 20)
- button = Button(image_frame,text='猛击这里',anchor = 'center',command = create_image_label)
- button.grid(row = 2, column = 0, sticky = NW, pady = 8, padx = 20)
- image_frame.pack()
- root.mainloop()
复制代码 怎么猛击都不显示,泪奔了
但是当我在create_image_label()体的最后一句故意加个错误代码时,尽管后台报错,但是图片显示了- # -*- coding:utf-8 -*-
- from Tkinter import *
- from PIL import Image, ImageTk
- root = Tk()
- image_frame = Frame(root)
- def create_image_label():
- image_file = Image.open("F:\\Pie.jpg")
- im = ImageTk.PhotoImage(image_file)
- image_label = Label(image_frame,image = im)
- image_label.grid(row = 3, column = 0, sticky = NW, pady = 8, padx = 20)
- image_label.show()
- button = Button(image_frame,text='猛击这里',anchor = 'center',command = create_image_label)
- button.grid(row = 2, column = 0, sticky = NW, pady = 8, padx = 20)
- image_frame.pack()
- root.mainloop()
复制代码 frame要怎么样做,才能动态更新呢? |
|