In this article we will see how we can add or show image in the button box. Button box is used to display a window having multiple buttons in EasyGUI, it can be used where there is condition to select one among lot of buttons for example buttons in lift at a time user can opt only one option, below is how the normal button box looks like
When we invoke the buttonbox function (or other functions that display a button box, such as msgbox, indexbox, ynbox, etc.), we can specify the keyword argument image=img where img is the filename of an image. The file can be .gif. Usually, we can use other images such as .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 fourth is keyword argument which is image source and the button choices
Return : It returns the text of the button that the user selected
Example : In this we will create a button box with a image, and user will be allowed to select any button and message will appear according to the message, below is the implementation
Python3
# 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) |
Output :
Another Example :
Python3
# 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) |
Output :