Python button add image

Tkinter Button

Summary: in this tutorial, you’ll learn about the Tkinter Button widget and how to use it to create various kinds of buttons.

Introduction to Tkinter button widget

Button widgets represent a clickable item in the applications. Typically, you use a text or an image to display the action that will be performed when clicked.

Buttons can display text in a single font. However, the text can span multiple lines. On top of that, you can make one of the characters underline to mark a keyboard shortcut.

To invoke a function or a method of a class automatically when the button is clicked, you assign its command option to the function or method. This is called the command binding in Tkinter.

To create a button, you use the ttk.Button constructor as follows:

button = ttk.Button(container, **option)Code language: Python (python)

A button has many options. However, the typical ones are like this:

button = ttk.Button(container, text, command)Code language: Python (python)
  • The container is the parent component on which you place the button.
  • The text is the label of the button.
  • The command specifies a callback function that will be called automatically when the button clicked.

Command callback

The command option associates the button’s action with a function or a method of a class. When you click or press the button, it’ll automatically invoke a callback function.

To assign a callback to the command option, you can use a lambda expression:

def callback(): # do something ttk.Button( root, text="Demo Button", command=callback )Code language: Python (python)

If the function contains one expression, you use a lamba expression:

ttk.Button( root, text="Demo Button", command=lambda_expression )Code language: Python (python)

Button states

The default state of a button is normal . In the normal state, the button will respond to the mouse events and keyboard presses by invoking the callback function assigned to its command option.

The button can also have the disabled state. In the disabled state, a button is greyed out and doesn’t respond to the mouse events and keyboard presses.

To control the state of a button, you use the state() method:

# set the disabled flag button.state(['disabled']) # remove the disabled flag button.state(['!disabled'])Code language: Python (python)

Tkinter button examples

Let’s take some examples of using button widgets.

1) Simple Tkinter button example

The following program shows how to display an Exit button. When you click it, the program is terminated.

import tkinter as tk from tkinter import ttk # root window root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Button Demo') # exit button exit_button = ttk.Button( root, text='Exit', command=lambda: root.quit() ) exit_button.pack( ipadx=5, ipady=5, expand=True ) root.mainloop()Code language: Python (python)

The following creates the Exit button:

exit_button = ttk.Button( root, text='Exit', command=lambda: root.quit() )Code language: Python (python)

The command of the button is assigned to a lambda expression that closes the root window.

2) Tkinter image button example

The following program shows how to display an image button. To practice this example, you need to download the following image first:

Just right-click and save it into a folder that is accessible from the following program e.g., assets folder:

import tkinter as tk from tkinter import ttk from tkinter.messagebox import showinfo # root window root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Image Button Demo') # download button def download_clicked(): showinfo( title='Information', message='Download button clicked!' ) download_icon = tk.PhotoImage(file='./assets/download.png') download_button = ttk.Button( root, image=download_icon, command=download_clicked ) download_button.pack( ipadx=5, ipady=5, expand=True ) root.mainloop()Code language: Python (python)

  • First, create a new instance of the tk.PhotoImage class that references the image file ‘./assets/download.png’ .
  • Second, create the ttk.Button whose image option is assigned to the image.
  • Third, assign a function to the command option. When you click the button, it’ll call the download_clicked function that displays a message box.

3) Displaying an image button

To display both text and image on a button, you need to use the compound option. If you don’t, the button will display the text only, not the image.

The following shows how to display both text and image on a button:

import tkinter as tk from tkinter import ttk from tkinter.messagebox import showinfo # root window root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Image Button Demo') # download button handler def download_clicked(): showinfo( title='Information', message='Download button clicked!' ) download_icon = tk.PhotoImage(file='./assets/download.png') download_button = ttk.Button( root, image=download_icon, text='Download', compound=tk.LEFT, command=download_clicked ) download_button.pack( ipadx=5, ipady=5, expand=True ) root.mainloop() Code language: Python (python)

Summary

  • Use the ttk.Button() class to create a button.
  • Assign a lambda expression or a function to the command option to respond to the button click event.
  • Assign the tk.PhotoImage() to the image property to display an image on the button.
  • Use the compound option if you want to display both text and image on a button.

Источник

