Python 如何使用按钮调整按钮内容的大小

Python 如何使用按钮调整按钮内容的大小,python,image,button,tkinter,Python,Image,Button,Tkinter,我有一个程序,可以在调整窗口大小时调整按钮大小,但图像不会随着按钮调整大小 这是我的代码: l = Label(window, image=img).grid(row=0, column=0, rowspan=3, sticky='nesw') con = Frame(window).grid(row=0, column=1, sticky='nesw') nextImg = PhotoImage(file='nextImg.png') lastImg = PhotoImage(

我有一个程序,可以在调整窗口大小时调整按钮大小,但图像不会随着按钮调整大小

这是我的代码:

l = Label(window, image=img).grid(row=0, column=0, rowspan=3, sticky='nesw')

con = Frame(window).grid(row=0, column=1, sticky='nesw')

nextImg    = PhotoImage(file='nextImg.png')
lastImg    = PhotoImage(file='lastImg.png')
ok = PhotoImage(file="ok".png')

nextCam = Button(con, image=nextImg,    command=nxt, background='#2B2B2B').grid(row=0, column=1, sticky='nesw')
lastCam = Button(con, image=lastImg,    command=lst, background='#2B2B2B').grid(row=2, column=1, sticky='nesw')
takeImg = Button(con, image=ok, command=ok, background='#2B2B2B').grid(row=1, column=1, sticky='nesw')
我希望输出如下所示:

但它实际上做的是:

------------------------编辑--------------------

这需要使用两个以上的按钮。

有趣的问题。PhotoImage没有调整大小的方法,但您可以使用PIL图像来实现。如果您没有PIL,您需要
pip安装枕头
才能获得它

import tkinter as tk
from PIL import Image, ImageTk

class ImageButton(tk.Button):
    """A button that displays an Image and resizes the image as the Button size changes"""
    def __init__(self, master=None, image=None, **kwargs):
        super().__init__(master, **kwargs)
        if not image: return # no image provided; act as a normal Button
        if isinstance(image, str):
            self.image = Image.open(image)
        elif isinstance(image, Image.Image):
            self.image = image
        else:
            raise TypeError("'image' argument must be a PIL.Image or filename")
        self.bind("<Configure>", self._on_configure)
    def _on_configure(self, event=None):
        size = event.width-4, event.height-4
        self.photoimage = ImageTk.PhotoImage(self.image.resize(size))
        self.config(image=self.photoimage)

### test / demo code: ###

def main():
    root = tk.Tk()
    root.geometry('200x200')
    win = ImageButton(root, image="ON.gif")
    win.pack(fill=tk.BOTH, expand=True)
    root.mainloop()

if __name__ == '__main__':
    main()
将tkinter作为tk导入
从PIL导入图像,ImageTk
类ImageButton(tk.按钮):
“”“显示图像并随着按钮大小的变化调整图像大小的按钮”“”
定义初始化(self,master=None,image=None,**kwargs):
super().\uuuu init\uuuu(主控,**kwargs)
如果没有图像:返回#没有提供图像;充当普通按钮
如果存在(图像,str):
self.image=image.open(image)
elif isinstance(image,image.image):
self.image=image
其他:
raise TypeError(“'image'参数必须是PIL.image或filename”)
self.bind(“,self.\u on\u configure)
def_on_configure(自我,事件=无):
大小=event.width-4,event.height-4
self.photoimage=ImageTk.photoimage(self.image.resize(大小))
self.config(image=self.photoimage)
###测试/演示代码:###
def main():
root=tk.tk()
root.geometry('200x200')
win=ImageButton(root,image=“ON.gif”)
win.pack(fill=tk.BOTH,expand=True)
root.mainloop()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

请注意,您必须定义窗口的初始大小才能使其正常工作。如果不这样做,那么每次调整大小都会触发它进一步增长

您必须使用
枕头
生成更大的图像并替换它们示例代码:当我使用两个按钮时,第一个按钮将在第二个按钮上展开。