{"id":18138,"date":"2017-08-09T16:15:01","date_gmt":"2017-08-09T13:15:01","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=18138"},"modified":"2018-01-09T09:29:06","modified_gmt":"2018-01-09T07:29:06","slug":"python-tkinter-tutorial","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/","title":{"rendered":"Python Tkinter Tutorial"},"content":{"rendered":"<p>Today we will be talking about one python library that is used for GUI programming which is <code>Tkinter<\/code>.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#intro\">1. What is Tkinter?<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#info\">1.1. More about Tkinter<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#install\">1.2. Installing Tkinter<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#example\">2. Examples<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#basic\">2.1. Basic examples<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#widget\">2.2. Widget<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#frame\">2.3. Frame<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#event\">2.4. Event handling<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#summary\">3. Summary<\/a><\/dt>\n<dt><a href=\"#homework\">4. Homework<\/a><\/dt>\n<dt><a href=\"#download\">5. Download the source code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"intro\"><\/a>1. What is Tkinter<\/h2>\n<h3><a name=\"info\"><\/a>1.1. More about Tkinter<\/h3>\n<p><code>Tkinter<\/code> is a library that is used for creating interfaces. Sometimes, we not only need to think about software itself, complicated algorithms, but a good interface as well. Without good interface there is a very little chance that our software product would be popular. It&#8217;s quite simple to create simple interfaces using the library. Without hesitation, let&#8217;s get started, shall we?<\/p>\n<h3><a name=\"install\"><\/a>1.2. Installing Tkinter<\/h3>\n<p><code>Tkinter<\/code> isn&#8217;t usually pre-installed. I will show how to install it on both: Windows and Linux.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (Windows)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">pip install python-tk\r\n<\/pre>\n<p>If it doesn&#8217;t work, there may be some problems. One problem might be that you are using 64 bit version when you need 32 bit version. Another one could be a problem with pip itself. In this case, I would suggest downloading wheel or binaries from python. If that fails, go to <code>ActiveState<\/code> and follow easy instructions on how to download it. Chances that you may be experiencing any problems with downloading <code>Tkinter<\/code> are close to none, but it&#8217;s always better to know how to solve issues that might happen.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (Debian Linux)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">sudo apt-get install python3-tk\r\n<\/pre>\n<p>That works great on all Debian-based systems. However, that would be unfair to tell that it works on all linux systems. So I should spend some time to tell you how to install it if you are using different systems. If you are an Arch Linux user, something like this can work great for you!<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (Arch Linux)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">sudo pacman -S tk<\/pre>\n<p>If you are a fan of RHEL, CentOS, Oracle, just use yum:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (RHEL, CentOS, Oracle Linux)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">yum install tkinter<\/pre>\n<p>In this case, you might have some problems. If you do, that means that <code>tk-devel<\/code> needs to be installed. So in other way, just do the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (RHEL, CentOS, Oracle Linux)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">yum install -y tkinter tk-devel<\/pre>\n<p>By the time writing this article I wasn&#8217;t quite aware that Mac OS users have some problems installing <code>Tkinter<\/code>. So it&#8217;s best to show you how to install it.<br \/>\nFirst, you go to <a href=\"www.activestate.com\">www.activestate.com<\/a>, and follow along the links to download the Community Edition of ActiveTcl, available as a universal binary. Make sure you&#8217;re downloading an 8.5.x version, not an older 8.4.x version. Once you finish all necessary downloads, you will find <code>Wish<\/code> application installed on your machine. Go ahead and open it. You will have another thing popped up which is your console. It will run all Tcl\/Tk commands. It&#8217;s also wise to verify the exact version of Tcl\/Tk that you are running. To do this, type the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installing Tkinter (Mac OS)<\/em><\/span><\/p>\n<pre class=\"brush:bash\">% info patchlevel<\/pre>\n<p>We would like to receive something like this: <code>Verified install using ActiveTcl 8.5.10.1 on Mac OS X 10.7.1.<\/code>. Alright, I guess I have covered all possible installations. Let&#8217;s get started, shall we?<\/p>\n<h2><a name=\"example\"><\/a>2. Examples<\/h2>\n<h3><a name=\"basic\"><\/a>2.1. Basic examples<\/h3>\n<p>First of all, we need to check if <code>Tkinter<\/code> has been successfully installed. To do so, we need to run the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Test<\/em><\/span><\/p>\n<pre class=\"brush:python\">import tkinter\r\ntkinter._test()<\/pre>\n<p>If everything is installed correctly, you will see a pop-up window that has some text and two buttons. If you don&#8217;t see it, go to the first section and download <code>Tkinter<\/code> first.<br \/>\nAlright, let&#8217;s go ahead and code! There are 2 ways how to import <code>Tkinter<\/code>: you may do <code>import tkinter<\/code>, or type <code>from tkinter import *<\/code>. We will use the second option, because we won&#8217;t have to name the module before using any methods. In any application, we always have some main window that eventually leads users to other widgets. Object-window is a variable <code>root<\/code>. Go ahead and run this:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Main window<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nroot = Tk()<\/pre>\n<p>Now, let&#8217;s create widget! We will only have one button there. To access the button we will use <code>Button()<\/code> method. The class <code>Button<\/code> has one mandatory attribute &#8211; object that is associated with a button. The idea is that button cannot exist by itself.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Adding button<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nroot = Tk()\r\nbut = Button(root)<\/pre>\n<p>Let&#8217;s put some text on the button by doing the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Adding button<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nroot = Tk()\r\nbut = Button(root)\r\nbut[\"text\"] = \"Hi there\"<\/pre>\n<p>Surely, button alone doesn&#8217;t do much. We need to have some action with it. For instance, we want to print some text once we click the button.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Adding button<\/em><\/span><\/p>\n<pre class=\"brush:python\">def printer(event):\r\n   print(\"Hi there, this button works\")<\/pre>\n<p>Don&#8217;t forget that it&#8217;s essential to put this function in the beginning of the code because it&#8217;s an event. If we do left mouse click, it is <code>Button<\/code>. We have to bind function with the button in order to make it work.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Adding button<\/em><\/span><\/p>\n<pre class=\"brush:python\">but.bind(\"\", printer)<\/pre>\n<p>So what we do here is we are binding the function <code>printer<\/code> with the event (left mouse click on the button). Before we run the program, we need to pack it. The easiest way to do that is type the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Packing<\/em><\/span><\/p>\n<pre class=\"brush:python\">but.pack()<\/pre>\n<p>If we don&#8217;t type this, the program will still run. We won&#8217;t see the button but it will be there. Lastly, the main window won&#8217;t show up if we don&#8217;t call it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Main window<\/em><\/span><\/p>\n<pre class=\"brush:python\">root.mainloop()<\/pre>\n<p>Finally, our first program will look something like this:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>basic.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\ndef printer(event):\r\n     print (\"Hi there, this button works!'\")\r\n \r\nroot = Tk()\r\nbut = Button(root)\r\nbut[\"text\"] = \"Hi there\"\r\nbut.bind(\"\",printer)\r\n \r\nbut.pack()\r\nroot.mainloop()<\/pre>\n<p>If you have ever read my article about Class Inheritance in Python, you know that I am fan of Object oriented programming. Let&#8217;s recreate the same program but use OOP!<\/p>\n<p><span style=\"text-decoration: underline;\"><em>basic.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nclass But_print:\r\n     def __init__(self):\r\n          self.but = Button(root)\r\n          self.but[\"text\"] = \"Hi there\"\r\n          self.but.bind(\"\",self.printer)\r\n          self.but.pack()\r\n     def printer(self,event):\r\n          print (\"Hi there, this button works!'\")\r\n \r\nroot = Tk()\r\nobj = But_print()\r\nroot.mainloop()\r\n<\/pre>\n<h3><a name=\"widget\"><\/a>2.2. Widget<\/h3>\n<p>Buttons have many attributes. Some of them are <code>bg<\/code> &#8211; background, <code>fg<\/code> &#8211; foreground. Here is a simple example of the button:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>button.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nbut = Button(root,\r\n          text=\"It is a button\", # text on the button\r\n          width=30,height=5, # width and height of the button\r\n          bg=\"white\",fg=\"blue\") # background and foreground\r\n \r\nbut.pack()\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>There are many ways of how text can be displayed. For example, let&#8217;s take labels.<br \/>\n<span style=\"text-decoration: underline;\"><em>Labels<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nlab = Label(root, text=\"This is a label \\n written in 2 lines\", \r\n            font=\"Arial 36\")\r\n \r\nlab.pack()\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>We can make entries by using <code>Entry<\/code> module.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Entry<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nent = Entry(root,width=20,bd=10)\r\nent.pack()\r\n\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>There is also another way to make a user input field.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Entry<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\ntex = Text(root,width=40, font=\"Verdana 12\", wrap=WORD) \r\ntex.pack()\r\n\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>We can create radio buttons.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Radio buttons<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nvar=IntVar()\r\nvar.set(1) # set the radiobutton chosen as rad1\r\nrad0 = Radiobutton(root,text=\"First\",\r\n          variable=var,value=0)\r\nrad1 = Radiobutton(root,text=\"Second\",\r\n          variable=var,value=1)\r\nrad2 = Radiobutton(root,text=\"Third\",\r\n          variable=var,value=2)\r\nrad0.pack()\r\nrad1.pack()\r\nrad2.pack()\r\n\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>We can create check buttons as easy as radio buttons.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Check buttons<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nc1 = IntVar()\r\nc2 = IntVar()\r\n\r\nche1 = Checkbutton(root,text=\"First checkbox\",\r\n          variable=c1,onvalue=1,offvalue=0)\r\nche2 = Checkbutton(root,text=\"Second checkbox\",\r\n          variable=c2,onvalue=2,offvalue=0) \r\nche1.pack()\r\nche2.pack()\r\n\r\n\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>There is probably one more feature that you need to know. Check it out for yourself!<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Lists<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nr = ['Linux','Python','Tk','Tkinter']\r\nlis = Listbox(root,selectmode=SINGLE,height=4)\r\nfor i in r:\r\n     lis.insert(END,i)\r\n\r\nlis.pack()\r\n\r\n\r\nroot.mainloop()\r\n\r\n<\/pre>\n<p>Let&#8217;s check what we can do already, shall we? I want you to try to create the program that asks for the address and gives users a chance to leave some comments. It also asks for something using radio and check buttons. If you are curious, look at the code below. It is something that has already been covered in this article and coded using OOP principles.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>interface.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nclass Form:\r\n    def __init__(self):\r\n        self.root = Tk()\r\n        self.root.title('Response form')\r\n        self.root.geometry('500x400+300+200')\r\n        self.width = 50\r\n \r\nclass Question:\r\n    def __init__(self, form, question):\r\n        self.lab = Label(form.root, text=question)\r\n        self.ent = Entry(form.root, width=form.width+16)\r\n        self.lab.pack()\r\n        self.ent.pack()\r\n \r\nclass BigQuestion:\r\n    def __init__(self, form, question):\r\n        self.lab = Label(form.root, text=question)\r\n        self.txt = Text(form.root, width=form.width, height=4)\r\n        self.lab.pack()\r\n        self.txt.pack()\r\n \r\nclass RadioBut:\r\n    def __init__(self, form, question):\r\n        self.lab = Label(form.root, text=question)\r\n        self.lab.pack()\r\n        self.var = IntVar()\r\n        self.var.set(1)\r\n        self.rad0 = Radiobutton(form.root, text=\"$\", variable=self.var,value=1)\r\n        self.rad1 = Radiobutton(form.root, text='$$', variable=self.var, value=2)\r\n        self.rad2 = Radiobutton(form.root, text='$$$', variable=self.var, value=3)\r\n        self.rad3 = Radiobutton(form.root, text='$ $ $ $', variable=self.var, value=4)\r\n        self.rad0.pack()\r\n        self.rad1.pack()\r\n        self.rad2.pack()\r\n        self.rad3.pack()\r\n \r\nclass Flags:\r\n    def __init__(self, form, question):\r\n        self.lab = Label(form.root, text=question)\r\n        self.lab.pack()\r\n        self.c0 = IntVar()\r\n        self.c1 = IntVar()\r\n        self.c2 = IntVar()\r\n        self.c3 = IntVar()\r\n        self.che0 = Checkbutton(form.root, text=\"Red\",bg='red',\r\n                                variable=self.c0, onvalue=1, offvalue=0)\r\n        self.che1 = Checkbutton(form.root, text=\"Blue\", bg='blue',\r\n                                variable=self.c1, onvalue=1, offvalue=0)\r\n        self.che2 = Checkbutton(form.root, text=\"Green\", bg='green',\r\n                                variable=self.c2, onvalue=1, offvalue=0)\r\n        self.che3 = Checkbutton(form.root, text=\"Yellow\",bg='yellow',\r\n                                variable=self.c3, onvalue=1, offvalue=0)\r\n        self.che0.pack()\r\n        self.che1.pack()\r\n        self.che2.pack()\r\n        self.che3.pack()\r\n \r\nclass Scene:\r\n    def __init__(self):\r\n        self.form = Form()\r\n        self.qstn = Question(self.form, 'Your address:')\r\n        self.comm = BigQuestion(self.form, 'Comments:')\r\n        self.radi = RadioBut(self.form, 'How much?')\r\n        self.flag = Flags(self.form, 'What color?')\r\n        self.butt = Button(self.form.root, text='Send')\r\n        self.butt.pack()\r\n        self.butt.bind('', self.go)\r\n        self.form.root.mainloop()\r\n \r\n    def go(self, event):\r\n        print('Send:\\n',\r\n              'Colors:', self.flag.c0.get(), self.flag.c1.get(),\r\n              self.flag.c2.get(), self.flag.c3.get(),\r\n              'Count:', self.radi.var.get())\r\n \r\nscene = Scene()\r\n\r\n<\/pre>\n<h3><a name=\"frame\"><\/a>2.3. Frame<\/h3>\n<p>As you may have already figured, frames are good tools to organize the application and widgets inside. Moreover, it&#8217;s wise to use it for a better app structure.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Frames<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\n \r\nfra1 = Frame(root,width=500,height=100,bg=\"darkred\")\r\nfra2 = Frame(root,width=300,height=200,bg=\"green\",bd=20)\r\nfra3 = Frame(root,width=500,height=150,bg=\"darkblue\")\r\n \r\nfra1.pack()\r\nfra2.pack()\r\nfra3.pack()\r\n \r\nroot.mainloop() \r\n<\/pre>\n<p>Let&#8217;s go ahead and just jump into a task. Let&#8217;s create a horizontal scrollbar.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>scrollbar.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\nroot = Tk()\r\nroot.title(\"Scrollbar\")\r\nroot.minsize(width=300, height=150)\r\n \r\nfr = Frame(root, width=150, height=100, bg=\"lightgreen\", bd=15)\r\nsc = Scale(fr,orient=HORIZONTAL, length=300,\r\n           from_=0, to=100, tickinterval=10,resolution=5)\r\nfr2 = Frame(root, width=400, height=100, bg=\"green\", bd=25)\r\nent = Entry(fr2, width=10)\r\n \r\nfr.pack()\r\nsc.pack()\r\nfr2.pack()\r\nent.pack()\r\n \r\nroot.mainloop()\r\n<\/pre>\n<p>That was pretty easy. Let&#8217;s just get our thinking &#8220;outside of the box&#8221;. We will create event handler for canvas. In other words, we want to draw something and change the color every time we click on it.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>canvas.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nfrom random import randrange as rnd\r\n\r\nroot = Tk()\r\nfr = Frame(root)\r\nroot.geometry('800x600')\r\n\r\nbt1 = Button(fr,text = 'Hello 1')\r\nbt2 = Button(fr,text = 'Hello 2')\r\n\r\nbt1.pack(side='left',padx=2)\r\nbt2.pack(side='left',padx=2)\r\n\r\nfr.pack(pady=5, fill = X)\r\n\r\ncanv = Canvas(root, bg = 'white')\r\ncanv.pack(fill=BOTH,expand=1)\r\n\r\ncolors = ['orange','red','blue','yellow','green']\r\n \r\n\r\ndef click(event):\r\n    global color\r\n    color = colors[rnd(len(colors))]\r\n \r\n\r\ncolor = colors[rnd(len(colors))]\r\n\r\ndef move(event):\r\n    x = event.x\r\n    y = event.y\r\n    canv.create_oval(x-30,y-30,x+30,y+30,fill=color)\r\n\r\ncanv.bind('',move)\r\ncanv.bind('<button>',click)\r\n\r\nmainloop()\r\n<\/button><\/pre>\n<h3><a name=\"event\"><\/a>2.4. Event handling<\/h3>\n<p>Sometimes, in many applications and especially in games we need to know what actions a user does. We can do it by implementing <code>bind<\/code> method. The structure is following: <code>widget.bind(event, function)<\/code>.<br \/>\nBefore we go any further, just keep in mind couple events:<\/p>\n<ul>\n<li><code>&lt;Button - 1&gt;<\/code> &#8211; left mouse click<\/li>\n<li><code>&lt;Button - 2&gt;<\/code> &#8211; middle mouse click<\/li>\n<li><code>&lt;Button - 3&gt;<\/code> &#8211; right mouse click<\/li>\n<li><code>&lt;Double - Button - 1&gt;<\/code> &#8211; double left click<\/li>\n<li><code>&lt;Motion&gt;<\/code> &#8211; mouse movements<\/li>\n<\/ul>\n<p>So basically in order to do some event handling, we just use such events.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>events.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n\r\ndef b1(event):\r\n     root.title(\"Left mouse click\")\r\n     \r\ndef b2(event):\r\n     root.title(\"Middle mouse click\")\r\n     \r\ndef b3(event):\r\n     root.title(\"Right mouse click\")\r\n     \r\ndef db1(event):\r\n    root.title(\"Double left mouse click\")\r\n    \r\ndef move(event):\r\n     root.title(\"Mouse movement\")\r\n\r\nroot = Tk()\r\nroot.minsize(width = 500, height=400)\r\n \r\nroot.bind('',b1)\r\nroot.bind('', b2)\r\nroot.bind('',b3)\r\nroot.bind('',db1)\r\nroot.bind('',move)\r\n\r\nroot.mainloop()<\/pre>\n<p>Now, if you are really careful, you understand that what we did is only mouse handling events. Surely, there are keys on keyboard and we need some handling for that too, right?<br \/>\nWell, that&#8217;s not too hard:<\/p>\n<ul>\n<li><code>&lt;Return&gt;<\/code> &#8211; Enter press<\/li>\n<li><code>&lt;Space&gt;<\/code> &#8211; Space press<\/li>\n<li><code>&lt;Control - Shift&gt;<\/code> &#8211; Ctrl and Shift press<\/li>\n<\/ul>\n<p>Another example to show you how it works:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>keys.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\n \r\ndef exit_(event):\r\n     root.destroy()\r\ndef caption(event):\r\n     t = ent.get()\r\n     lbl.configure(text = t)\r\n \r\nroot = Tk()\r\n \r\nent = Entry(root, width = 40)\r\nlbl = Label(root, width = 80)\r\n \r\nent.pack()\r\nlbl.pack()\r\n \r\nent.bind('',caption)\r\nroot.bind('',exit_)\r\n \r\nroot.mainloop()<\/pre>\n<p>Let&#8217;s head to another task. Let&#8217;s create an interface where we can use check boxes and see what&#8217;s left unchecked. By doing this task, we will revise frames and event handling. Here is my representation of how it can be done:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>keys.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">import tkinter as tk\r\n \r\nclass myapp:\r\n    def __init__(self):\r\n        self.root = tk.Tk()\r\n        self.root.title(\"Event handling\")\r\n        self.root.bind(\"q\",self.exitself)\r\n        self.text = tk.Label(self.root,text='Press \"q\" to exit\"')\r\n        self.flagsDic = {\r\n        'First':1,\r\n        'Second':2,\r\n        'Third':3,\r\n        'Fourth':4\r\n        }\r\n        for key in self.flagsDic:\r\n            self.flagsDic[key] = tk.IntVar()\r\n            Flag = tk.Checkbutton(self.root,text=key,variable=self.flagsDic[key])\r\n            Flag.grid(sticky='w')\r\n        self.ent = tk.Entry(self.root,width=20)\r\n        self.fstatus = tk.Text(self.root)\r\n        self.text.grid(row=4,column=0,columnspan=2)\r\n        self.fstatus.grid(row=0,column=1,rowspan=len(self.flagsDic))\r\n        self.fstatus.bind(\"\",self.display)\r\n    def display(self,event):\r\n        checked = []\r\n        unchecked = []\r\n        for key, value in self.flagsDic.items():\r\n            if value.get():\r\n                checked.append(key)\r\n            else:\r\n                unchecked.append(key)\r\n        if len(checked) == 0:\r\n            message = \"Unchecked:\"\r\n            for s in unchecked:\r\n                message = message + \" \" + s\r\n            print(message)\r\n        if len(unchecked) == 0:\r\n            message = \"Checked:\"\r\n            for s in checked:\r\n                message = message + \" \" + s\r\n        if len(checked) != 0 and len(unchecked) != 0:\r\n            message = \"Unchecked:\"\r\n            for s in unchecked:\r\n                message = message + \" \" + s\r\n            message = message + \"\\nChecked:\"\r\n            for s in checked:\r\n                message = message + \" \" + s\r\n        self.fstatus.delete(0.0,tk.END)\r\n        self.fstatus.insert(0.0,message)\r\n    def exitself(self,event):\r\n        self.root.destroy()\r\n \r\ndef main():\r\n    app = myapp()\r\n    app.root.mainloop()\r\n \r\nif __name__ == \"__main__\":\r\n    main()<\/pre>\n<h2><a name=\"summary\"><\/a>3. Summary<\/h2>\n<p><code>Tkinter<\/code> is a powerful library that is used for building interfaces. In order to use it, I want to put a table of what you might want to use:<\/p>\n<ul>\n<li><code>Button<\/code> &#8211; to display buttons<\/li>\n<li><code>Canvas<\/code> &#8211; to draw shapes<\/li>\n<li><code>Checkbutton<\/code> &#8211; to display number of options<\/li>\n<li><code>Entry<\/code> &#8211; to display a single-line text field<\/li>\n<li><code>Frame<\/code> &#8211; to organize other widgets<\/li>\n<li><code>Label<\/code> &#8211; to provide single-line caption<\/li>\n<li><code>Listbox<\/code> &#8211; to provide list options to a user<\/li>\n<li><code>Menubutton<\/code> &#8211; to display menus<\/li>\n<li><code>Menu<\/code> &#8211; to provide various commands to a user<\/li>\n<li><code>Message<\/code> &#8211; to display multiple text fields<\/li>\n<li><code>Radiobutton<\/code> &#8211; to display a number of options as radio buttons<\/li>\n<li><code>Scale<\/code> &#8211; to provide a slider widget<\/li>\n<li><code>Scrollbar<\/code> &#8211; to add scrolling capability<\/li>\n<li><code>Text<\/code> &#8211; to display text in multiple times<\/li>\n<li><code>Toplevel<\/code> &#8211; to provide a separate window container<\/li>\n<li><code>Spinbox<\/code> &#8211; to select from a fixed number of values<\/li>\n<li><code>PanedWindow<\/code> &#8211; it is a container widget<\/li>\n<li><code>LabelFrame<\/code> &#8211; it is a simple container widget which is used to act as a spacer<\/li>\n<li><code>tkMessageBox<\/code> &#8211; to display message boxes in your applications<\/li>\n<\/ul>\n<h2><a name=\"homework\"><\/a>4. Homework<\/h2>\n<p>One of the most easiest and still quite useful feature is calculator, right? So why not try to recreate it using <code>Tkinter<\/code>? There are 2 options how to do it:<\/p>\n<ul>\n<li>We create one window and put some RAW data there. The program itself understands what is what, and prints the output<\/li>\n<li>We create a fully functional GUI calculator<\/li>\n<\/ul>\n<p>The first option is below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>calculator_basic.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nwin = Tk()\r\nwin.title(\"Calculator\")\r\n \r\ndef test(e):\r\n    try:\r\n        lab2[\"text\"] = eval(ent.get())\r\n    except:\r\n        lab2[\"text\"] = \"Incorrect input, try again\"\r\n \r\n \r\nlab = Label(text=\"Put raw data using +, -, *, \/, \/\/, %, **\")\r\nlab.pack()\r\nent = Entry(width=50,bg=\"white\",justify=CENTER)\r\nent.focus()\r\nent.pack()\r\nent.bind('', test)\r\nlab2 = Label()\r\nlab2.pack()\r\n \r\nwin.mainloop()<\/pre>\n<p>Another version is sort of GUI calculator which is more classic:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>calculator_classic.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from tkinter import *\r\nimport parser\r\n\r\nroot = Tk()\r\nroot.title('Calculator')\r\n\r\ni = 0\r\n\r\ndef factorial():\r\n    \"\"\"Calculates the factorial of the number entered.\"\"\"\r\n    whole_string = display.get()\r\n    number = int(whole_string)\r\n    fact = 1\r\n    counter = number \r\n    try:\r\n        while counter &gt; 0:\r\n            fact = fact*counter\r\n            counter -= 1\r\n        clear_all()\r\n        display.insert(0, fact)\r\n    except Exception:\r\n        clear_all()\r\n        display.insert(0, \"Error\")\r\n\r\n\r\ndef clear_all():\r\n    \"\"\"clears all the content in the Entry widget\"\"\"\r\n    display.delete(0, END)\r\n\r\ndef get_variables(num):\r\n    \"\"\"Gets the user input for operands and puts it inside the entry widget\"\"\"\r\n    global i\r\n    display.insert(i, num)\r\n    i += 1\r\n\r\ndef get_operation(operator):\r\n    \"\"\"Gets the operand the user wants to apply on the functions\"\"\"\r\n    global i\r\n    length = len(operator)\r\n    display.insert(i, operator)\r\n    i += length\r\n\r\ndef undo():\r\n    \"\"\"removes the last entered operator\/variable from entry widget\"\"\"\r\n    whole_string = display.get()\r\n    if len(whole_string):        ## repeats until\r\n        ## now just decrement the string by one index\r\n        new_string = whole_string[:-1]\r\n        print(new_string)\r\n        clear_all()\r\n        display.insert(0, new_string)\r\n    else:\r\n        clear_all() \r\n        display.insert(0, \"Error, press AC\")\r\n\r\ndef calculate():\r\n\r\n    whole_string = display.get()\r\n    try:\r\n        formulae = parser.expr(whole_string).compile()\r\n        result = eval(formulae)\r\n        clear_all()\r\n        display.insert(0, result)\r\n    except Exception:\r\n        clear_all()\r\n        display.insert(0, \"Error!\")\r\n\r\nroot.columnconfigure(0,pad=3)\r\nroot.columnconfigure(1,pad=3)\r\nroot.columnconfigure(2,pad=3)\r\nroot.columnconfigure(3,pad=3)\r\nroot.columnconfigure(4,pad=3)\r\n\r\nroot.rowconfigure(0,pad=3)\r\nroot.rowconfigure(1,pad=3)\r\nroot.rowconfigure(2,pad=3)\r\nroot.rowconfigure(3,pad=3)\r\n\r\ndisplay = Entry(root, font = (\"Calibri\", 13))\r\ndisplay.grid(row = 1, columnspan = 6    , sticky = W+E)\r\n\r\none = Button(root, text = \"1\", command = lambda : get_variables(1), font=(\"Calibri\", 12))\r\none.grid(row = 2, column = 0)\r\ntwo = Button(root, text = \"2\", command = lambda : get_variables(2), font=(\"Calibri\", 12))\r\ntwo.grid(row = 2, column = 1)\r\nthree = Button(root, text = \"3\", command = lambda : get_variables(3), font=(\"Calibri\", 12))\r\nthree.grid(row = 2, column = 2)\r\n\r\nfour = Button(root, text = \"4\", command = lambda : get_variables(4), font=(\"Calibri\", 12))\r\nfour.grid(row = 3 , column = 0)\r\nfive = Button(root, text = \"5\", command = lambda : get_variables(5), font=(\"Calibri\", 12))\r\nfive.grid(row = 3, column = 1)\r\nsix = Button(root, text = \"6\", command = lambda : get_variables(6), font=(\"Calibri\", 12))\r\nsix.grid(row = 3, column = 2)\r\n\r\nseven = Button(root, text = \"7\", command = lambda : get_variables(7), font=(\"Calibri\", 12))\r\nseven.grid(row = 4, column = 0)\r\neight = Button(root, text = \"8\", command = lambda : get_variables(8), font=(\"Calibri\", 12))\r\neight.grid(row = 4, column = 1)\r\nnine = Button(root , text = \"9\", command = lambda : get_variables(9), font=(\"Calibri\", 12))\r\nnine.grid(row = 4, column = 2)\r\n\r\ncls = Button(root, text = \"AC\", command = clear_all, font=(\"Calibri\", 12), foreground = \"red\")\r\ncls.grid(row = 5, column = 0)\r\nzero = Button(root, text = \"0\", command = lambda : get_variables(0), font=(\"Calibri\", 12))\r\nzero.grid(row = 5, column = 1)\r\nresult = Button(root, text = \"=\", command = calculate, font=(\"Calibri\", 12), foreground = \"red\")\r\nresult.grid(row = 5, column = 2)\r\n\r\nplus = Button(root, text = \"+\", command =  lambda : get_operation(\"+\"), font=(\"Calibri\", 12))\r\nplus.grid(row = 2, column = 3)\r\nminus = Button(root, text = \"-\", command =  lambda : get_operation(\"-\"), font=(\"Calibri\", 12))\r\nminus.grid(row = 3, column = 3)\r\nmultiply = Button(root,text = \"*\", command =  lambda : get_operation(\"*\"), font=(\"Calibri\", 12))\r\nmultiply.grid(row = 4, column = 3)\r\ndivide = Button(root, text = \"\/\", command = lambda :  get_operation(\"\/\"), font=(\"Calibri\", 12))\r\ndivide.grid(row = 5, column = 3)\r\n\r\n# adding new operations\r\npi = Button(root, text = \"pi\", command = lambda: get_operation(\"*3.14\"), font =(\"Calibri\", 12))\r\npi.grid(row = 2, column = 4)\r\nmodulo = Button(root, text = \"%\", command = lambda :  get_operation(\"%\"), font=(\"Calibri\", 12))\r\nmodulo.grid(row = 3, column = 4)\r\nleft_bracket = Button(root, text = \"(\", command = lambda: get_operation(\"(\"), font =(\"Calibri\", 12))\r\nleft_bracket.grid(row = 4, column = 4)\r\nexp = Button(root, text = \"exp\", command = lambda: get_operation(\"**\"), font = (\"Calibri\", 10))\r\nexp.grid(row = 5, column = 4)\r\n\r\n## To be added :\r\n# sin, cos, log, ln\r\nundo_button = Button(root, text = \"&lt;-\", command = undo, font =(\"Calibri\", 12), foreground = \"red\")\r\nundo_button.grid(row = 2, column = 5)\r\nfact = Button(root, text = \"x!\", command = factorial, font=(\"Calibri\", 12))\r\nfact.grid(row = 3, column = 5)\r\nright_bracket = Button(root, text = \")\", command = lambda: get_operation(\")\"), font =(\"Calibri\", 12))\r\nright_bracket.grid(row = 4, column = 5)\r\nsquare = Button(root, text = \"^2\", command = lambda: get_operation(\"**2\"), font = (\"Calibri\", 10))\r\nsquare.grid(row = 5, column = 5)\r\n\r\nroot.mainloop()<\/pre>\n<h2><a name=\"download\"><\/a>5. Download the source code<\/h2>\n<p>You can find all materials needed in the file below.<\/p>\n<div class=\"download\"><strong>Downoload<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/08\/Python-Tkinter-tutorial.zip\"><strong>Python-Tkinter-tutorial.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ulp id=&#8217;R7QVpFMZmjosABLC&#8217;] Table Of Contents 1. What is Tkinter? 1.1. More about Tkinter 1.2. Installing Tkinter 2. Examples 2.1. Basic examples 2.2. Widget 2.3. Frame 2.4. Event &hellip;<\/p>\n","protected":false},"author":657,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[483,484,290,482,485],"class_list":["post-18138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-gui","tag-interface","tag-python","tag-tkinter","tag-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Tkinter Tutorial - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tkinter Tutorial - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-09T13:15:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:29:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aleksandr Krasnov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aleksandr Krasnov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\"},\"author\":{\"name\":\"Aleksandr Krasnov\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\"},\"headline\":\"Python Tkinter Tutorial\",\"datePublished\":\"2017-08-09T13:15:01+00:00\",\"dateModified\":\"2018-01-09T07:29:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\"},\"wordCount\":1583,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"keywords\":[\"gui\",\"interface\",\"python\",\"tkinter\",\"tutorial\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\",\"name\":\"Python Tkinter Tutorial - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2017-08-09T13:15:01+00:00\",\"dateModified\":\"2018-01-09T07:29:06+00:00\",\"description\":\"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Tkinter Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\",\"name\":\"Aleksandr Krasnov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"caption\":\"Aleksandr Krasnov\"},\"description\":\"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tkinter Tutorial - Web Code Geeks - 2026","description":"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python Tkinter Tutorial - Web Code Geeks - 2026","og_description":"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2017-08-09T13:15:01+00:00","article_modified_time":"2018-01-09T07:29:06+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Aleksandr Krasnov","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Aleksandr Krasnov","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/"},"author":{"name":"Aleksandr Krasnov","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373"},"headline":"Python Tkinter Tutorial","datePublished":"2017-08-09T13:15:01+00:00","dateModified":"2018-01-09T07:29:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/"},"wordCount":1583,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","keywords":["gui","interface","python","tkinter","tutorial"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/","name":"Python Tkinter Tutorial - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2017-08-09T13:15:01+00:00","dateModified":"2018-01-09T07:29:06+00:00","description":"Today we will be talking about one python library that is used for GUI programming which is Tkinter. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-tkinter-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Tkinter Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373","name":"Aleksandr Krasnov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","caption":"Aleksandr Krasnov"},"description":"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.","url":"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/18138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/657"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=18138"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/18138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=18138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=18138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=18138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}