Python button add image

  • Python | Creating a button in tkinter
  • Python | Add style to tkinter button
  • Python | Add image on a Tkinter button
  • Python Tkinter – Label
  • Python Tkinter | Create LabelFrame and add widgets to it
  • RadioButton in Tkinter | Python
  • Python Tkinter – Checkbutton Widget
  • Python Tkinter – Canvas Widget
  • Python Tkinter | Create different shapes using Canvas class
  • Python Tkinter | Create different type of lines using Canvas class
  • Python Tkinter | Moving objects using Canvas.move() method
  • Combobox Widget in tkinter | Python
  • maxsize() method in Tkinter | Python
  • minsize() method in Tkinter | Python
  • resizable() method in Tkinter | Python
  • Python Tkinter – Entry Widget
  • Tkinter – Read only Entry Widget
  • Python Tkinter – Text Widget
  • Python Tkinter – Message
  • Python | Menu widget in Tkinter
  • Python Tkinter – Menubutton Widget
  • Python Tkinter – SpinBox
  • Progressbar widget in Tkinter | Python
  • Python-Tkinter Scrollbar
  • Python Tkinter – ScrolledText Widget
  • Python Tkinter – ListBox Widget
  • Scrollable ListBox in Python-tkinter
  • Python Tkinter – Frame Widget
  • Scrollable Frames in Tkinter
  • How to make a proper double scrollbar frame in Tkinter
  • Python Tkinter – Scale Widget
  • Hierarchical treeview in Python GUI application
  • Python-Tkinter Treeview scrollbar
  • Python Tkinter – Toplevel Widget
  • Python | askopenfile() function in Tkinter
  • Python | asksaveasfile() function in Tkinter
  • Python – Tkinter askquestion Dialog
  • Python Tkinter – MessageBox Widget
  • Create a Yes/No Message Box in Python using tkinter
  • Change the size of MessageBox – Tkinter
  • Different messages in Tkinter | Python
  • Change Icon for Tkinter MessageBox
  • Python – Tkinter Choose color Dialog
  • Popup Menu in Tkinter
  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python
  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?
  • Python | Creating a button in tkinter
  • Python | Add style to tkinter button
  • Python | Add image on a Tkinter button
  • Python Tkinter – Label
  • Python Tkinter | Create LabelFrame and add widgets to it
  • RadioButton in Tkinter | Python
  • Python Tkinter – Checkbutton Widget
  • Python Tkinter – Canvas Widget
  • Python Tkinter | Create different shapes using Canvas class
  • Python Tkinter | Create different type of lines using Canvas class
  • Python Tkinter | Moving objects using Canvas.move() method
  • Combobox Widget in tkinter | Python
  • maxsize() method in Tkinter | Python
  • minsize() method in Tkinter | Python
  • resizable() method in Tkinter | Python
  • Python Tkinter – Entry Widget
  • Tkinter – Read only Entry Widget
  • Python Tkinter – Text Widget
  • Python Tkinter – Message
  • Python | Menu widget in Tkinter
  • Python Tkinter – Menubutton Widget
  • Python Tkinter – SpinBox
  • Progressbar widget in Tkinter | Python
  • Python-Tkinter Scrollbar
  • Python Tkinter – ScrolledText Widget
  • Python Tkinter – ListBox Widget
  • Scrollable ListBox in Python-tkinter
  • Python Tkinter – Frame Widget
  • Scrollable Frames in Tkinter
  • How to make a proper double scrollbar frame in Tkinter
  • Python Tkinter – Scale Widget
  • Hierarchical treeview in Python GUI application
  • Python-Tkinter Treeview scrollbar
  • Python Tkinter – Toplevel Widget
  • Python | askopenfile() function in Tkinter
  • Python | asksaveasfile() function in Tkinter
  • Python – Tkinter askquestion Dialog
  • Python Tkinter – MessageBox Widget
  • Create a Yes/No Message Box in Python using tkinter
  • Change the size of MessageBox – Tkinter
  • Different messages in Tkinter | Python
  • Change Icon for Tkinter MessageBox
  • Python – Tkinter Choose color Dialog
  • Popup Menu in Tkinter
  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python
  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?

Источник

Читайте также:  Закрыть страницу браузера php
Оцените статью