<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Harsh Prateek</title>
    <description>The latest articles on Forem by Harsh Prateek (@harshprateek).</description>
    <link>https://forem.com/harshprateek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F757569%2F95961b76-8482-4940-979a-6d485c26117c.jpg</url>
      <title>Forem: Harsh Prateek</title>
      <link>https://forem.com/harshprateek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/harshprateek"/>
    <language>en</language>
    <item>
      <title>Keep GUI in order: Tkinter Grid layout</title>
      <dc:creator>Harsh Prateek</dc:creator>
      <pubDate>Tue, 08 Nov 2022 12:39:51 +0000</pubDate>
      <link>https://forem.com/harshprateek/keep-gui-in-order-tkinter-grid-layout-394e</link>
      <guid>https://forem.com/harshprateek/keep-gui-in-order-tkinter-grid-layout-394e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Ok, this is going to be a short article. In this one, I am about to tell you about grid in Tkinter window object and how you can use it to place the widgets according to your needs in the window.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a grid in Tkinter 🤔❓
&lt;/h2&gt;

&lt;p&gt;You can consider a grid in Tkinter as an intersection of horizontal and vertical lines, making boxes which can store widgets to store data. It is just like the spreadsheet in MS Excel or Google Sheets.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to grab one of them 🤔❓
&lt;/h2&gt;

&lt;p&gt;To grab one of the grid box and put content in it, you just have to use this syntax:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tkinter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Widget"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above label would be placed in the 1st row of the first column. Remember, row and column values start at 0, thus the label would be placed in the 2nd row and column of the window.&lt;/p&gt;

&lt;p&gt;The window would create rows and column as per the requirement of the content. This process is done using the grid() method. We also have to mention the window object in the declaration of the widget. We don't need the pack method since the rendering is done by the grid method itself.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making a layout using grid😄❕
&lt;/h1&gt;

&lt;p&gt;Enough talking, let's make something to understand the point of using the grid method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# import tkinter module
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;tkinter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;tkinter.ttk&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="c1"&gt;# creating main tkinter window/toplevel
&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Tk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# this will create a label widget
&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Height"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Width"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# grid method to arrange labels in respective
# rows and columns as specified
&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# entry widgets, used to take entry from user
&lt;/span&gt;&lt;span class="n"&gt;e1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# this will arrange entry widgets
&lt;/span&gt;&lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# checkbutton widget
&lt;/span&gt;&lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Checkbutton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Preserve"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;columnspan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# adding image (remember image should be PNG and not JPG)
&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PhotoImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="s"&gt;"C:\Users\Admin\Pictures\capture1.png"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;img1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subsample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# setting image with the help of label
&lt;/span&gt;&lt;span class="n"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;columnspan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rowspan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;padx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# button widget
&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Zoom in"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Zoom out"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# arranging button widgets
&lt;/span&gt;&lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# infinite loop which can be terminated
# by keyboard or mouse interrupt
&lt;/span&gt;&lt;span class="n"&gt;mainloop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the GUI that is rendered by running the code:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pHK7T0NM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1ovtirw9dz4ya2waulf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pHK7T0NM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1ovtirw9dz4ya2waulf.png" alt="Output of the code" width="300" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know I used some of the features of Tkinter that I have not discussed yet but I though this is the perfect oppurtunity to provide a preview of the later posts.&lt;/p&gt;

&lt;p&gt;This is a grid with 3 rows and 4 columns. The Entry label, i.e the input box is spanned to two columns while the others are the kept in only one column. The image is also spanned, but it is spanned in both row and column, making a square image.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:-
&lt;/h2&gt;

&lt;p&gt;This was it guys, it was everything about the Tkinter grid tutorial. I have also started learing &lt;strong&gt;flask&lt;/strong&gt; so maybe the next post is going to be about &lt;strong&gt;Flask&lt;/strong&gt; framework in Python. Hope you would enjoy that as well. &lt;br&gt;
Follow me for more content like this, and subscribe to my newsletter so that you never miss anything I post. &lt;/p&gt;

