Python 在tk按钮中使用图像时,按钮消失

Python 在tk按钮中使用图像时,按钮消失,python,tkinter,Python,Tkinter,这是我的代码,你可以忽略其中的大部分,但只能看到最后一部分# 将tkinter作为tk导入 从PIL导入ImageTk,图像 def bresize_和_加载(路径): 全球bwidth,bhweight im=图像。打开(路径) bwidth,bHweight=im.size 调整大小=bresizemode(im、bwidth、bheight) 宽度、高度=已调整大小。尺寸 返回图像tk.PhotoImage(已调整大小) def bresizemode(im、宽度、高度): 如果高度/宽度

这是我的代码,你可以忽略其中的大部分,但只能看到最后一部分#

将tkinter作为tk导入
从PIL导入ImageTk,图像
def bresize_和_加载(路径):
全球bwidth,bhweight
im=图像。打开(路径)
bwidth,bHweight=im.size
调整大小=bresizemode(im、bwidth、bheight)
宽度、高度=已调整大小。尺寸
返回图像tk.PhotoImage(已调整大小)
def bresizemode(im、宽度、高度):
如果高度/宽度>=比率:
返回im.resize((int(圆形((宽度/高度)*可用高度)),可用高度),
图像(反别名)
如果高度/宽度<比率:
返回im.resize((可用宽度),(int(圆形((高度/宽度)*可用宽度)),
图像(反别名)
root=tk.tk()
root.state(“缩放”)
根。可调整大小(宽度=假,高度=假)
frame=tk.frame(根)
frame.grid(行=0,列=0,粘性='nsew')
tk.Grid.rowconfigure(根,0,权重=1)
tk.Grid.columnconfigure(根,0,权重=1)
行=4
列=5
对于范围内的ro(行):
tk.Grid.rowconfigure(帧,ro,重量=1)
对于范围内的co(列):
tk.Grid.columnconfigure(帧,co,重量=1)
root.update()
f_width=frame.winfo_width()
f_height=frame.winfo_height()
booklistbutton=[]
对于范围内的i(行):
对于范围内的e(列):
b按钮=tk.按钮(框架,高度=int(f_高度/行),
宽度=整数(f_宽度/列))
bbutton.grid(行=i,列=e)
booklistbutton.append(bbutton)
root.update()
可用宽度=booklistbutton[0]。winfo宽度()
可用高度=booklistbutton[0]。winfo高度()
比率=可用高度/可用宽度
#这是图像路径
路径='sample.jpg'
imm=[]
#如果它是范围(20)(刚好=行*列)或小于列(这里是5),则工作正常
对于范围(20)内的i:
imm.append(bresize_和_load(路径))
booklistbutton[i].config(image=imm[i])
root.mainloop()
我的问题是,如果您在按钮中加载图像,但图像按钮的数量不小于column或等于row*column,则图像按钮将消失

当范围等于行*列(20)时:

当范围为6时:

这对我来说很奇怪,有人知道吗

另外,若你们不设置按钮的宽度和高度,它们也不会消失。但按钮将比图像稍大。

(代表OP发布解决方案)


我自己发现了问题,问题是当我设置按钮的大小时,它是chr大小,但当我加载图像时,它会变为像素大小,在相同的大小编号下,chr大小越来越大,因此图像按钮变得太小而无法显示。

抱歉,它应该做什么?@Dova它应该创建许多带有图像的按钮(不同的图像)。
import tkinter as tk
from PIL import ImageTk, Image

def bresize_and_load(path):
    global bwidth, bheight
    im = Image.open(path)
    bwidth,bheight = im.size    
    resized = bresizemode(im, bwidth, bheight)    
    width,height = resized.size    
    return ImageTk.PhotoImage(resized)

def bresizemode(im, width, height):
    if height / width >= ratio:
        return im.resize((int(round((width / height) * usable_height)), usable_height), 
                  Image.ANTIALIAS)
       
    if height / width < ratio:
        return im.resize((usable_width, (int(round((height / width) * usable_width)))), 
                  Image.ANTIALIAS)

root = tk.Tk()
root.state("zoomed")
root.resizable(width=False, height=False)

frame = tk.Frame(root)

frame.grid(row = 0, column = 0, sticky = 'nsew')

tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)

row = 4
column = 5
for ro in range(row):
    tk.Grid.rowconfigure(frame, ro, weight=1)
for co in range(column):
    tk.Grid.columnconfigure(frame, co, weight=1)

root.update()
f_width  = frame.winfo_width()
f_height = frame.winfo_height()

booklistbutton = []
for i in range(row):
    for e in range(column):
        bbutton = tk.Button(frame, height = int(f_height / row), 
                            width = int(f_width / column))
        bbutton.grid(row = i, column = e)
        booklistbutton.append(bbutton)

root.update()
usable_width = booklistbutton[0].winfo_width()
usable_height = booklistbutton[0].winfo_height()
ratio = usable_height / usable_width

#here is image path
path = 'sample.jpg'
imm = []

#if it is range(20)(just = row * column) or less than column(here is 5), it work fine
for i in range(20):    
    imm.append(bresize_and_load(path))
    booklistbutton[i].config(image = imm[i])

root.mainloop()