{"id":26483,"date":"2022-01-29T14:01:14","date_gmt":"2022-01-29T14:01:14","guid":{"rendered":"https:\/\/www.askpython.com\/?p=26483"},"modified":"2022-01-29T14:01:16","modified_gmt":"2022-01-29T14:01:16","slug":"coin-flip-gui","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/coin-flip-gui","title":{"rendered":"Coin Flip GUI using Python Tkinter"},"content":{"rendered":"\n<p>In this tutorial, we\u2019ll code a coin flip program with Graphical User Interface (GUI) using Python Tkinter. The tutorial is aimed at teaching you the basics of the <strong>Tkinter <\/strong>module, a great module for developing GUI-based programs in Python.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-buttons\" data-type=\"post\" data-id=\"5696\">Tkinter Tutorial \u2013 Using Tkinter Buttons<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementing-the-coin-flip-gui-app-in-python-tkinter\">Implementing the Coin Flip GUI App in Python Tkinter<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-font-class\" data-type=\"post\" data-id=\"22690\">Tkinter<\/a> is the standard GUI library for python which is used to make interface-based applications. Tkinter when combined with Python makes it very easy to make GUI-based applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-installing-modules\">1. Installing Modules<\/h3>\n\n\n\n<p>For this program, we need Python <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-universal-functions\" data-type=\"post\" data-id=\"14334\">NumPy<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/pillow-module\" data-type=\"post\" data-id=\"17924\">Pillow<\/a>, and Tkinter libraries which we can easily download using <strong>pip<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install numpy\npip install pillow\npip install tk\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"2-importing-modules\">2. Importing Modules<\/h3>\n\n\n\n<p>After installing the modules we can start coding our program by importing all modules in our program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nfrom tkinter import *\nfrom PIL import Image, ImageTk\n<\/pre><\/div>\n\n\n<p>Note:- In the above code, &#8216;*&#8217; means that we\u2019re importing everything from module Tkinter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-creating-the-main-window-for-our-application\">3. Creating the Main Window for our Application\u00a0<\/h3>\n\n\n\n<p>First, we\u2019ll initialize the Tkinter class using Tk() object and assign it to the &#8216;root&#8217; variable. So now by using\u00a0root we can access all the methods of the Tkinter module.<\/p>\n\n\n\n<p>In the second line of code, we specify the <strong>Widow Size<\/strong> of the output GUI screen, like here we\u2019ve given it a value of 400*400(for widthxheight).<\/p>\n\n\n\n<p>Now we use mainloop method to make the window <strong>persistent<\/strong> meaning that the window will not close unless we want to close it ourselves. If we skip this line of code, the output screen appears once and closes immediately.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nroot = Tk()\nroot.geometry(&quot;500*500&quot;)\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>After running the above code we\u2019ll get a window like shown below. If you get a window like this you are all good and can follow on.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"493\" height=\"360\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/output-GUI-screen-tkinter.png\" alt=\"Output GUI Screen Tkinter\" class=\"wp-image-26484\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/output-GUI-screen-tkinter.png 493w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/output-GUI-screen-tkinter-300x219.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><figcaption>Output GUI Screen Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-loading-images\">4. Loading Images<\/h3>\n\n\n\n<p>We\u2019ll show the image of a coin according to the outcome of our program. If the output is <strong>Heads<\/strong> then it will show the head side of the coin, and Tails side of the coin when it is <strong>Tails<\/strong>.<\/p>\n\n\n\n<p>Heads and Tails both images are saved in the same directory as our program file. If not, in your case then you need to pass the image file name with the location of it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#load heads image\nload = Image.open(&quot;heads.jpg&quot;)\nheads = ImageTk.PhotoImage(load)\n\n#load tails image\nload = Image.open(&quot;tails.jpg&quot;)\ntails = ImageTk.PhotoImage(load)\n<\/pre><\/div>\n\n\n<p>For your convenience, we have added the image file to the head and tail side of a coin.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/head.png\">Coin head image<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/tail.jpg\">Coin tail image<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-adding-a-button\">5. Adding A Button<\/h3>\n\n\n\n<p>Now that our main window is ready we need a <strong>button<\/strong> which we can press to <strong>Toss<\/strong> the coin.<\/p>\n\n\n\n<p>By pressing this button we are just calling a <strong>tossTheCoin<\/strong> function. We can use the <strong>Button <\/strong>class in tkinter to create the button.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nb1=Button(root, text=&quot;Toss the Coin&quot;, font=(&quot;Arial&quot;, 10), command=tossTheCoin, bg=&#039;teal&#039;, fg=&#039;white&#039;, activebackground=&quot;lightblue&quot;, padx=10, pady=10)\nb1.pack()\n<\/pre><\/div>\n\n\n<p>The above code will render a button in our main window with the text <strong>Toss the Coin. <\/strong>In <strong>command,<\/strong> we\u2019ll pass our function name.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We use the <strong>pack() <\/strong>method for every element we want to render on our main window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-text-field-for-result\">6. Text Field for result<\/h3>\n\n\n\n<p>Now, we make a text field for our result of the coin flip in the text format. We do so, by using the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntfield = Text(root, width=52, height=5)\ntfield.pack()\n<\/pre><\/div>\n\n\n<p>To insert text in this text field we use the insert function, as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntfield.insert(INSERT, &quot;Click on the Button.. To Flip the Coin and get the result&quot;)\n<\/pre><\/div>\n\n\n<p>Now, for every time change in the result, we will need to erase the previous inserted text, so we bind the <strong>delete<\/strong> text method with the button click &#8211; <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntfield.delete(&quot;1.0&quot;, &quot;end&quot;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"7-implementing-tossthecoin-function\">7. Implementing tossTheCoin() function<\/h3>\n\n\n\n<p>When we toss a coin, it has a 50% chance that it lands on either head or tail. We want to have this fair nature in our program so it can be close to the real coin-tossing scenario. For this, we will use the binomial method of the NumPy module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.random.binomial(1,0.5)\n<\/pre><\/div>\n\n\n<p>This will return either one or zero. So we can write if condition statement to check if it is 1 or 0 and render the head or tail image accordingly, using the <strong>config<\/strong> method.<\/p>\n\n\n\n<p>The full code will look like this-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nfrom tkinter import *\nfrom PIL import Image, ImageTk\n\n\ndef coinFlip():\n    result = np.random.binomial(1,0.5)\n    tfield.delete(&quot;1.0&quot;, &quot;end&quot;)\n\n    if(result == 1):\n        tfield.insert(INSERT, &quot; It&#039;s \u2014\u2014\u2014\u2014&gt; HEADS&quot;)\n        i.config(image = heads)\n        \n    else:\n        tfield.insert(INSERT, &quot; It&#039;s \u2014\u2014\u2014\u2014&gt; TAILS&quot;)\n        i.config(image = tails)\n\nroot = Tk()\nroot.title(&quot;Python Coin Flip&quot;)\n\n#load heads image\nload = Image.open(&quot;head.png&quot;)\nheads = ImageTk.PhotoImage(load)\n\n#load tails image\nload = Image.open(&quot;tail.png&quot;)\ntails = ImageTk.PhotoImage(load)\n\ni = Label(root, image=heads)\ni.pack()\n\nroot.geometry(&quot;500x500&quot;)\nb1 = Button(root, text=&quot;Toss the Coin&quot;, font=(&quot;Arial&quot;, 10), command=coinFlip, bg=&#039;teal&#039;, fg=&#039;white&#039;, activebackground=&quot;lightblue&quot;, padx=10, pady=10)\nb1.pack()\n\n#Text Field for Result\ntfield = Text(root, width=52, height=5)\ntfield.pack()\ntfield.insert(INSERT, &quot;Click on the Button.. To Flip the Coin and get the result&quot;)\n\n\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>The final output of the program:-<\/p>\n\n\n\n<figure class=\"wp-block-gallery aligncenter has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"585\" data-id=\"26492\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Heads-1.png\" alt=\"Output For Heads 1\" class=\"wp-image-26492\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Heads-1.png 631w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Heads-1-300x278.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><figcaption>Output For Heads 1<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"549\" data-id=\"26493\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Tails-1.png\" alt=\"Output For Tails 1\" class=\"wp-image-26493\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Tails-1.png 629w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Output-For-Tails-1-300x262.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><figcaption>Output For Tails 1<\/figcaption><\/figure>\n<figcaption class=\"blocks-gallery-caption\">The output of our code &#8211; on button click for Coin Flip<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>That&#8217;s It! for the tutorial. Hope you have learned well and coded a Coin Flip &#8211; GUI-based code using Python Tkinter easily and without any hassle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we\u2019ll code a coin flip program with Graphical User Interface (GUI) using Python Tkinter. The tutorial is aimed at teaching you the basics of the Tkinter module, a great module for developing GUI-based programs in Python. Also read: Tkinter Tutorial \u2013 Using Tkinter Buttons Implementing the Coin Flip GUI App in Python [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":26497,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-26483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/26483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=26483"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/26483\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/26497"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=26483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=26483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=26483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}