在使用tkinter进行for循环创建按钮时遇到麻烦。 (蟒蛇) [英] Having trouble creating buttons with a for-loop through tkinter. (Python)
问题描述
这是我的代码: / p>
film = films_dict ['filmsoptv'] [film]#<<< tkinterGUI
Buttons = ['Button1','Button2','Button3','Button4','Button5']
lijstnummers = [1,2,3,4,5]
电影,i,j在拉链((films_dict ['filmsoptv'] [电影]),(lijstnummers),(按钮)):
image_bytes = urlopen(film [cover]) .read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
j = Button(window, = close,height = 296,width = 200,image = tk_image)
j.grid(row = 0,column = i)
films_dict包含5个子词典,我通过在for循环中调用它来滚动以访问子字典的封面url。 film_dict每天都在变化,所以我不能使用被动的url。
任何人可以帮助我创建5个按钮而不是一个?
只是一个猜测,但我认为除了最后一个图像之外的所有图片都是垃圾收集,因为只有对最后一张图片的引用( tk_image
仍然指向一个循环后)。由于某些原因,在Button中或Label中使用的映像不被作为垃圾收集器的参考。尝试将列表或字典中的图像存储到所有的引用,然后它应该可以工作。
此外,您似乎想添加通过将其分配给 j
列表中的按钮按钮
。这不行。更好地将按钮
初始化为空列表,将新的按钮添加到该列表中。尝试这个(未测试):
images = []
buttons = []
for i,电影在枚举中(film_dict ['filmsoptv'] [film],1):
image_bytes = urlopen(film [cover])read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
j = Button(window,command = close,height = 296,width = 200,image = tk_image)
j.grid(row = 0,column = i)
images.append(tk_image)
buttons.append(j)
Im trying to create multiple buttons through a loop with TKinter, but when i run the script, 5 button-like objects get created, but only the last behaves as a button.With TKimage, im trying to overlay a picture that comes from an url inside a dictionary on each button. But the dictionary contains 5 images, and only the last button turns into an actual button, and has the final of the 5 images on it.
This is my code:
film = films_dict['filmsoptv']["film"] #<<<< voor plaatjes films in TkinterGUI
Buttons = ['Button1','Button2','Button3','Button4','Button5']
lijstnummers = [1,2,3,4,5]
for film, i, j in zip((films_dict['filmsoptv']["film"]),(lijstnummers),(Buttons)):
image_bytes = urlopen(film["cover"]).read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
j = Button(window,command=close,height=296,width=200,image=tk_image)
j.grid(row=0, column=i)
films_dict contains 5 sub-dictionaries which i, by calling it in a for loop, roll through to access the sub-dictionary's cover-url. The films_dict changes every day, so i can't use passive url's.
Anyone that can help me create 5 buttons instead of one?
Just a guess, but I think all but the last image are garbage collected, since there is only a reference to the last image left (tk_image
still points to that one after the loop). For some reason, the image being used in the Button, or in a Label does not count as a reference for the garbage collector. Try storing references to all the images in a list or dictionary, then it should work.
Also, it seems like you want to add the Button to the list Buttons
by assigning it to j
. This will not work, though. Better initialize Buttons
as an empty list and append
the new Button to that list. Try this (not tested):
images = []
buttons = []
for i, film in enumerate(films_dict['filmsoptv']["film"], 1):
image_bytes = urlopen(film["cover"]).read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
j = Button(window, command=close, height=296, width=200, image=tk_image)
j.grid(row=0, column=i)
images.append(tk_image)
buttons.append(j)
这篇关于在使用tkinter进行for循环创建按钮时遇到麻烦。 (蟒蛇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!