Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Add style to tkinter button
Next article icon
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive.Image can be added with the help ofPhotoImage() method. This is a Tkinter method which means you don't have to import any other module in order to use it.Important: If both image and text are given onButton, the text will be dominated and only image will appear on the Button. But if you want to show both image and text then you have to passcompound in button options.
Button(master, text = "Button", image = "image.png", compound=LEFT)compound = LEFT -> image will be at left side of the buttoncompound = RIGHT -> image will be at right side of buttoncompound = TOP -> image will be at top of buttoncompound = BOTTOM -> image will be at bottom of button
Syntax:
photo = PhotoImage(file = "path_of_file")
path_of_file is any valid path available on your local machine.Code #1:Python3 1==
# importing only those functions# which are neededfromtkinterimport*fromtkinter.ttkimport*# creating tkinter windowroot=Tk()# Adding widgets to the root windowLabel(root,text='GeeksforGeeks',font=('Verdana',15)).pack(side=TOP,pady=10)# Creating a photoimage object to use imagephoto=PhotoImage(file=r"C:\Gfg\circle.png")# here, image option is used to# set image on buttonButton(root,text='Click Me !',image=photo).pack(side=TOP)mainloop()
Output:In output observe that only image is shown on the button and the size of the button is also bigger than the usual size it is because we haven't set the size of the image. Code #2: To show both image and text onButton.Python3 1==
# importing only those functions# which are neededfromtkinterimport*fromtkinter.ttkimport*# creating tkinter windowroot=Tk()# Adding widgets to the root windowLabel(root,text='GeeksforGeeks',font=('Verdana',15)).pack(side=TOP,pady=10)# Creating a photoimage object to use imagephoto=PhotoImage(file=r"C:\Gfg\circle.png")# Resizing image to fit on buttonphotoimage=photo.subsample(3,3)# here, image option is used to# set image on button# compound option is used to align# image on LEFT side of buttonButton(root,text='Click Me !',image=photoimage,compound=LEFT).pack(side=TOP)mainloop()
Output:Observe that both text and image are appearing as well as size of the image is also small.

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp