{"id":1336,"date":"2020-11-30T01:00:12","date_gmt":"2020-11-30T01:00:12","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1336"},"modified":"2025-04-03T08:47:46","modified_gmt":"2025-04-03T08:47:46","slug":"tkinter-button","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/","title":{"rendered":"Tkinter Button"},"content":{"rendered":"\r\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter Button widget and how to use it to create various kinds of buttons.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-button-widget'>Introduction to the Tkinter Button widget <a href=\"#introduction-to-the-tkinter-button-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-button-widget\" title=\"Anchor for Introduction to the Tkinter Button widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>A button triggers an action when you click on it. The tkinter button can display:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Text label<\/li>\r\n\r\n\r\n\r\n<li>Image<\/li>\r\n\r\n\r\n\r\n<li>Both text label and image<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Typically, an action is a function you want to execute when users click the button. To execute the function, you assign its name to the command parameter of the Button widget. This is called <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-command\/\">the command binding in Tkinter<\/a>.<\/p>\r\n\r\n\r\n\r\n<p>Here are the steps for creating a button widget:<\/p>\r\n\r\n\r\n\r\n<p>First, import ttk from tkinter:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Second, create a new button widget using the the <code>Button<\/code> constructor:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">button = Button(master, **kw)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this syntax:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>The <code>master<\/code> is the parent widget on which you place the button.<\/li>\r\n\r\n\r\n\r\n<li>The <code>**kw<\/code> is one or more keyword arguments you use to change the appearance and behaviors of the button.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Here are the common configurations of the button widget:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">button = ttk.Button(\r\n   master, \r\n   text=label,\r\n   command=fn\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this syntax:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>The <code>text<\/code> determines the label of the button.<\/li>\r\n\r\n\r\n\r\n<li>The <code>command<\/code> specifies a callback function that will execute automatically when you click the button.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='command-callback'>Command callback <a href=\"#command-callback\" class=\"anchor\" id=\"command-callback\" title=\"Anchor for Command callback\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The <code>command<\/code> option associates the button&#8217;s action with a function or a <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">method<\/a> of a class. When you click the button, the callback automatically executes.<\/p>\r\n\r\n\r\n\r\n<p>To assign a callback to the <code>command<\/code> option, you can use a regular function:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">callback<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\r\n    <span class=\"hljs-comment\"># do something<\/span>\r\n\r\n\r\nttk.Button(\r\n   root, \r\n   text=<span class=\"hljs-string\">\"Click Me\"<\/span>, \r\n   command=callback\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>If the function contains one expression, you can use a lambda expression:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">ttk.Button(\r\n   root, \r\n   text=<span class=\"hljs-string\">\"Demo Button\"<\/span>, \r\n   command=<span class=\"hljs-keyword\">lambda<\/span>: ...\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='button-states'>Button states <a href=\"#button-states\" class=\"anchor\" id=\"button-states\" title=\"Anchor for Button states\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The default state of a button is <code>normal<\/code>. In the <code>normal<\/code> state, the button will respond to the following events:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Mouse click<\/li>\r\n\r\n\r\n\r\n<li>Keyboard press<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>The button can also have the <code>disabled<\/code> state. In the <code>disabled<\/code> state, a button is greyed out and doesn&#8217;t respond to the mouse click and keyboard press events.<\/p>\r\n\r\n\r\n\r\n<p>To set the state of a button, you use the <code>state()<\/code> method:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># set the disabled flag<\/span>\r\nbutton.state(&#91;<span class=\"hljs-string\">'disabled'<\/span>])\r\n\r\n<span class=\"hljs-comment\"># remove the disabled flag<\/span>\r\nbutton.state(&#91;<span class=\"hljs-string\">'!disabled'<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='tkinter-button-examples'>Tkinter button examples <a href=\"#tkinter-button-examples\" class=\"anchor\" id=\"tkinter-button-examples\" title=\"Anchor for Tkinter button examples\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>Let&#8217;s take some examples of using button widgets.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='simple-tkinter-button-example'>Simple Tkinter button example <a href=\"#simple-tkinter-button-example\" class=\"anchor\" id=\"simple-tkinter-button-example\" title=\"Anchor for Simple Tkinter button example\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>The following program shows how to display a <code>Exit<\/code> button. When you click it, the program is terminated.<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n\r\n<span class=\"hljs-comment\"># main window<\/span>\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\r\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Button Demo'<\/span>)\r\n\r\n<span class=\"hljs-comment\"># exit button<\/span>\r\nexit_button = ttk.Button(\r\n    root,\r\n    text=<span class=\"hljs-string\">'Exit'<\/span>,\r\n    command=<span class=\"hljs-keyword\">lambda<\/span>: root.quit()\r\n)\r\n\r\nexit_button.pack(\r\n    ipadx=<span class=\"hljs-number\">5<\/span>,\r\n    ipady=<span class=\"hljs-number\">5<\/span>,\r\n    expand=<span class=\"hljs-literal\">True<\/span>\r\n)\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"230\" class=\"wp-image-1338\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Button-Simple-Button.png\" alt=\"\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>How it works.<\/p>\r\n\r\n\r\n\r\n<p>The following creates the <code>Exit<\/code> button:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">exit_button\u00a0=\u00a0ttk.Button(\r\n\u00a0\u00a0\u00a0\u00a0root,\r\n\u00a0\u00a0\u00a0\u00a0text=<span class=\"hljs-string\">'Exit'<\/span>,\r\n\u00a0\u00a0\u00a0\u00a0command=<span class=\"hljs-keyword\">lambda<\/span>:\u00a0root.quit()\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The button&#8217;s command is assigned to a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\">lambda expression<\/a> that closes the main window.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='tkinter-image-button-example'>Tkinter image button example <a href=\"#tkinter-image-button-example\" class=\"anchor\" id=\"tkinter-image-button-example\" title=\"Anchor for Tkinter image button example\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>The following program shows how to display an image button. To practice this example, you need to download the following image first:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" class=\"wp-image-1456\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/download.png\" alt=\"\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>Just right-click and save it into a folder that is accessible from the following program, e.g., <code>assets<\/code> folder:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span>\u00a0tkinter\u00a0<span class=\"hljs-keyword\">as<\/span>\u00a0tk\r\n<span class=\"hljs-keyword\">from<\/span>\u00a0tkinter\u00a0<span class=\"hljs-keyword\">import<\/span>\u00a0ttk\r\n<span class=\"hljs-keyword\">from<\/span>\u00a0tkinter.messagebox\u00a0<span class=\"hljs-keyword\">import<\/span>\u00a0showinfo\r\n\r\n\r\n<span class=\"hljs-comment\">#\u00a0main window<\/span>\r\nroot\u00a0=\u00a0tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\r\nroot.resizable(<span class=\"hljs-literal\">False<\/span>,\u00a0<span class=\"hljs-literal\">False<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Image\u00a0Button\u00a0Demo'<\/span>)\r\n\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span>\u00a0<span class=\"hljs-title\">handle_click<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\r\n\u00a0\u00a0\u00a0\u00a0showinfo(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0title=<span class=\"hljs-string\">'Information'<\/span>,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0message=<span class=\"hljs-string\">'Download\u00a0button\u00a0clicked!'<\/span>\r\n\u00a0\u00a0\u00a0\u00a0)\r\n\r\n\r\ndownload_icon\u00a0=\u00a0tk.PhotoImage(file=<span class=\"hljs-string\">'.\/assets\/download.png'<\/span>)\r\ndownload_button\u00a0=\u00a0ttk.Button(\r\n\u00a0\u00a0\u00a0\u00a0root,\r\n\u00a0\u00a0\u00a0\u00a0image=download_icon,\r\n\u00a0\u00a0\u00a0\u00a0command=handle_click\r\n)\r\n\r\ndownload_button.pack(\r\n\u00a0\u00a0\u00a0\u00a0ipadx=<span class=\"hljs-number\">5<\/span>,\r\n\u00a0\u00a0\u00a0\u00a0ipady=<span class=\"hljs-number\">5<\/span>,\r\n\u00a0\u00a0\u00a0\u00a0expand=<span class=\"hljs-literal\">True<\/span>\r\n)\r\n\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"232\" class=\"wp-image-1454\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-Button.png\" alt=\"\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-Button.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-Button-300x230.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>How it works.<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>First, create a new instance of the <code>tk.PhotoImage<\/code> class that references the image file <code>'.\/assets\/download.png'<\/code>.<\/li>\r\n\r\n\r\n\r\n<li>Second, create the <code>ttk.Button<\/code> whose image option is assigned to the image.<\/li>\r\n\r\n\r\n\r\n<li>Third, assign a function to the <code>command<\/code> option. When you click the button, it calls the <code>handle_click<\/code> function to display a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-messagebox\/\">message box<\/a>.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>To display a message box, you follow these steps:<\/p>\r\n\r\n\r\n\r\n<p>First, import the <code>showinfo<\/code> function from the <code>tkinter.messagebox<\/code> module:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span>\u00a0tkinter.messagebox\u00a0<span class=\"hljs-keyword\">import<\/span>\u00a0showinfo<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Second, call the <code>showinfo<\/code> function to display a message box:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">showinfo(\r\n   title=<span class=\"hljs-string\">'Information'<\/span>,\r\n   message=<span class=\"hljs-string\">'Download button clicked!'<\/span>\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='displaying-an-image-button'>Displaying an image button <a href=\"#displaying-an-image-button\" class=\"anchor\" id=\"displaying-an-image-button\" title=\"Anchor for Displaying an image button\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>To display both text and image on a button, you need to use the <code>compound<\/code> option of the button. If you don&#8217;t, the button will display the text label only, not the image.<\/p>\r\n\r\n\r\n\r\n<p>The following shows how to display both text and image on a button:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\r\n\r\n\r\n<span class=\"hljs-comment\"># main window<\/span>\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\r\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Image Button Demo'<\/span>)\r\n\r\n\r\n<span class=\"hljs-comment\"># download button handler<\/span>\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_click<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\r\n    showinfo(\r\n        title=<span class=\"hljs-string\">'Information'<\/span>,\r\n        message=<span class=\"hljs-string\">'Download button clicked!'<\/span>\r\n    )\r\n\r\n\r\ndownload_icon = tk.PhotoImage(file=<span class=\"hljs-string\">'.\/assets\/download.png'<\/span>)\r\n\r\ndownload_button = ttk.Button(\r\n    root,\r\n    image=download_icon,\r\n    text=<span class=\"hljs-string\">'Download'<\/span>,\r\n    compound=tk.LEFT,\r\n    command=handle_click\r\n)\r\n\r\ndownload_button.pack(\r\n    ipadx=<span class=\"hljs-number\">5<\/span>,\r\n    ipady=<span class=\"hljs-number\">5<\/span>,\r\n    expand=<span class=\"hljs-literal\">True<\/span>\r\n)\r\n\r\n\r\nroot.mainloop()\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"232\" class=\"wp-image-1452\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-and-Text-Button.png\" alt=\"\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-and-Text-Button.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Python-Button-Image-and-Text-Button-300x230.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Use the <code>ttk.Button()<\/code> class to create a button.<\/li>\r\n\r\n\r\n\r\n<li>Assign a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\">lambda expression<\/a> or a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> to the <code>command<\/code> option to respond to the button click event.<\/li>\r\n\r\n\r\n\r\n<li>Assign the <code>tk.PhotoImage()<\/code> to the <code>image<\/code> property to display an image on the button.<\/li>\r\n\r\n\r\n\r\n<li>Use the <code>compound<\/code> option if you want to display both text and image on a button.<\/li>\r\n<\/ul>\r\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1336\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\"\n\t\t\t\tdata-post-title=\"Tkinter Button\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1336\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\"\n\t\t\t\tdata-post-title=\"Tkinter Button\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Tkinter Button widget and how to use it to create various kinds of buttons.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1336","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1336"}],"version-history":[{"count":4,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1336\/revisions"}],"predecessor-version":[{"id":7439,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1336\/revisions\/7439"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}