2
我一直在努力獲得一個簡單的Tkinter畫布,使用create_image顯示圖像。我讀過很多線程,說你需要在任何函數或類之外創建對象的引用,否則圖像對象將被垃圾回收。不幸的是,我仍然無法得到這個工作。以下是我的代碼。忽略所有的顏色 - 我用它們來說明框架和畫布在窗戶上的位置。Tkinter create_image
-Kirk
import Tkinter as tk
from PIL import Image
from PIL import ImageTk
imageList = []
image = Image.open('len_std.jpg')
#event handlers
def hit_sel_click():
imageList = []
test_image = ImageTk.PhotoImage(image)
imageList.append(cnv_hits.create_image(0,0,
image=test_image))
#start root
root = tk.Tk()
root.title('SimView')
root.resizable(width=False, height=False)
#target/control variables
hit_sel = tk.StringVar() #holds radio button with activity level
#build GUI
frm_hits = tk.Frame(root, height=800, width=200, bg='#FF0000')
frm_hits.grid(rowspan=3, sticky=tk.W+tk.N+tk.S+tk.E)
tk.Label(frm_hits, text='Activity:').grid()
tk.Radiobutton(frm_hits, text='Low', variable=hit_sel, value='Low',
command=hit_sel_click).grid(sticky=tk.W)
tk.Radiobutton(frm_hits, text='Medium', variable=hit_sel, value='Medium',
command=hit_sel_click).grid(sticky=tk.W)
tmp = tk.Radiobutton(frm_hits, text='High', variable=hit_sel,value='High',
command=hit_sel_click)
tmp.grid(sticky=tk.W)
tmp.select()
frm_hit_list = tk.Frame(frm_hits, bg='#002288')
frm_hit_list.grid(sticky=tk.W+tk.N+tk.E+tk.S)
scrl_hits = tk.Scrollbar(frm_hit_list, orient=tk.VERTICAL)
scrl_hits.grid(row=0, column=1, sticky=tk.N+tk.S)
cnv_hits = tk.Canvas(frm_hit_list, bg='#888800',width=200, height=200,
yscrollcommand=scrl_hits.set)
cnv_hits.grid(row=0, column=0, sticky=tk.W+tk.N+tk.E+tk.S)
scrl_hits.config(command=cnv_hits.yview)
root.mainloop()
謝謝!我創建了一個參考,他是錯誤的變量。沒有更多的深夜編碼給我。 8^d – KirkDCO