我有一个大约90个图像的路径列表,现在我想把它们全部放在canva上,但是如果我使用
from tkinter import *
def createCanvaImages(paths):
paths = ['list with the paths']
mainWin = Tk()
canva = Canvas(mainWin, width = 900, height = 300).pack()
for x in range(0, len(paths),):
if x <= 30: #not sure if this places only 30 in one row
y=x/3
elif x > 30
y=(x+24)/3
elif x >= 60
y=(x+48)/3
img = PhotoImage(file = paths[x])
canva.create_image(x+24, y, image = img)
mainWin.mainloop()
它只显示最后一个路径的图像
现在显示画布上的所有图像,如果画布不在框架中(感谢小说),但如果canva在框架中则不起作用
from tkinter import *
def createImagePaths(dct):
paths=[]
for i in range(len(masteries)):
if dct.get(masteries[i]) == 0:
file = masteries[i]+'.png'
path = os.path.join(path_gray, file)
paths.append(path)
#create canvas image fnc
if dct.get(masteries[i]) != 0:
file = masteries[i]+'.png'
path = os.path.join(path_colored, file)
paths.append(path)
return createCanvaImages(paths)
def createCanvaImages(paths):
img_refs = []
canva = Canvas(masteryFrame, height = 400).pack()
for i, path in enumerate(paths):
col,row = divmod(i,30)
img = PhotoImage(file=path)
canva.create_image( row*24, col*24, image = img, anchor = 'nw')
img_refs.append(img)
root = Tk()
mainFrame = Frame(root)
mainFrame.grid(column=0,row=0, sticky=(N,W,E,S))
masteryFrame = Frame(root)
masteryFrame.grid(row=1,column=0, sticky=(N,W,E,S))
root.mainloop()
您需要保存图像引用。最简单的方法就是把它们添加到一个列表中。作为猜测:
另外,确保不要将小部件初始化和布局放在同一行上。瞧,千万别这样:
Widget(master).pack()
。总是把它们分开放。你知道吗你也应该很快学习OOP和类。像这样使用函数来构建UI会很快变得非常混乱和错误。你知道吗
相关问题 更多 >
编程相关推荐