简体   繁体   English

在 tk Button 中使用图像时,Button 消失

[英]When use image in tk Button, Button disappear

Here is my code, you can ignore most of them but only see the last part which have #这是我的代码,您可以忽略其中的大部分,但只能看到具有 # 的最后一部分

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()

My question is, if you load image in button, but the number of imaged buttons is not less than column or equal row * column, the imaged buttons will disappear.我的问题是,如果您在按钮中加载图像,但图像按钮的数量不小于列或等于行 * 列,则图像按钮将消失。

When range equal row * column(20):当范围等于行 * 列(20)时:

http://i.imgur.com/XEjLH0n.png

When range is 6:当范围为 6 时:

http://i.imgur.com/kSO6MQw.png

This is weird for me, does anyone have any idea?这对我来说很奇怪,有人知道吗?

Also, if you do not set button's width and height, they won't disappear.此外,如果您不设置按钮的宽度和高度,它们也不会消失。 But buttons will little bigger than images.但是按钮不会比图像大一点。

(Posted solution on behalf of the OP) . (代表OP发布解决方案)

I find the problem by myself, the problem is when I set the Buttons' size, it is chr size, but when I load a image, it change to pixel size, and at the same size number, chr size is bigger and bigger than pixel size, so the imaged button become too small to show.我自己发现了这个问题,问题是当我设置Buttons的大小时,它是chr大小,但是当我加载图像时,它变为像素大小,并且在相同的大小数字下,chr大小越来越大像素大小,因此图像按钮变得太小而无法显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM