avatarEsteban Thilliez

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4456

Abstract

ange the cursor appearance when the mouse cursor is over the frame.</li><li><b>height</b></li><li><b>padding</b></li><li><b>relief</b>: Specify the relief style for the border. It needs <code>borderwidth</code> to be set to be effective.</li><li><b>style</b></li><li><b>takefocus</b>: A boolean value specifies whether the frame is visited during focus traversal. By default, it is <code>False</code>. So the frame widget does not accept focus.</li><li><b>width</b></li></ul><p id="9710">Some parameters don’t need comments as their purpose is obvious.</p><p id="8e34">An example of a Frame with parameters can be:</p><div id="2194"><pre>frame = ttk.Frame(main_window, padding=(15, 20), <span class="hljs-attribute">relief</span>=<span class="hljs-string">'sunken'</span>, <span class="hljs-attribute">height</span>=300, <span class="hljs-attribute">width</span>=300)</pre></div><ul><li><b>padding</b>: there are many ways to provide this argument. You can use a single value if you want all padding to be the same (ex: <code>padding=5</code> ), you can use a tuple to specify left-right and top-bottom padding as above, or you can use a tuple containing 4 values to specify the padding of each side individually (<code>padding=(left, top, right, bottom</code> ).</li><li><b>height/width</b>: if you don’t specify any values for these, they will be calculated automatically.</li></ul><p id="05e6">Frames can be a bit hard to understand as their only purpose is to contain other widgets, but this is also what makes them so important. Don’t worry, in the next story, we’ll probably see an example of a small Tkinter application you can build to practice.</p><h2 id="2249">Text</h2><p id="b3bc">The Text widget is used to display an editable text area.</p><div id="5781"><pre><span class="hljs-built_in">text</span> = tk.<span class="hljs-built_in">Text</span>(container, **params)</pre></div><p id="0717">You can access its text using <code>text.get(start, end)</code> . You can insert text using <code>text.insert(start, text)</code> . You can also delete text using <code>text.delete(start, end)</code> .</p><p id="abce">Positions are given using Tkinter constants (<code>tk.END</code> , <code>tk.START</code>), strings ( <code>"start"</code> , <code>"end"</code> ), or using tuples following this format: <code>(row, column)</code> .</p><p id="23bf">For example:</p><div id="a1c3"><pre>text_area = tk.Text(frame, <span class="hljs-attribute">width</span>=40, <span class="hljs-attribute">height</span>=10) text_area.insert(tk.END, <span class="hljs-string">"Hello World!"</span>) text_area.pack()

<span class="hljs-built_in">print</span>(text_area.<span class="hljs-built_in">get</span>(<span class="hljs-string">"1.0"</span>, tk.END))</pre></div><div id="d3fd"><pre><span class="hljs-meta prompt_">...</span></pre></div><div id="cce6"><pre>Hello World!</pre></div><figure id="f564"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GF0-iSIg_T4_l_Az5K-LIw.png"><figcaption></figcaption></figure><p id="9a2e">You can also disable this widget to make users unable to edit its content using the <code>state</code> property:</p><div id="a700"><pre>text_area<span class="hljs-selector-attr">[<span class="hljs-string">"state"</span>]</span> = tk<span class="hljs-selector-class">.NORMAL</span> text_area<span class="hljs-selector-attr">[<span class="hljs-string">"state"</span>]</span> = tk.DISABLED</pre></div><h2 id="74c3">Checkbutton</h2><p id="4a32">It’s a widget displaying a checkable box. You can execute commands when the checkbox is checked or not, and when its state changes. Below is a sample checkbox to understand its keywords arguments:</p><div id="9540"><pre>is_checked = tk.BooleanVar() checkbox = ttk.Checkbutton(frame, <span class="hljs-attribute">text</span>=<span class="hljs-string">"Check me!"</span>, <span class="hljs-attribute">variable</span>=is_checked, <span class="hljs-attribute">onvalue</span>=<span class="hljs-literal">True</span>, <span class="hljs-attribute">offvalue</span>=<span class="hljs-literal">False</span>, <span class="hljs-attribute">command</span>=lambda: <span class="hljs-built_in">print</span>(is_checked.<span class="hljs-built_in">get</span>()))</pre></div><ul><li><b>variable</b>: a variable storing the actual value of the checkbox.</li><li><b>onvalue</b>: the variable value when the box is checked.</li><li><b>offvalue</b>: the variable value when the box is not checked.</li><li><b>comm

Options

and</b>: a function executed when the checkbox’s state changes.</li></ul><figure id="dc84"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yM9V5WQIWfaFRYf1a28bhA.png"><figcaption></figcaption></figure><h2 id="556b">Slider</h2><p id="b467">Using a Slider, you can change a value by moving an indicator.</p><div id="9505"><pre>current_value = tk.IntVar() slider = ttk.Scale(frame, <span class="hljs-attribute">from_</span>=0, <span class="hljs-attribute">to</span>=100, <span class="hljs-attribute">orient</span>=tk.HORIZONTAL, <span class="hljs-attribute">command</span>=lambda value: <span class="hljs-built_in">print</span>(current_value.<span class="hljs-built_in">get</span>()), <span class="hljs-attribute">variable</span>=current_value)</pre></div><p id="e2ce">Like before, you can provide a variable to this widget whose value will be updated each time the Slider’s value changes. You can also provide a command executed when the Slider’s value changes.</p><figure id="f4d1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UZA6mrZTXS2tuEBwgPO1Zg.png"><figcaption></figcaption></figure><h2 id="2fec">Spinbox</h2><p id="0550">It’s like a Slider, except you don’t change its value with a drag and drop.</p><div id="6489"><pre>spinbox_value = tk.IntVar() spinbox = ttk.Spinbox(frame, <span class="hljs-attribute">from_</span>=0, <span class="hljs-attribute">to</span>=100, <span class="hljs-attribute">command</span>=lambda: <span class="hljs-built_in">print</span>(spinbox_value.<span class="hljs-built_in">get</span>()), <span class="hljs-attribute">textvariable</span>=spinbox_value)</pre></div><p id="5c97"><code>textvariable</code> is a parameter used to specify the variable displayed by the widget.</p><figure id="fe4c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*S_0_ragmOcLjllKs420BAA.png"><figcaption></figcaption></figure><h2 id="a9d3">Final Note</h2><p id="4ec6">There are many Tkinter widgets, each one with its well-defined purpose. In this story, we have discovered 5 of them. You also know some widgets such as a Label or a Button if you’ve followed the series.</p><p id="4f12">But there are still a lot of widgets remaining. I’ll talk about some other widgets in the next story. In the meantime, I recommend you to experiment with the widgets you know to practice and try to create small applications.</p><p id="1fd4">Also, be sure to follow me so that you don’t miss the next story about other widgets!</p><p id="aef7"><i>You can find the other stories of this series here:</i></p><div id="c77a" class="link-block"> <a href="https://readmedium.com/build-user-interfaces-with-python-a757dadfbd5b"> <div> <div> <h2>Build User Interfaces with Python</h2> <div><h3>Python is a programming language having multiple applications, mostly in data science or web development. But it can…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*vecQXv6kz7U0QCj0)"></div> </div> </div> </a> </div><p id="24e7"><i>To explore more of my Python stories, click <a href="https://readmedium.com/tech-aa824bad0d67">here</a>!</i></p><p id="e697"><i>If you liked the story, don’t forget to clap and maybe follow me if you want to explore more of my content :)</i></p><p id="7f8d"><i>You can also subscribe to me via email to be notified every time I publish a new story, just click <a href="https://medium.com/subscribe/@estebanthi">here</a>!</i></p><p id="8079"><i>If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:</i></p><div id="23b1" class="link-block"> <a href="https://medium.com/@estebanthi/membership"> <div> <div> <h2>Join Medium with my referral link — Esteban Thilliez</h2> <div><h3>Read every story from Esteban Thilliez (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*IoN4BofrwCNWA_bS)"></div> </div> </div> </a> </div></article></body>

How to Build User Interfaces with Python — Tkinter Widgets — Part. 1

Photo by Brett Jordan on Unsplash

This story follows the User Interfaces series. If you have missed the last Tkinter story, you can find it here:

What Is a Widget

Tkinter, and most of the UI libraries or frameworks existing work on a widget system. That’s why it’s so important to understand what is a widget.

A widget is an element of a GUI that can be used to display information, structure a UI, or give the possibility to the user to interact with the application.

In Tkinter, everything is a widget, except the top-level window containing the application.

If we take for example a calculator app, each button is a widget, the display of the result is a widget, and the frame containing all these elements is a widget too.

How to Use Widgets in Tkinter

I’ve already talked about this in the first story of this series, so I won’t go into too much detail here. But just a quick reminder, here is the syntax to create and use a widget:

from tkinter import ttk
label = ttk.Label(main_window, "Hellow World")
label.pack()

You can also bind commands to widgets:

def clicked():
    print("Button clicked!")
button = ttk.Button(main_window, text="Hello World!", command=clicked)

You can also bind more complex commands to widgets using events:

main_window.bind("<Button>", lambda event: callback("Button clicked!"))

For more detail about all these things, check this: How to Build User Interfaces with Python — Tkinter Basics.

Main Tkinter Widgets

Tkinter provides a lot of widgets for you to work with. It allows you to don’t have to code anything you need, because when you need to create a Tkinter application, chances are that the widgets you need are already integrated into Tkinter. We’ll review the main widgets, so the ones you will use most of the time.

Frame

To create a Frame, you use the ttk.Frame class. Like all the widgets, the first parameter is the container of the Frame, then you can provide optional keywords arguments:

  • borderwidth
  • class_
  • cursor: Change the cursor appearance when the mouse cursor is over the frame.
  • height
  • padding
  • relief: Specify the relief style for the border. It needs borderwidth to be set to be effective.
  • style
  • takefocus: A boolean value specifies whether the frame is visited during focus traversal. By default, it is False. So the frame widget does not accept focus.
  • width

Some parameters don’t need comments as their purpose is obvious.

An example of a Frame with parameters can be:

frame = ttk.Frame(main_window, padding=(15, 20), relief='sunken', height=300, width=300)
  • padding: there are many ways to provide this argument. You can use a single value if you want all padding to be the same (ex: padding=5 ), you can use a tuple to specify left-right and top-bottom padding as above, or you can use a tuple containing 4 values to specify the padding of each side individually (padding=(left, top, right, bottom ).
  • height/width: if you don’t specify any values for these, they will be calculated automatically.

Frames can be a bit hard to understand as their only purpose is to contain other widgets, but this is also what makes them so important. Don’t worry, in the next story, we’ll probably see an example of a small Tkinter application you can build to practice.

Text

The Text widget is used to display an editable text area.

text = tk.Text(container, **params)

You can access its text using text.get(start, end) . You can insert text using text.insert(start, text) . You can also delete text using text.delete(start, end) .

Positions are given using Tkinter constants (tk.END , tk.START), strings ( "start" , "end" ), or using tuples following this format: (row, column) .

For example:

text_area = tk.Text(frame, width=40, height=10)
text_area.insert(tk.END, "Hello World!")
text_area.pack()

print(text_area.get("1.0", tk.END))
...
Hello World!

You can also disable this widget to make users unable to edit its content using the state property:

text_area["state"] = tk.NORMAL
text_area["state"] = tk.DISABLED

Checkbutton

It’s a widget displaying a checkable box. You can execute commands when the checkbox is checked or not, and when its state changes. Below is a sample checkbox to understand its keywords arguments:

is_checked = tk.BooleanVar()
checkbox = ttk.Checkbutton(frame, text="Check me!", variable=is_checked, onvalue=True, offvalue=False, command=lambda: print(is_checked.get()))
  • variable: a variable storing the actual value of the checkbox.
  • onvalue: the variable value when the box is checked.
  • offvalue: the variable value when the box is not checked.
  • command: a function executed when the checkbox’s state changes.

Slider

Using a Slider, you can change a value by moving an indicator.

current_value = tk.IntVar()
slider = ttk.Scale(frame, from_=0, to=100, orient=tk.HORIZONTAL, command=lambda value: print(current_value.get()), variable=current_value)

Like before, you can provide a variable to this widget whose value will be updated each time the Slider’s value changes. You can also provide a command executed when the Slider’s value changes.

Spinbox

It’s like a Slider, except you don’t change its value with a drag and drop.

spinbox_value = tk.IntVar()
spinbox = ttk.Spinbox(frame, from_=0, to=100, command=lambda: print(spinbox_value.get()), textvariable=spinbox_value)

textvariable is a parameter used to specify the variable displayed by the widget.

Final Note

There are many Tkinter widgets, each one with its well-defined purpose. In this story, we have discovered 5 of them. You also know some widgets such as a Label or a Button if you’ve followed the series.

But there are still a lot of widgets remaining. I’ll talk about some other widgets in the next story. In the meantime, I recommend you to experiment with the widgets you know to practice and try to create small applications.

Also, be sure to follow me so that you don’t miss the next story about other widgets!

You can find the other stories of this series here:

To explore more of my Python stories, click here!

If you liked the story, don’t forget to clap and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:

Python
Software Development
User Interface
Coding
Programming
Recommended from ReadMedium