&lt;p&gt;Hope you have a great day😉.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Make Pythonic GUI: Widgets in Tkinter</title>
      <dc:creator>Harsh Prateek</dc:creator>
      <pubDate>Tue, 01 Nov 2022 15:40:07 +0000</pubDate>
      <link>https://forem.com/harshprateek/make-pythonic-gui-widgets-in-tkinter-3g1e</link>
      <guid>https://forem.com/harshprateek/make-pythonic-gui-widgets-in-tkinter-3g1e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;So, in my &lt;a href="https://dev.to/harshprateek/make-pythonic-gui-with-tkinter-tkinter-tutorial-27oh"&gt;previous post&lt;/a&gt;, I had talked about developing GUI applications with the help of Tkinter framework of Python. In this post, I am gonna tell you about &lt;strong&gt;widgets&lt;/strong&gt; present in Tkinter that provide the UI and the interactivity to our GUI application. Let's begin😃&lt;/p&gt;

&lt;h2&gt;
  
  
  What are widgets🤔?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Normally, we consider a widget as an element of Graphical User Interface of a device that either displays some important information, or let's user interact with the OS easily.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For a user, this description is perfect, but for us developers, there is a different meaning of a widget, especially in Tkinter. Here, a widget is an object of the Tkinter framework that can render graphical elements and have special functions to manipulate the look and functionality of the widget.&lt;/p&gt;

&lt;p&gt;We have already used one of the widgets to make our window object, this is how we did it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;tkinter&lt;/span&gt;

&lt;span class="c1"&gt;# Here, the Tk is a widget that will 
# render the window of the GUI application.
&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tkinter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mainloop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Tk() is a widget that would render a window and this is the way we are suppose to call and implement the widgets:- &lt;code&gt;variable = tkinter.widget()&lt;/code&gt; if we have named imported tkinter.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many widgets are there in Tkinter🤔?
&lt;/h2&gt;

&lt;p&gt;For number's sake, there are 19 widgets present in the Tkinter framework. Each of them have different use case and when used together, can create a robust and good-looking UI for any desktop application. Here is a list of all the widgets present in Tkinter.&lt;br&gt;
&lt;strong&gt;Button, Canvas, CheckButton, Entry, Frame, Label, Listbox, Menu, Menubutton, Message, Radiobutton, Scale, Text, Toplevel, SpinBox, PanedWindow, LabelFrame, MessageBox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this post, I am gonna use two of the widgets from the massive list that I mentioned above. The two would be &lt;strong&gt;Text&lt;/strong&gt; and &lt;strong&gt;Button&lt;/strong&gt;. I am also gonna show you how can a widget be styled in any way you want by changing the background color, text color, and the font of the widgets. Let's get started with the code now😉.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This line imports everything from the module.
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;tkinter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Tk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# This line sets the title of the GUI Application
&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NotePad"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# This line make the window acquire the fullscreen mode by default.
&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"zoomed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# This is the implementation of a button that can close a window.
&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
         &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Close"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;foreground&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"white"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
         &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"red"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Times"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"bold"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# This line creates a textbox in the window.
&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="c1"&gt;# This line is manipulates the font and text of the textbox
&lt;/span&gt;    &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Times"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"italic"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
    &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mainloop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation of the above mentioned codebase:
&lt;/h2&gt;

&lt;p&gt;I know that it can be overwhelming to see all that code at once without any proper explanation but now the explanation would be much easier to understand. Here it is then: &lt;/p&gt;

&lt;h3&gt;
  
  
  Button widget:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
         &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Close"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;foreground&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"white"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
         &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"red"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
         &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Times"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"bold"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this explanation is about the button widget, but this pattern is true for every widget in tkinter.&lt;/p&gt;

