Add image on a Tkinter button
プログラムソース
from tkinter import *
from tkinter.ttk import *
root = Tk()
Label(root, text='GeekforGeeks',
font=('Verdana',15)).pack(side=TOP,pady=10)
photo = PhotoImage(file=r"c:\python\mydog2020.PNG")
photoimage=photo.subsample(3,3)
Button(root,text='Click Me !',
image=photoimage,compound=TOP).pack(side=TOP)
mainloop()
compound=LEFT に変更した場合
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 of PhotoImage()
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 on Button, 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 pass compound 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
コメント
コメントを投稿