你可以请帮助我解决与代码的问题,它不显示在从mysql数据库检索的临时目录中保存的treeview中的图像。
def get_imgs():
global td
global img_ref
img_ref = []
td = tempfile.mkdtemp()
cur.execute("SELECT * FROM Candidates")
r = cur.fetchall()
for i in r:
b_dt = i[1]
img_dt = Image.open(io.BytesIO(b_dt))
img_dt.save(td+'/'+str(i[0])+'.png')
img_ref.append(img_dt)
def insert_val():
get_imgs()
count=0
cur.execute("SELECT * FROM Candidates")
data = cur.fetchall()
for record in data:
path = td+'/'+str(record[0])+'.png'
img = ImageTk.PhotoImage(Image.open(path))
can_table.insert(parent='', index='end', iid=count, open=True, text='', image=img, values=(record[0]))
count += 1
treestyle = ttk.Style()
treestyle.theme_use('default')
treestyle.configure("Treeview", background=bg_color, foreground=text_color, fieldbackground=bg_color,
borderwidth=0, font=('Koulen', 26), rowheight=80)
treestyle.configure('Treeview.Heading', font=('Koulen', 22), foreground='white',
fieldbackground=bg_color, background=bg_color)
treestyle.map('Treeview', background=[('selected', bg_color)],
foreground=[('selected', selected_color)])
can_table = ttk.Treeview(fr, selectmode='browse', height=5)
can_table.pack(expand=True, fill='both')
can_table['columns'] = ("StudentName")
can_table['show'] = ('tree headings')
can_table.column("#0", anchor=CENTER, width=200)
can_table.column("StudentName", anchor=CENTER, width=400)
can_table.heading('#0', text='#')
can_table.heading('StudentName', text='Student Name')
can_table.tag_configure('oddrow', background='#161717')
can_table.tag_configure('oddrow', background='#393C3C')
字符串
我基本上尝试直接将blob数据转换为图像并插入到treeview中,但这也没有给予好的结果!
图像用于显示,如果它是一个单一的行,但不是多个图像,现在不会发生!
我主要使用自定义tkinter为gui和外观..
1条答案
按热度按时间t40tm48m1#
实际上这是一个我没有注意到的非常小的愚蠢的错误!该范围内的前一个图像的引用被垃圾收集,导致在treeview中不显示任何图像!
所以我只是引用了get_imgs()中的图像,并将其保存在一个列表中,解决了这个问题。
谢谢你