&lt;p&gt;The Button widget as the name suggests, renders a clickable button in the GUI that can be assigned any function. &lt;br&gt;
Here is a breakdown of the arguments taken by the widget:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first argument it took is the &lt;strong&gt;text&lt;/strong&gt; that is to be shown inside the widget, in this case, &lt;strong&gt;Close&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second argument is the function that the button has to perform. We have to assign the &lt;strong&gt;command&lt;/strong&gt; to the function that is to be perform, in this case, I want to close the window, which can be achieved with &lt;strong&gt;window.destroy&lt;/strong&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Third and fourth arguments set the text-color with &lt;strong&gt;foreground&lt;/strong&gt; and the background color with the &lt;strong&gt;background&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The fourth one is the argument that can manipulate the font of the widget. It takes a tuple in which, the first value is a string that is the font that is to be used. Second value is the text size and the third value is the font weight.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it folks. This was the entire breakdown of the Button widget, now let's make the textbox that I promised we would and that is present in the codebase above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Text Widget:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="c1"&gt;# This line is manipulates the font and text of the textbox
&lt;/span&gt;    &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Times"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"italic"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I already explained everything above, I am not gonna waste any more of your time reading nonsense online. &lt;/p&gt;

&lt;p&gt;The text widget creates a textarea in which the user can type plain text as long as he want and it can be saved in the form of paragraphs, instead of long strings without newline character. You can consider it similar to the notepad that comes with Windows OS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finished Product:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CwlHqkLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g48k8ulvu4hurn2kgi26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CwlHqkLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g48k8ulvu4hurn2kgi26.png" alt="Image of the GUI that is created with teh codebase" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One last thing, you would have to pack the variable in which you made the widget in the window. This is done with the &lt;code&gt;pack()&lt;/code&gt; function. This is how you can do it &lt;code&gt;button.pack()&lt;/code&gt;. Without this, you would not be able to see the widget on the window.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the post about widget's in Tkinter framework. Follow me if you like my work and subscribe to the newsletter so that you would always get the article in your inbox. Hope you have a great day😉.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Make Pythonic GUI with Tkinter: Tkinter tutorial</title>
      <dc:creator>Harsh Prateek</dc:creator>
      <pubDate>Sun, 30 Oct 2022 14:48:33 +0000</pubDate>
      <link>https://forem.com/harshprateek/make-pythonic-gui-with-tkinter-tkinter-tutorial-27oh</link>
      <guid>https://forem.com/harshprateek/make-pythonic-gui-with-tkinter-tkinter-tutorial-27oh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;This post is all about how to get started with GUI programming with Python and how to use Tkinter framework of Python to make exceptional GUI tools for windows, mac and linux devices. By the end of this long post(Not too long though😅), you can at get started with Tkinter and understand what it is all about. Consider it as an introduction to GUI Development and to Tkinter. Let's get started now.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Tkinter?
&lt;/h2&gt;

&lt;p&gt;Ok, so basically Tkinter is a framework of Python that come bundled with the Python language and can be used without installing any 3rd party dependencies. It is used to make GUI applications in Python and has native support for making desktop applications of &lt;strong&gt;Windows&lt;/strong&gt;, &lt;strong&gt;Linux&lt;/strong&gt;, and &lt;strong&gt;MacOS&lt;/strong&gt;. Since it is present in the python package natively, it is the most popular framework for making GUI applications in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to get started with Tkinter?
&lt;/h2&gt;

&lt;p&gt;To start making GUI apps, we first have to import the Tkinter module in our program environment. This is how we can get it done: &lt;code&gt;import tkinter as tk&lt;/code&gt;.&lt;br&gt;
This way we can get the module in our program to be used. Let's start a window with our tkinter framework.&lt;/p&gt;
&lt;h3&gt;
  
  
  Making a window object with Tkinter:
&lt;/h3&gt;

