Python EasyGUI - 在按钮框中显示图像
在本文中,我们将了解如何在按钮框中添加或显示图像。按钮框用于在 EasyGUI 中显示一个具有多个按钮的窗口,它可以用于有条件从许多按钮中选择一个按钮,例如电梯中的按钮一次用户只能选择一个选项,下面是普通按钮的方式盒子看起来像
当我们调用 buttonbox函数(或其他显示按钮框的函数,如 msgbox、indexbox、ynbox 等)时,我们可以指定关键字参数 image=img 其中 img 是图像的文件名。该文件可以是 .gif。通常,我们可以使用其他图像,例如 .png。
In order to do this we will use buttonbox
method
Syntax : buttonbox(text, title, image=img, choices=button_list)
Argument : It takes 4 arguments, first string i.e text to be displayed, second string i.e title of the window and third and forth is keyword argument which is image source and the button choices
Return : It returns the text of the button that the user selected
例子 :
在此我们将创建一个带有图像的按钮框,允许用户选择任何按钮并根据消息显示消息,以下是实现
# importing easygui module
from easygui import *
# message to be displayed
text = "Message to be displayed on the window GfG"
# window title
title = "Window Title GfG"
# button list
button_list = []
# button 1
button1 = "Average"
# second button
button2 = "Good"
# third button
button3 = "Very Good"
# appending button to the button list
button_list.append(button1)
button_list.append(button2)
button_list.append(button3)
img = "gfg.png"
# creating a button box
output = buttonbox(text, title, image = img, choices = button_list)
# title for the message box
title = "Message Box"
# message
message = "You selected : " + output
# creating a message box
msg = msgbox(message, title)
输出 :
另一个例子 :
# importing easygui module
from easygui import *
# message to be displayed
text = "How much do you like the image given below"
# window title
title = "Window Title GfG"
# button list
button_list = []
# button 1
button1 = "Average"
# second button
button2 = "Good"
# third button
button3 = "Very Good"
# appending button to the button list
button_list.append(button1)
button_list.append(button2)
button_list.append(button3)
# a image of a dog
img = "dog_image.png"
# creating a button box
output = buttonbox(text, title, image = img, choices = button_list)
# title for the message box
title = "Message Box"
# message
message = "You selected : " + output
# creating a message box
msg = msgbox(message, title)
输出 :