使用图像限制按钮的大小
limit the size of a button with an image
问题:
我想创建自己的小部件,在按钮中使用图像,但图像导致按钮太大。如何将按钮的大小调整为正常的按钮大小(正常文本的大小)。
代码:
add = Button(master=controlfrm , image=myimagepath)
add.pack()
结果:
目标:
我希望将图像调整为与 Entry 小部件相同的高度。
Tkinter 不会缩小或展开图像。最好的办法是在 PhotoImage 上使用 zoom
和 subsample
方法,这样您就可以将大小更改为原来的 2 倍。
如果您想在按钮上使用图像,并且希望它更小,最好的解决方案是从尺寸合适的图像开始。
尝试设置图片的宽高属性,使图片适合您需要的按钮大小。
您可以使用ImageTk.PhotoImage调整图像大小并在按钮中调用它。使用缩放功能缩小或放大按钮图像。
button_image_file = "images/square-button-1.png"
button_image = Image.open(button_image_file)
zoom = .40 # multiplier for image size by zooming -/+
pixels_x, pixels_y = tuple([int(zoom * x) for x in button_image.size])
button_image = ImageTk.PhotoImage(button_image.resize((pixels_x, pixels_y)))
button = tk.Button(root_main, text="Button", command=lambda: do_something(), font="Arial",
bg="#20bebe", fg="white", image=button_image)
button.place(rely=0.01, relx=.01)
问题:
我想创建自己的小部件,在按钮中使用图像,但图像导致按钮太大。如何将按钮的大小调整为正常的按钮大小(正常文本的大小)。代码:
add = Button(master=controlfrm , image=myimagepath)
add.pack()
结果:
目标:
我希望将图像调整为与 Entry 小部件相同的高度。
Tkinter 不会缩小或展开图像。最好的办法是在 PhotoImage 上使用 zoom
和 subsample
方法,这样您就可以将大小更改为原来的 2 倍。
如果您想在按钮上使用图像,并且希望它更小,最好的解决方案是从尺寸合适的图像开始。
尝试设置图片的宽高属性,使图片适合您需要的按钮大小。
您可以使用ImageTk.PhotoImage调整图像大小并在按钮中调用它。使用缩放功能缩小或放大按钮图像。
button_image_file = "images/square-button-1.png"
button_image = Image.open(button_image_file)
zoom = .40 # multiplier for image size by zooming -/+
pixels_x, pixels_y = tuple([int(zoom * x) for x in button_image.size])
button_image = ImageTk.PhotoImage(button_image.resize((pixels_x, pixels_y)))
button = tk.Button(root_main, text="Button", command=lambda: do_something(), font="Arial",
bg="#20bebe", fg="white", image=button_image)
button.place(rely=0.01, relx=.01)