&lt;p&gt;To make a window object, we have to use the Tk() function of the tkinter module. If we want to make a window with the name &lt;strong&gt;window&lt;/strong&gt;, we can do it like this: &lt;code&gt;window = tk.Tk()&lt;/code&gt;. Since I have imported the module by the name tk, I can use this method easily, but this can also be done this way: &lt;code&gt;window = tkinter.Tk()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;After doing all this we would (if not for any bugs!😆) get a window if we would run the code. Although if at this point you would run the code, the code would execute perfectly without showing any window, this is not a bug, the window did open but it instantly closes. You can say that it does not get the time to show up in front you😅.&lt;/p&gt;

&lt;p&gt;To keep the window open, we would need the &lt;code&gt;mainloop()&lt;/code&gt; function of the window object.This is how we can do it: &lt;code&gt;window.mainloop()&lt;/code&gt;. The mainloop function keeps our window open and thus we can use it for making our GUI.&lt;/p&gt;

&lt;p&gt;This is the code so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import tkinter as tk

# Used to make the window object that would contain all the function necessary to make GUI.
window = tk.Tk() 

# Would keep the window open and prevent it from closing until we do it.
window.mainloop()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If, at this point you would run the code, this is what you are going to see.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f5OIGuID--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvrbsqk9ama1vipg7vfd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f5OIGuID--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvrbsqk9ama1vipg7vfd.png" alt="Image of the window object which is rendered when the code is executed" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A completely blank screen which was not there until we run the code. This is the screen which we made. Congratulations on making your first ever GUI screen🎉🎊🎉. From now on we would meddle with this screen to see what more we can do with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can the window object do?
&lt;/h2&gt;

&lt;p&gt;So, as I have previously said that the window object is like the playground where all the stuff we make as GUI would exist. That being said, we can still make a few changes to our window object, it has some amazing function with which, we can change it's size, color, title or even set the minimum and maximum size of the window. Let's have a look at each one of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Title:
&lt;/h3&gt;

&lt;p&gt;To change the title of our window we can use the &lt;strong&gt;title()&lt;/strong&gt; function. It takes a string as argument and then use the string as the title of the window. This is how it is done &lt;code&gt;window.title("title of the window")&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Background Color:
&lt;/h3&gt;

&lt;p&gt;We can also change the background color of the window. The background color of the window is like a dictionary in the window object, thus it is to be changed the same we change the value of a key-value pair in Python. This is how it is done: &lt;code&gt;window['bg'] = "green"&lt;/code&gt;, this line would set the background color as green. &lt;/p&gt;

&lt;h3&gt;
  
  
  Dimensions:
&lt;/h3&gt;

&lt;p&gt;To open the window in fullscreen mode by default, we can achieve this by changing the state to &lt;strong&gt;zoomed&lt;/strong&gt;. This is how it is done: &lt;code&gt;window.state("zoomed")&lt;/code&gt;. Now, the window would open in the fullscreen mode by default. By this point, this is how our codebase looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import tkinter as tk

# Used to make the window object that would contain all the function necessary to make GUI.
window = tk.Tk() 

# Sets the title of the window.
window.title("GUI with Python")

# Sets the background color to green
window['bg'] = "Green"

# Opens the window in fullscreen by default.
window.state("zoomed")

# Would keep the window open and prevent it from closing until we do it.
window.mainloop()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would appreciate if you would take the time to meddle with the code and execute it on your own system. Trust me, it would be worth it😉.&lt;/p&gt;

&lt;p&gt;I hope you liked this little tutorial which I made on Tkinter, although I say it was on tkinter, it is more like the start of a series in which I would everyday post an article about tkinter, thus I thought it was best to start it with nothing but the building block of whole thing GUI, the window object. &lt;/p&gt;

&lt;p&gt;In the next one, I am gonna post about what are widgets and how to use widgets to make your application unique and great. I hope you enjoyed this little tutorial (although it was pretty boring tbh😅).&lt;/p&gt;

&lt;p&gt;If you followed this far in the tutorial, I would ask of you to follow me and subscribe to my newsletter so that you never miss any article that I post about Python and Tkinter. Hope you to have a great day😊.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
