Cara menggunakan python gui builder

Python has a lot of GUI frameworks, but Tkinter is the only framework that’s built into the Python standard library. Tkinter has several strengths. It’s cross-platform, so the same code works on Windows, macOS, and Linux. Visual elements are rendered using native operating system elements, so applications built with Tkinter look like they belong on the platform where they’re run.

Although Tkinter is considered the de facto Python GUI framework, it’s not without criticism. One notable criticism is that GUIs built with Tkinter look outdated. If you want a shiny, modern interface, then Tkinter may not be what you’re looking for.

However, Tkinter is lightweight and relatively painless to use compared to other frameworks. This makes it a compelling choice for building GUI applications in Python, especially for applications where a modern sheen is unnecessary, and the top priority is to quickly build something that’s functional and cross-platform.

In this tutorial, you’ll learn how to:

  • Get started with Tkinter with a Hello, World application
  • Work with widgets, such as buttons and text boxes
  • Control your application layout with geometry managers
  • Make your applications interactive by associating button clicks with Python functions

Note: This tutorial is adapted from the chapter “Graphical User Interfaces” of Python Basics: A Practical Introduction to Python 3.

The book uses Python’s built-in IDLE editor to create and edit Python files and interact with the Python shell. In this tutorial, references to IDLE have been removed in favor of more general language.

The bulk of the material in this tutorial has been left unchanged, and you should have no problems running the example code from the editor and environment of your choice.

Once you’ve mastered these skills by working through the exercises at the end of each section, you’ll tie everything together by building two applications. The first is a temperature converter, and the second is a text editor. It’s time to dive right in and learn how to build an application with Tkinter!

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Take the Quiz: Test your knowledge with our interactive “Python GUI Programming With Tkinter” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Building Your First Python GUI Application With Tkinter

The foundational element of a Tkinter GUI is the window. Windows are the containers in which all other GUI elements live. These other GUI elements, such as text boxes, labels, and buttons, are known as widgets. Widgets are contained inside of windows.

First, create a window that contains a single widget. Start up a new session and follow along!

Note: The code examples in this tutorial have all been tested on Windows, macOS, and Ubuntu Linux 20.04 with Python version 3.10.

If you’ve installed Python with the official installers available for Windows and macOS from python.org, then you should have no problem running the sample code. You can safely skip the rest of this note and continue with the tutorial!

If you haven’t installed Python with the official installers, or there’s no official distribution for your system, then here are some tips for getting up and going.

Python on macOS with Homebrew:

The Python distribution for macOS available on Homebrew doesn’t come bundled with the Tcl/Tk dependency required by Tkinter. The default system version is used instead. This version may be outdated and prevent you from importing the Tkinter module. To avoid this problem, use the official macOS installer.

Ubuntu Linux 20.04:

To conserve memory space, the default version of the Python interpreter that comes pre-installed on Ubuntu Linux 20.04 has no support for Tkinter. However, if you want to continue using the Python interpreter bundled with your operating system, then install the following package:

$ sudo apt-get install python3-tk

This installs the Python GUI Tkinter module.

Other Linux Flavors:

If you’re unable to get a working Python installation on your flavor of Linux, then you can build Python with the correct version of Tcl/Tk from the source code. For a step-by-step walk-through of this process, check out the . You may also try using pyenv to manage multiple Python versions.

With your Python shell open, the first thing you need to do is import the Python GUI Tkinter module:

>>>

>>> import tkinter as tk

A window is an instance of Tkinter’s

>>> import tkinter as tk
02 class. Go ahead and create a new window and assign it to the variable
>>> import tkinter as tk
03:

>>>

>>> window = tk.Tk()

When you execute the above code, a new window pops up on your screen. How it looks depends on your operating system:

Cara menggunakan python gui builder

Throughout the rest of this tutorial, you’ll see Windows screenshots.

Remove ads

Adding a Widget

Now that you have a window, you can add a widget. Use the

>>> import tkinter as tk
04 class to add some text to a window. Create a
>>> import tkinter as tk
05 widget with the text
>>> import tkinter as tk
06 and assign it to a variable called
>>> import tkinter as tk
07:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")

The window you created earlier doesn’t change. You just created a

>>> import tkinter as tk
05 widget, but you haven’t added it to the window yet. There are several ways to add widgets to a window. Right now, you can use the
>>> import tkinter as tk
05 widget’s
>>> import tkinter as tk
10 method:

>>>

>>> greeting.pack()

The window now looks like this:

Cara menggunakan python gui builder

When you pack a widget into a window, Tkinter sizes the window as small as it can be while still fully encompassing the widget. Now execute the following:

>>>

>>> window.mainloop()

Nothing seems to happen, but notice that no new prompt appears in the shell.

>>> import tkinter as tk
11 tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method. Go ahead and close the window you’ve created, and you’ll see a new prompt displayed in the shell.

Warning: When you work with Tkinter from a Python REPL, updates to windows are applied as each line is executed. This is not the case when a Tkinter program is executed from a Python file!

If you don’t include

>>> import tkinter as tk
11 at the end of a program in a Python file, then the Tkinter application will never run, and nothing will be displayed. Alternatively, you can build your user interface incrementally in Python REPL by calling
>>> import tkinter as tk
13 after each step to reflect the change.

Creating a window with Tkinter only takes a couple of lines of code. But blank windows aren’t very useful! In the next section, you’ll learn about some of the widgets available in Tkinter, and how you can customize them to meet your application’s needs.

Check Your Understanding

Expand the code blocks below to check your understanding:

Exercise: Create a Tkinter windowShow/Hide

Write a full Python script that creates a Tkinter window with the text

>>> import tkinter as tk
14.

The window should look like this:

Cara menggunakan python gui builder

Try this exercise now.

You can expand the code block below to see a solution:

Solution: Create a Tkinter windowShow/Hide

Here’s one possible solution:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()

Keep in mind your code may look different.

When you’re ready, you can move on to the next section.

Working With Widgets

Widgets are the bread and butter of the Python GUI framework Tkinter. They’re the elements through which users interact with your program. Each widget in Tkinter is defined by a class. Here are some of the widgets available:

Widget ClassDescription

>>> import tkinter as tk
05A widget used to display text on the screen
>>> import tkinter as tk
16A button that can contain text and can perform an action when clicked
>>> import tkinter as tk
17A text entry widget that allows only a single line of text
>>> import tkinter as tk
18A text entry widget that allows multiline text entry
>>> import tkinter as tk
19A rectangular region used to group related widgets or provide padding between widgets

You’ll see how to work with each of these in the following sections, but keep in mind that Tkinter has many more widgets than those listed here. The widget’s choice gets even more complicated when you account for a whole new set of themed widgets. In the remaining part of this tutorial, you’re only going to use Tkinter’s classic widgets, though.

If you’d like to learn more about the two widget types, then you can expand the collapsible section below:

Classic vs Themed WidgetsShow/Hide

It’s worth noting that there are currently two broad categories of widgets in Tkinter:

  1. Classic widgets: Available in the
    >>> import tkinter as tk
    
    20 package, for example
    >>> import tkinter as tk
    
    21
  2. Themed widgets: Available in the
    >>> import tkinter as tk
    
    22 submodule, for example
    >>> import tkinter as tk
    
    23

Tkinter’s classic widgets are highly customizable and straightforward, but they tend to appear dated or somewhat foreign on most platforms today. If you’d like to take advantage of widgets with a native look and feel familiar to users of a given operating system, then you might want to check out the themed widgets.

Most of the themed widgets are near drop-in replacements for their legacy counterparts, but with a more modern look. You can also use a few brand-new widgets, such as the , which weren’t available in Tkinter before. At the same time, you’ll need to continue using some of the classic widgets that don’t have a themed alternative.

Note: Themed widgets in the

>>> import tkinter as tk
24 module use the operating system’s native look and feel by default. However, you can change their theme for a customized visual appearance, such as light and dark modes. A theme is a collection of reusable style definitions, which you can think of as a Cascading Style Sheet (CSS) for Tkinter.

Making the new widgets themable meant extracting most of their style information into separate objects. On the one hand, such a separation of concerns is a desired property in the library’s design, but on the other hand, it introduces an additional abstraction layer, which makes themed widgets more difficult to style than the classic ones.

When working with regular and themed widgets in Tkinter, it’s customary to declare the following aliases for the Tkinter packages and modules:

>>>

>>> import tkinter as tk
>>> import tkinter.ttk as ttk

Aliases like this let you explicitly refer to either

>>> import tkinter as tk
04 or
>>> import tkinter as tk
26, for example, in one program depending on your needs:

>>>

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>

However, you may sometimes find it more convenient to use a wildcard import (

>>> import tkinter as tk
27) to automatically override all legacy widgets with the themed ones where possible, like so:

>>>

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>

Now, you don’t have to prefix the widget’s class name with its corresponding Python module. You’ll always create a themed widget as long as it’s available, or you’ll fall back to the classic widget otherwise. The two import statements above must be placed in the specified order to have an effect. Because of that, wildcard imports are considered a bad practice, which should generally be avoided unless used consciously.

For a full list of Tkinter widgets, check out Basic Widgets and More Widgets in the TkDocs tutorial. Even though it describes themed widgets introduced in Tcl/Tk 8.5, most of the information there should also apply to the classic widgets.

Fun Fact: Tkinter literally stands for “Tk interface” because it’s a Python binding or a programming interface to the Tk library in the Tcl scripting language.

For now, take a closer look at the

>>> import tkinter as tk
05 widget.

Remove ads

Displaying Text and Images With >>> import tkinter as tk 05 Widgets

>>> import tkinter as tk
05 widgets are used to display text or images. The text displayed by a
>>> import tkinter as tk
05 widget can’t be edited by the user. It’s for display purposes only. As you saw in the example at the beginning of this tutorial, you can create a
>>> import tkinter as tk
05 widget by instantiating the
>>> import tkinter as tk
05 class and passing a string to the
>>> import tkinter as tk
34 parameter:

>>> import tkinter as tk
0

>>> import tkinter as tk
05 widgets display text with the default system text color and the default system text background color. These are typically black and white, respectively, but you may see different colors if you’ve changed these settings in your operating system.

You can control

>>> import tkinter as tk
05 text and background colors using the
>>> import tkinter as tk
37 and
>>> import tkinter as tk
38 parameters:

>>> import tkinter as tk
1

There are numerous valid color names, including:

  • >>> import tkinter as tk
    
    39
  • >>> import tkinter as tk
    
    40
  • >>> import tkinter as tk
    
    41
  • >>> import tkinter as tk
    
    42
  • >>> import tkinter as tk
    
    43
  • >>> import tkinter as tk
    
    44

Many of the HTML color names work with Tkinter. For a full reference, including macOS- and Windows-specific system colors that the current system theme controls, check out the colors manual page.

You can also specify a color using :

>>> import tkinter as tk
2

This sets the label background to a nice, light blue color. Hexadecimal RGB values are more cryptic than named colors, but they’re also more flexible. Fortunately, there are tools available that make getting hexadecimal color codes relatively painless.

If you don’t feel like typing out

>>> import tkinter as tk
37 and
>>> import tkinter as tk
38 all the time, then you can use the shorthand
>>> import tkinter as tk
47 and
>>> import tkinter as tk
48 parameters to set the foreground and background colors:

>>> import tkinter as tk
3

You can also control the width and height of a label with the

>>> import tkinter as tk
49 and
>>> import tkinter as tk
50 parameters:

>>> import tkinter as tk
4

Here’s what this label looks like in a window:

Cara menggunakan python gui builder

It may seem strange that the label in the window isn’t square even though the width and height are both set to

>>> import tkinter as tk
51. This is because the width and height are measured in text units. One horizontal text unit is determined by the width of the character
>>> import tkinter as tk
52, or the number zero, in the default system font. Similarly, one vertical text unit is determined by the height of the character
>>> import tkinter as tk
52.

Note: For width and height measurements, Tkinter uses text units, instead of something like inches, centimeters, or pixels, to ensure consistent behavior of the application across platforms.

Measuring units by the width of a character means that the size of a widget is relative to the default font on a user’s machine. This ensures the text fits properly in labels and buttons, no matter where the application is running.

Labels are great for displaying some text, but they don’t help you get input from a user. The next three widgets that you’ll learn about are all used to get user input.

Remove ads

Displaying Clickable Buttons With >>> import tkinter as tk 16 Widgets

>>> import tkinter as tk
16 widgets are used to display clickable buttons. You can configure them to call a function whenever they’re clicked. You’ll cover how to call functions from button clicks in the next section. For now, take a look at how to create and style a button.

There are many similarities between

>>> import tkinter as tk
16 and
>>> import tkinter as tk
05 widgets. In many ways, a button is just a label that you can click! The same keyword arguments that you use to create and style a
>>> import tkinter as tk
05 will work with
>>> import tkinter as tk
16 widgets. For example, the following code creates a button with a blue background and yellow text. It also sets the width and height to
>>> import tkinter as tk
60 and
>>> import tkinter as tk
61 text units, respectively:

>>> import tkinter as tk
5

Here’s what the button looks like in a window:

Cara menggunakan python gui builder

Pretty nifty! You can use the next two widgets to collect text input from a user.

Getting User Input With >>> import tkinter as tk 17 Widgets

When you need to get a little bit of text from a user, like a name or an email address, use an

>>> import tkinter as tk
17 widget. It’ll display a small text box that the user can type some text into. Creating and styling an
>>> import tkinter as tk
17 widget works pretty much exactly like with
>>> import tkinter as tk
05 and
>>> import tkinter as tk
16 widgets. For example, the following code creates a widget with a blue background, some yellow text, and a width of
>>> import tkinter as tk
67 text units:

>>> import tkinter as tk
6

The interesting bit about

>>> import tkinter as tk
17 widgets isn’t how to style them, though. It’s how to use them to get input from a user. There are three main operations that you can perform with
>>> import tkinter as tk
17 widgets:

  1. Retrieving text with
    >>> import tkinter as tk
    
    70
  2. Deleting text with
    >>> import tkinter as tk
    
    71
  3. Inserting text with
    >>> import tkinter as tk
    
    72

The best way to get an understanding of

>>> import tkinter as tk
17 widgets is to create one and interact with it. Open up a Python shell and follow along with the examples in this section. First, import
>>> import tkinter as tk
20 and create a new window:

>>>

>>> import tkinter as tk
7

Now create a

>>> import tkinter as tk
05 and an
>>> import tkinter as tk
17 widget:

>>>

>>> import tkinter as tk
8

The

>>> import tkinter as tk
05 describes what sort of text should go in the
>>> import tkinter as tk
17 widget. It doesn’t enforce any sort of requirements on the
>>> import tkinter as tk
17, but it tells the user what your program expects them to put there. You need to
>>> import tkinter as tk
10 the widgets into the window so that they’re visible:

>>>

>>> import tkinter as tk
9

Here’s what that looks like:

Cara menggunakan python gui builder

Notice that Tkinter automatically centers the label above the

>>> import tkinter as tk
17 widget in the window. This is a feature of
>>> import tkinter as tk
10, which you’ll learn more about in later sections.

Click inside the

>>> import tkinter as tk
17 widget with your mouse and type
>>> import tkinter as tk
84:

Cara menggunakan python gui builder

Now you’ve got some text entered into the

>>> import tkinter as tk
17 widget, but that text hasn’t been sent to your program yet. You can use
>>> import tkinter as tk
70 to retrieve the text and assign it to a variable called
>>> import tkinter as tk
87:

>>>

>>> window = tk.Tk()
0

You can delete text as well. This

>>> import tkinter as tk
71 method takes an integer argument that tells Python which character to remove. For example, the code block below shows how
>>> import tkinter as tk
89 deletes the first character from
>>> import tkinter as tk
17:

>>>

>>> window = tk.Tk()
1

The text remaining in the widget is now

>>> import tkinter as tk
91:

Cara menggunakan python gui builder

Note that, just like Python string objects, text in an

>>> import tkinter as tk
17 widget is indexed starting with
>>> import tkinter as tk
52.

If you need to remove several characters from an

>>> import tkinter as tk
17, then pass a second integer argument to
>>> import tkinter as tk
71 indicating the index of the character where deletion should stop. For example, the following code deletes the first four letters in
>>> import tkinter as tk
17:

>>>

>>> window = tk.Tk()
2

The remaining text now reads

>>> import tkinter as tk
97:

Cara menggunakan python gui builder

>>> import tkinter as tk
98 works just like . The first argument determines the starting index, and the deletion continues up to but not including the index passed as the second argument. Use the special constant
>>> import tkinter as tk
99 for the second argument of
>>> import tkinter as tk
71 to remove all text in
>>> import tkinter as tk
17:

>>>

>>> window = tk.Tk()
3

You’ll now see a blank text box:

Cara menggunakan python gui builder

On the opposite end of the spectrum, you can also insert text into an

>>> import tkinter as tk
17 widget:

>>>

>>> window = tk.Tk()
4

The window now looks like this:

Cara menggunakan python gui builder

The first argument tells

>>> import tkinter as tk
72 where to insert the text. If there’s no text in
>>> import tkinter as tk
17, then the new text will always be inserted at the beginning of the widget, no matter what value you pass as the first argument. For example, calling
>>> import tkinter as tk
72 with
>>> window = tk.Tk()
06 as the first argument instead of
>>> import tkinter as tk
52, as you did above, would’ve generated the same output.

If

>>> import tkinter as tk
17 already contains some text, then
>>> import tkinter as tk
72 will insert the new text at the specified position and shift all existing text to the right:

>>>

>>> window = tk.Tk()
5

The widget text now reads

>>> import tkinter as tk
84:

Cara menggunakan python gui builder

>>> import tkinter as tk
17 widgets are great for capturing small amounts of text from a user, but because they’re only displayed on a single line, they’re not ideal for gathering large amounts of text. That’s where
>>> import tkinter as tk
18 widgets come in!

Remove ads

Getting Multiline User Input With >>> import tkinter as tk 18 Widgets

>>> import tkinter as tk
18 widgets are used for entering text, just like
>>> import tkinter as tk
17 widgets. The difference is that
>>> import tkinter as tk
18 widgets may contain multiple lines of text. With a
>>> import tkinter as tk
18 widget, a user can input a whole paragraph or even several pages of text! Just like with
>>> import tkinter as tk
17 widgets, you can perform three main operations with
>>> import tkinter as tk
18 widgets:

  1. Retrieve text with
    >>> import tkinter as tk
    
    70
  2. Delete text with
    >>> import tkinter as tk
    
    71
  3. Insert text with
    >>> import tkinter as tk
    
    72

Although the method names are the same as the

>>> import tkinter as tk
17 methods, they work a bit differently. It’s time to get your hands dirty by creating a
>>> import tkinter as tk
18 widget and seeing what it can do.

Note: Do you still have the window from the previous section open?

If so, then you can close it by executing the following:

>>>

>>> window = tk.Tk()
6

You can also close it manually by clicking the Close button.

In your Python shell, create a new blank window and pack a

>>> window = tk.Tk()
25 widget into it:

>>>

>>> window = tk.Tk()
7

Text boxes are much larger than

>>> import tkinter as tk
17 widgets by default. Here’s what the window created above looks like:

Cara menggunakan python gui builder

Click anywhere inside the window to activate the text box. Type in the word

>>> window = tk.Tk()
27. Then press Enter and type
>>> window = tk.Tk()
28 on the second line. The window should now look like this:

Cara menggunakan python gui builder

Just like with

>>> import tkinter as tk
17 widgets, you can retrieve the text from a
>>> import tkinter as tk
18 widget using
>>> import tkinter as tk
70. However, calling
>>> import tkinter as tk
70 with no arguments doesn’t return the full text in the text box like it does for
>>> import tkinter as tk
17 widgets. It raises an exception:

>>>

>>> window = tk.Tk()
8

>>> window = tk.Tk()
34 requires at least one argument. Calling
>>> import tkinter as tk
70 with a single index returns a single character. To retrieve several characters, you need to pass a start index and an end index. Indices in
>>> import tkinter as tk
18 widgets work differently than in
>>> import tkinter as tk
17 widgets. Since
>>> import tkinter as tk
18 widgets can have several lines of text, an index must contain two pieces of information:

  1. The line number of a character
  2. The position of a character on that line

Line numbers start with

>>> window = tk.Tk()
39, and character positions start with
>>> import tkinter as tk
52. To make an index, you create a string of the form
>>> window = tk.Tk()
41, replacing
>>> window = tk.Tk()
42 with the line number and
>>> window = tk.Tk()
43 with the character number. For example,
>>> window = tk.Tk()
44 represents the first character on the first line, and
>>> window = tk.Tk()
45 represents the fourth character on the second line.

Use the index

>>> window = tk.Tk()
44 to get the first letter from the text box that you created earlier:

>>>

>>> window = tk.Tk()
9

There are five letters in the word

>>> window = tk.Tk()
27, and the character number of
>>> window = tk.Tk()
48 is
>>> window = tk.Tk()
49, since character numbers start from
>>> import tkinter as tk
52, and the word
>>> window = tk.Tk()
27 starts at the first position in the text box. Just like with Python string slices, in order to get the entire word
>>> window = tk.Tk()
27 from the text box, the end index must be one more than the index of the last character to be read.

So, to get the word

>>> window = tk.Tk()
27 from the text box, use
>>> window = tk.Tk()
44 for the first index and
>>> window = tk.Tk()
55 for the second index:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
0

To get the word

>>> window = tk.Tk()
28 on the second line of the text box, change the line numbers in each index to
>>> window = tk.Tk()
57:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
1

To get all of the text in a text box, set the starting index in

>>> window = tk.Tk()
44 and use the special
>>> import tkinter as tk
99 constant for the second index:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
2

Notice that text returned by

>>> import tkinter as tk
70 includes any newline characters. You can also see from this example that every line in a
>>> import tkinter as tk
18 widget has a newline character at the end, including the last line of text in the text box.

>>> import tkinter as tk
71 is used to delete characters from a text box. It works just like
>>> import tkinter as tk
71 for
>>> import tkinter as tk
17 widgets. There are two ways to use
>>> import tkinter as tk
71:

  1. With a single argument
  2. With two arguments

Using the single-argument version, you pass to

>>> import tkinter as tk
71 the index of a single character to be deleted. For example, the following deletes the first character,
>>> window = tk.Tk()
67, from the text box:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
3

The first line of text in the window now reads

>>> window = tk.Tk()
68:

Cara menggunakan python gui builder

With the two-argument version, you pass two indices to delete a range of characters starting at the first index and up to, but not including, the second index.

For example, to delete the remaining

>>> window = tk.Tk()
68 on the first line of the text box, use the indices
>>> window = tk.Tk()
44 and
>>> window = tk.Tk()
71:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
4

Notice that the text is gone from the first line. This leaves a blank line followed the word

>>> window = tk.Tk()
28 on the second line:

Cara menggunakan python gui builder

Even though you can’t see it, there’s still a character on the first line. It’s a newline character! You can verify this using

>>> import tkinter as tk
70:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
5

If you delete that character, then the rest of the contents of the text box will shift up a line:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
3

Now,

>>> window = tk.Tk()
28 is on the first line of the text box:

Cara menggunakan python gui builder

Try to clear out the rest of the text in the text box. Set

>>> window = tk.Tk()
44 as the start index and use
>>> import tkinter as tk
99 for the second index:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
7

The text box is now empty:

Cara menggunakan python gui builder

You can insert text into a text box using

>>> import tkinter as tk
72:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
8

This inserts the word

>>> window = tk.Tk()
27 at the beginning of the text box, using the same
>>> window = tk.Tk()
79 format used by
>>> import tkinter as tk
70 to specify the insertion position:

Cara menggunakan python gui builder

Check out what happens if you try to insert the word

>>> window = tk.Tk()
28 on the second line:

>>>

>>> greeting = tk.Label(text="Hello, Tkinter")
9

Instead of inserting the text on the second line, the text is inserted at the end of the first line:

Cara menggunakan python gui builder

If you want to insert text onto a new line, then you need to insert a newline character manually into the string being inserted:

>>>

>>> greeting.pack()
0

Now

>>> window = tk.Tk()
28 is on the second line of the text box:

Cara menggunakan python gui builder

>>> import tkinter as tk
72 will do one of two things:

  1. Insert text at the specified position if there’s already text at or after that position.
  2. Append text to the specified line if the character number is greater than the index of the last character in the text box.

It’s usually impractical to try and keep track of what the index of the last character is. The best way to insert text at the end of a

>>> import tkinter as tk
18 widget is to pass
>>> import tkinter as tk
99 to the first parameter of
>>> import tkinter as tk
72:

>>>

>>> greeting.pack()
1

Don’t forget to include the newline character (

>>> window = tk.Tk()
87) at the beginning of the text if you want to put it on a new line:

>>>

>>> greeting.pack()
2

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17, and
>>> import tkinter as tk
18 widgets are just a few of the widgets available in Tkinter. There are several others, including widgets for checkboxes, radio buttons, scroll bars, and progress bars. For more information on all of the available widgets, see the Additional Widgets list in the section.

Remove ads

Assigning Widgets to Frames With >>> import tkinter as tk 19 Widgets

In this tutorial, you’re going to work with only five widgets:

  1. >>> import tkinter as tk
    
    05
  2. >>> import tkinter as tk
    
    16
  3. >>> import tkinter as tk
    
    17
  4. >>> import tkinter as tk
    
    18
  5. >>> import tkinter as tk
    
    19

These are the four you’ve seen so far plus the

>>> import tkinter as tk
19 widget.
>>> import tkinter as tk
19 widgets are important for organizing the layout of your widgets in an application.

Before you get into the details about laying out the visual presentation of your widgets, take a closer look at how

>>> import tkinter as tk
19 widgets work, and how you can assign other widgets to them. The following script creates a blank
>>> import tkinter as tk
19 widget and assigns it to the main application window:

>>> greeting.pack()
3

>>> greeting = tk.Label(text="Hello, Tkinter")
02 packs the frame into the window so that the window sizes itself as small as possible to encompass the frame. When you run the above script, you get some seriously uninteresting output:

Cara menggunakan python gui builder

An empty

>>> import tkinter as tk
19 widget is practically invisible. Frames are best thought of as containers for other widgets. You can assign a widget to a frame by setting the widget’s
>>> greeting = tk.Label(text="Hello, Tkinter")
04 attribute:

>>> greeting.pack()
4

To get a feel for how this works, write a script that creates two

>>> import tkinter as tk
19 widgets called
>>> greeting = tk.Label(text="Hello, Tkinter")
06 and
>>> greeting = tk.Label(text="Hello, Tkinter")
07. In this script,
>>> greeting = tk.Label(text="Hello, Tkinter")
06 contains a label with the text
>>> greeting = tk.Label(text="Hello, Tkinter")
09, and
>>> greeting = tk.Label(text="Hello, Tkinter")
07 contains the label
>>> greeting = tk.Label(text="Hello, Tkinter")
11. Here’s one way to do this:

>>> greeting.pack()
5

Note that

>>> greeting = tk.Label(text="Hello, Tkinter")
06 is packed into the window before
>>> greeting = tk.Label(text="Hello, Tkinter")
07. The window that opens shows the label in
>>> greeting = tk.Label(text="Hello, Tkinter")
06 above the label in
>>> greeting = tk.Label(text="Hello, Tkinter")
07:

Cara menggunakan python gui builder

Now see what happens when you swap the order of

>>> greeting = tk.Label(text="Hello, Tkinter")
16 and
>>> greeting = tk.Label(text="Hello, Tkinter")
17:

>>> greeting.pack()
6

The output looks like this:

Cara menggunakan python gui builder

Now

>>> greeting = tk.Label(text="Hello, Tkinter")
18 is on top. Since
>>> greeting = tk.Label(text="Hello, Tkinter")
18 is assigned to
>>> greeting = tk.Label(text="Hello, Tkinter")
07, it moves to wherever
>>> greeting = tk.Label(text="Hello, Tkinter")
07 is positioned.

All four of the widget types that you’ve learned about—

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17, and
>>> import tkinter as tk
18—have a
>>> greeting = tk.Label(text="Hello, Tkinter")
04 attribute that’s set when you instantiate them. That way, you can control which
>>> import tkinter as tk
19 a widget is assigned to.
>>> import tkinter as tk
19 widgets are great for organizing other widgets in a logical manner. Related widgets can be assigned to the same frame so that, if the frame is ever moved in the window, then the related widgets stay together.

Note: If you omit the

>>> greeting = tk.Label(text="Hello, Tkinter")
04 argument when creating a new widget instance, then it’ll be placed inside of the top-level window by default.

In addition to grouping your widgets logically,

>>> import tkinter as tk
19 widgets can add a little flare to the visual presentation of your application. Read on to see how to create various borders for
>>> import tkinter as tk
19 widgets.

Remove ads

Adjusting Frame Appearance With Reliefs

>>> import tkinter as tk
19 widgets can be configured with a
>>> greeting = tk.Label(text="Hello, Tkinter")
33 attribute that creates a border around the frame. You can set
>>> greeting = tk.Label(text="Hello, Tkinter")
33 to be any of the following values:

  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    35: Has no border effect (the default value)
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    36: Creates a sunken effect
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    37: Creates a raised effect
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    38: Creates a grooved border effect
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    39: Creates a ridged effect

To apply the border effect, you must set the

>>> greeting = tk.Label(text="Hello, Tkinter")
40 attribute to a value greater than
>>> window = tk.Tk()
39. This attribute adjusts the width of the border in pixels. The best way to get a feel for what each effect looks like is to see them for yourself. Here’s a script that packs five
>>> import tkinter as tk
19 widgets into a window, each with a different value for the
>>> greeting = tk.Label(text="Hello, Tkinter")
33 argument:

>>> greeting.pack()
7

Here’s a breakdown of this script:

  • Lines 3 to 9 create a dictionary whose keys are the names of the different relief effects available in Tkinter. The values are the corresponding Tkinter objects. This dictionary is assigned to the

    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    44 variable.

  • Line 13 starts a

    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    45 loop to loop over each item in the
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    44 dictionary.

  • Line 14 creates a new

    >>> import tkinter as tk
    
    19 widget and assigns it to the
    >>> import tkinter as tk
    
    03 object. The
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    33 attribute is set to the corresponding relief in the
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    44 dictionary, and the
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    51 attribute is set to
    >>> import tkinter as tk
    
    61 so that the effect is visible.

  • Line 15 packs the

    >>> import tkinter as tk
    
    19 into the window using
    >>> import tkinter as tk
    
    10. The
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    55 keyword argument tells Tkinter in which direction to pack the
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    56 objects. You’ll see more about how this works in the next section.

  • Lines 16 and 17 create a

    >>> import tkinter as tk
    
    05 widget to display the name of the relief and pack it into the
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    56 object you just created.

The window produced by the above script looks like this:

Cara menggunakan python gui builder

In this image, you can see the following effects:

  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    35 creates a frame that appears to be flat.
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    36 adds a border that gives the frame the appearance of being sunken into the window.
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    37 gives the frame a border that makes it appear to stick out from the screen.
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    38 adds a border that appears as a sunken groove around an otherwise flat frame.
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    39 gives the appearance of a raised lip around the edge of the frame.

These effects give your Python GUI Tkinter application a bit of visual appeal.

Understanding Widget Naming Conventions

When you create a widget, you can give it any name you like, as long as it’s a valid Python identifier. It’s usually a good idea to include the name of the widget class in the variable name that you assign to the widget instance. For example, if a

>>> import tkinter as tk
05 widget is used to display a user’s name, then you might name the widget
>>> greeting = tk.Label(text="Hello, Tkinter")
65. An
>>> import tkinter as tk
17 widget used to collect a user’s age might be called
>>> greeting = tk.Label(text="Hello, Tkinter")
67.

Note: Sometimes, you may define a new widget without assigning it to a variable. You’ll call its

>>> import tkinter as tk
10 method directly on the same line of code:

>>>

>>> greeting.pack()
8

This might be helpful when you don’t intend to refer to the widget’s instance later on. Due to automatic memory management, Python would normally such unassigned objects, but Tkinter prevents that by registering every new widget internally.

When you include the widget class name in the variable name, you help yourself and anyone else who needs to read your code to understand what type of widget the variable name refers to. However, using the full name of the widget class can lead to long variable names, so you may want to adopt a shorthand for referring to each widget type. For the rest of this tutorial, you’ll use the following shorthand prefixes to name widgets:

Widget ClassVariable Name PrefixExample

>>> import tkinter as tk
05
>>> greeting = tk.Label(text="Hello, Tkinter")
70
>>> greeting = tk.Label(text="Hello, Tkinter")
71
>>> import tkinter as tk
16
>>> greeting = tk.Label(text="Hello, Tkinter")
73
>>> greeting = tk.Label(text="Hello, Tkinter")
74
>>> import tkinter as tk
17
>>> greeting = tk.Label(text="Hello, Tkinter")
76
>>> greeting = tk.Label(text="Hello, Tkinter")
77
>>> import tkinter as tk
18
>>> greeting = tk.Label(text="Hello, Tkinter")
79
>>> greeting = tk.Label(text="Hello, Tkinter")
80
>>> import tkinter as tk
19
>>> greeting = tk.Label(text="Hello, Tkinter")
82
>>> greeting = tk.Label(text="Hello, Tkinter")
83

In this section, you learned how to create a window, use widgets, and work with frames. At this point, you can make some plain windows that display messages, but you’ve yet to create a full-blown application. In the next section, you’ll learn how to control the layout of your applications using Tkinter’s powerful geometry managers.

Check Your Understanding

Expand the code block below for an exercise to check your understanding:

Exercise: Create an Entry widget and insert some textShow/Hide

Write a complete script that displays an

>>> import tkinter as tk
17 widget that’s 40 text units wide and has a white background and black text. Use
>>> import tkinter as tk
72 to display text in the widget that reads
>>> greeting = tk.Label(text="Hello, Tkinter")
86.

The output window should look like this:

Cara menggunakan python gui builder

Try this exercise now.

You can expand the code block below to see a solution:

Solution: Create an Entry widget and insert some textShow/Hide

There are a couple of ways to solve this exercise. Here’s one solution that uses the

>>> import tkinter as tk
48 and
>>> import tkinter as tk
47 parameters to set the
>>> import tkinter as tk
17 widget’s background and foreground colors:

>>> greeting.pack()
9

This solution is great because it explicitly sets the background and foreground colors for the

>>> import tkinter as tk
17 widget.

On most systems, the default background color for an

>>> import tkinter as tk
17 widget is white, and the default foreground color is black. So, you might be able to generate the same window with the
>>> import tkinter as tk
48 and
>>> import tkinter as tk
47 parameters left out:

>>> window.mainloop()
0

Keep in mind your code may look different.

When you’re ready, you can move on to the next section.

Remove ads

Controlling Layout With Geometry Managers

Up until now, you’ve been adding widgets to windows and

>>> import tkinter as tk
19 widgets using
>>> import tkinter as tk
10, but you haven’t learned what exactly this method does. Let’s clear things up! Application layout in Tkinter is controlled with geometry managers. While
>>> import tkinter as tk
10 is an example of a geometry manager, it isn’t the only one. Tkinter has two others:

  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    97
  • >>> greeting = tk.Label(text="Hello, Tkinter")
    
    98

Each window or

>>> import tkinter as tk
19 in your application can use only one geometry manager. However, different frames can use different geometry managers, even if they’re assigned to a frame or window using another geometry manager. Start by taking a closer look at
>>> import tkinter as tk
10.

The >>> import tkinter as tk 10 Geometry Manager

The

>>> import tkinter as tk
10 geometry manager uses a packing algorithm to place widgets in a
>>> import tkinter as tk
19 or window in a specified order. For a given widget, the packing algorithm has two primary steps:

  1. Compute a rectangular area called a parcel that’s just tall (or wide) enough to hold the widget and fills the remaining width (or height) in the window with blank space.
  2. Center the widget in the parcel unless a different location is specified.

>>> import tkinter as tk
10 is powerful, but it can be difficult to visualize. The best way to get a feel for
>>> import tkinter as tk
10 is to look at some examples. See what happens when you
>>> import tkinter as tk
10 three
>>> import tkinter as tk
05 widgets into a
>>> import tkinter as tk
19:

>>> window.mainloop()
1

>>> import tkinter as tk
10 places each
>>> import tkinter as tk
19 below the previous one by default, in the order that they’re assigned to the window:

Cara menggunakan python gui builder

Each

>>> import tkinter as tk
19 is placed at the topmost available position. Therefore, the red
>>> import tkinter as tk
19 is placed at the top of the window. Then the yellow
>>> import tkinter as tk
19 is placed just below the red one and the blue
>>> import tkinter as tk
19 just below the yellow one.

There are three invisible parcels, each containing one of the three

>>> import tkinter as tk
19 widgets. Each parcel is as wide as the window and as tall as the
>>> import tkinter as tk
19 that it contains. Because no anchor point was specified when
>>> import tkinter as tk
10 was called for each
>>> greeting.pack()
18 they’re all centered inside of their parcels. That’s why each
>>> import tkinter as tk
19 is centered in the window.

>>> import tkinter as tk
10 accepts some keyword arguments for more precisely configuring widget placement. For example, you can set the
>>> greeting.pack()
21 keyword argument to specify in which direction the frames should fill. The options are
>>> greeting.pack()
22 to fill in the horizontal direction,
>>> greeting.pack()
23 to fill vertically, and
>>> greeting.pack()
24 to fill in both directions. Here’s how you would stack the three frames so that each one fills the whole window horizontally:

>>> window.mainloop()
2

Notice that the

>>> import tkinter as tk
49 is not set on any of the
>>> import tkinter as tk
19 widgets.
>>> import tkinter as tk
49 is no longer necessary because each frame sets
>>> import tkinter as tk
10 to fill horizontally, overriding any width you may set.

The window produced by this script looks like this:

Cara menggunakan python gui builder

One of the nice things about filling the window with

>>> import tkinter as tk
10 is that the fill is responsive to window resizing. Try widening the window generated by the previous script to see how this works. As you widen the window, the width of the three
>>> import tkinter as tk
19 widgets grow to fill the window:

Cara menggunakan python gui builder

Notice, though, that the

>>> import tkinter as tk
19 widgets don’t expand in the vertical direction.

The

>>> greeting = tk.Label(text="Hello, Tkinter")
55 keyword argument of
>>> import tkinter as tk
10 specifies on which side of the window the widget should be placed. These are the available options:

  • >>> greeting.pack()
    
    34
  • >>> greeting.pack()
    
    35
  • >>> greeting.pack()
    
    36
  • >>> greeting.pack()
    
    37

If you don’t set

>>> greeting = tk.Label(text="Hello, Tkinter")
55, then
>>> import tkinter as tk
10 will automatically use
>>> greeting.pack()
34 and place new widgets at the top of the window, or at the topmost portion of the window that isn’t already occupied by a widget. For example, the following script places three frames side by side from left to right and expands each frame to fill the window vertically:

>>> window.mainloop()
3

This time, you have to specify the

>>> import tkinter as tk
50 keyword argument on at least one of the frames to force the window to have some height.

The resulting window looks like this:

Cara menggunakan python gui builder

Just like when you set

>>> greeting.pack()
42 to make the frames responsive when you resized the window horizontally, you can set
>>> greeting.pack()
43 to make the frames responsive when you resize the window vertically:

Cara menggunakan python gui builder

To make the layout truly responsive, you can set an initial size for your frames using the

>>> import tkinter as tk
49 and
>>> import tkinter as tk
50 attributes. Then, set the
>>> greeting.pack()
21 keyword argument of
>>> import tkinter as tk
10 to
>>> greeting.pack()
24 and set the
>>> greeting.pack()
49 keyword argument to
>>> greeting.pack()
50:

>>> window.mainloop()
4

When you run the above script, you’ll see a window that initially looks the same as the one you generated in the previous example. The difference is that now you can resize the window however you want, and the frames will expand and fill the window responsively:

Cara menggunakan python gui builder

Pretty cool!

Remove ads

The >>> greeting = tk.Label(text="Hello, Tkinter") 97 Geometry Manager

You can use

>>> greeting = tk.Label(text="Hello, Tkinter")
97 to control the precise location that a widget should occupy in a window or
>>> import tkinter as tk
19. You must provide two keyword arguments,
>>> greeting.pack()
54 and
>>> greeting.pack()
55, which specify the x- and y-coordinates for the top-left corner of the widget. Both
>>> greeting.pack()
54 and
>>> greeting.pack()
55 are measured in pixels, not text units.

Keep in mind that the origin, where

>>> greeting.pack()
54 and
>>> greeting.pack()
55 are both
>>> import tkinter as tk
52, is the top-left corner of the
>>> import tkinter as tk
19 or window. So, you can think of the
>>> greeting.pack()
55 argument of
>>> greeting = tk.Label(text="Hello, Tkinter")
97 as the number of pixels from the top of the window, and the
>>> greeting.pack()
54 argument as the number of pixels from the left edge of the window.

Here’s an example of how the

>>> greeting = tk.Label(text="Hello, Tkinter")
97 geometry manager works:

>>> window.mainloop()
5

Here’s how this code works:

  • Lines 5 and 6 create a new
    >>> import tkinter as tk
    
    19 widget called
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    56, measuring
    >>> greeting.pack()
    
    68 pixels wide and
    >>> greeting.pack()
    
    68 pixels tall, and pack it into the window with
    >>> import tkinter as tk
    
    10.
  • Lines 8 and 9 create a new
    >>> import tkinter as tk
    
    05 called
    >>> greeting.pack()
    
    72 with a red background and place it in
    >>> greeting.pack()
    
    73 at position (0, 0).
  • Lines 11 and 12 create a second
    >>> import tkinter as tk
    
    05 called
    >>> greeting.pack()
    
    75 with a yellow background and place it in
    >>> greeting.pack()
    
    73 at position (75, 75).

Here’s the window that the code produces:

Cara menggunakan python gui builder

Note that if you run this code on a different operating system that uses different font sizes and styles, then the second label might become partially obscured by the window’s edge. That’s why

>>> greeting = tk.Label(text="Hello, Tkinter")
97 isn’t used often. In addition to this, it has two main drawbacks:

  1. Layout can be difficult to manage with
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    97. This is especially true if your application has lots of widgets.
  2. Layouts created with
    >>> greeting = tk.Label(text="Hello, Tkinter")
    
    97 aren’t responsive. They don’t change as the window is resized.

One of the main challenges of cross-platform GUI development is making layouts that look good no matter which platform they’re viewed on, and

>>> greeting = tk.Label(text="Hello, Tkinter")
97 is a poor choice for making responsive and cross-platform layouts.

That’s not to say you should never use

>>> greeting = tk.Label(text="Hello, Tkinter")
97! In some cases, it might be just what you need. For example, if you’re creating a GUI interface for a map, then
>>> greeting = tk.Label(text="Hello, Tkinter")
97 might be the perfect choice to ensure widgets are placed at the correct distance from each other on the map.

>>> import tkinter as tk
10 is usually a better choice than
>>> greeting = tk.Label(text="Hello, Tkinter")
97, but even
>>> import tkinter as tk
10 has some downsides. The placement of widgets depends on the order in which
>>> import tkinter as tk
10 is called, so it can be difficult to modify existing applications without fully understanding the code controlling the layout. The
>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager solves a lot of these issues, as you’ll see in the next section.

The >>> greeting = tk.Label(text="Hello, Tkinter") 98 Geometry Manager

The geometry manager you’ll likely use most often is

>>> greeting = tk.Label(text="Hello, Tkinter")
98, which provides all the power of
>>> import tkinter as tk
10 in a format that’s easier to understand and maintain.

>>> greeting = tk.Label(text="Hello, Tkinter")
98 works by splitting a window or
>>> import tkinter as tk
19 into rows and columns. You specify the location of a widget by calling
>>> greeting = tk.Label(text="Hello, Tkinter")
98 and passing the row and column indices to the
>>> greeting.pack()
94 and
>>> greeting.pack()
95 keyword arguments, respectively. Both row and column indices start at
>>> import tkinter as tk
52, so a row index of
>>> window = tk.Tk()
39 and a column index of
>>> window = tk.Tk()
57 tells
>>> greeting = tk.Label(text="Hello, Tkinter")
98 to place a widget in the third column of the second row.

The following script creates a 3 × 3 grid of frames with

>>> import tkinter as tk
05 widgets packed into them:

>>> window.mainloop()
6

Here’s what the resulting window looks like:

Cara menggunakan python gui builder

You’re using two geometry managers in this example. Each frame is attached to

>>> import tkinter as tk
03 with the
>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager:

>>> window.mainloop()
7

Each

>>> window.mainloop()
03 is attached to its master
>>> import tkinter as tk
19 with
>>> import tkinter as tk
10:

>>> window.mainloop()
8

The important thing to realize here is that even though

>>> greeting = tk.Label(text="Hello, Tkinter")
98 is called on each
>>> import tkinter as tk
19 object, the geometry manager applies to the
>>> import tkinter as tk
03 object. Similarly, the layout of each
>>> greeting = tk.Label(text="Hello, Tkinter")
56 is controlled with the
>>> import tkinter as tk
10 geometry manager.

The frames in the previous example are placed tightly next to one another. To add some space around each frame, you can set the padding of each cell in the grid. Padding is just some blank space that surrounds a widget and visually sets its content apart.

The two types of padding are external and internal padding. External padding adds some space around the outside of a grid cell. It’s controlled with two keyword arguments to

>>> greeting = tk.Label(text="Hello, Tkinter")
98:

  1. >>> window.mainloop()
    
    12 adds padding in the horizontal direction.
  2. >>> window.mainloop()
    
    13 adds padding in the vertical direction.

Both

>>> window.mainloop()
12 and
>>> window.mainloop()
13 are measured in pixels, not text units, so setting both of them to the same value will create the same amount of padding in both directions. Try to add some padding around the outside of the frames from the previous example:

>>> window.mainloop()
9

Here’s the resulting window:

Cara menggunakan python gui builder

>>> import tkinter as tk
10 also has
>>> window.mainloop()
12 and
>>> window.mainloop()
13 parameters. The following code is nearly identical to the previous code, except that you add five pixels of additional padding around each label in both the
>>> greeting.pack()
54 and
>>> greeting.pack()
55 directions:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
0

The extra padding around the

>>> import tkinter as tk
05 widgets gives each cell in the grid a little bit of breathing room between the
>>> import tkinter as tk
19 border and the text in the label:

Cara menggunakan python gui builder

That looks pretty nice! But if you try and expand the window in any direction, then you’ll notice that the layout isn’t very responsive:

Cara menggunakan python gui builder

The whole grid stays at the top-left corner as the window expands.

By using

>>> window.mainloop()
23 and
>>> window.mainloop()
24 on the
>>> import tkinter as tk
03 object, you can adjust how the rows and columns of the grid grow as the window is resized. Remember, the grid is attached to
>>> import tkinter as tk
03, even though you’re calling
>>> greeting = tk.Label(text="Hello, Tkinter")
98 on each
>>> import tkinter as tk
19 widget. Both
>>> window.mainloop()
23 and
>>> window.mainloop()
24 take three essential arguments:

  1. Index: The index of the grid column or row that you want to configure or a list of indices to configure multiple rows or columns at the same time
  2. Weight: A keyword argument called
    >>> window.mainloop()
    
    31 that determines how the column or row should respond to window resizing, relative to the other columns and rows
  3. Minimum Size: A keyword argument called
    >>> window.mainloop()
    
    32 that sets the minimum size of the row height or column width in pixels

>>> window.mainloop()
31 is set to
>>> import tkinter as tk
52 by default, which means that the column or row doesn’t expand as the window resizes. If every column or row is given a weight of
>>> window = tk.Tk()
39, then they all grow at the same rate. If one column has a weight of
>>> window = tk.Tk()
39 and another a weight of
>>> window = tk.Tk()
57, then the second column expands at twice the rate of the first. Adjust the previous script to better handle window resizing:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
1

>>> window.mainloop()
23 and
>>> window.mainloop()
24 are placed in the body of the outer
>>> greeting = tk.Label(text="Hello, Tkinter")
45 loop. You could explicitly configure each column and row outside of the
>>> greeting = tk.Label(text="Hello, Tkinter")
45 loop, but that would require writing an additional six lines of code.

On each iteration of the loop, the

>>> window.mainloop()
42-th column and row are configured to have a weight of
>>> window = tk.Tk()
39. This ensures that the row and column expand at the same rate whenever the window is resized. The
>>> window.mainloop()
32 argument is set to
>>> window.mainloop()
45 for each column and
>>> import tkinter as tk
67 for each row. This ensures that the
>>> import tkinter as tk
05 widget always displays its text without chopping off any characters, even if the window size is extremely small.

The result is a grid layout that expands and contracts smoothly as the window is resized:

Cara menggunakan python gui builder

Try it yourself to get a feel for how it works! Play around with the

>>> window.mainloop()
31 and
>>> window.mainloop()
32 parameters to see how they affect the grid.

By default, widgets are centered in their grid cells. For example, the following code creates two

>>> import tkinter as tk
05 widgets and places them in a grid with one column and two rows:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
2

Each grid cell is

>>> window.mainloop()
51 pixels wide and
>>> window = tk.Tk()
06 pixels tall. The labels are placed in the center of each cell, as you can see in the following figure:

Cara menggunakan python gui builder

You can change the location of each label inside of the grid cell using the

>>> window.mainloop()
53 parameter, which accepts a string containing one or more of the following letters:

  • >>> window.mainloop()
    
    54 or
    >>> window.mainloop()
    
    55 to align to the top-center part of the cell
  • >>> window.mainloop()
    
    56 or
    >>> window.mainloop()
    
    57 to align to the right-center side of the cell
  • >>> window.mainloop()
    
    58 or
    >>> window.mainloop()
    
    59 to align to the bottom-center part of the cell
  • >>> window.mainloop()
    
    60 or
    >>> window.mainloop()
    
    61 to align to the left-center side of the cell

The letters

>>> window.mainloop()
54,
>>> window.mainloop()
58,
>>> window.mainloop()
56, and
>>> window.mainloop()
60 come from the cardinal directions north, south, east, and west. Setting
>>> window.mainloop()
53 to
>>> window.mainloop()
54 on both labels in the previous code positions each label at the top-center of its grid cell:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
3

Here’s the output:

Cara menggunakan python gui builder

You can combine multiple letters in a single string to position each label in the corner of its grid cell:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
4

In this example, the

>>> window.mainloop()
53 parameter of
>>> greeting.pack()
72 is set to
>>> window.mainloop()
70, which places the label at the top-right corner of its grid cell.
>>> greeting.pack()
75 is positioned in the bottom-left corner by passing
>>> window.mainloop()
72 to
>>> window.mainloop()
53. Here’s what that looks like in the window:

Cara menggunakan python gui builder

When a widget is positioned with

>>> window.mainloop()
53, the size of the widget itself is just big enough to contain any text and other contents inside of it. It won’t fill the entire grid cell. In order to fill the grid, you can specify
>>> window.mainloop()
75 to force the widget to fill the cell in the vertical direction, or
>>> window.mainloop()
76 to fill the cell in the horizontal direction. To fill the entire cell, set
>>> window.mainloop()
53 to
>>> window.mainloop()
78. The following example illustrates each of these options:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
5

Here’s what the output looks like:

Cara menggunakan python gui builder

What the above example illustrates is that the

>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager’s
>>> window.mainloop()
53 parameter can be used to achieve the same effects as the
>>> import tkinter as tk
10 geometry manager’s
>>> greeting.pack()
21 parameter. The correspondence between the
>>> window.mainloop()
53 and
>>> greeting.pack()
21 parameters is summarized in the following table:

>>> greeting = tk.Label(text="Hello, Tkinter")
98
>>> import tkinter as tk
10
>>> window.mainloop()
87
>>> greeting.pack()
43
>>> window.mainloop()
89
>>> greeting.pack()
42
>>> window.mainloop()
91
>>> window.mainloop()
92

>>> greeting = tk.Label(text="Hello, Tkinter")
98 is a powerful geometry manager. It’s often easier to understand than
>>> import tkinter as tk
10 and is much more flexible than
>>> greeting = tk.Label(text="Hello, Tkinter")
97. When you’re creating new Tkinter applications, you should consider using
>>> greeting = tk.Label(text="Hello, Tkinter")
98 as your primary geometry manager.

Note:

>>> greeting = tk.Label(text="Hello, Tkinter")
98 offers much more flexibility than you’ve seen here. For example, you can configure cells to span multiple rows and columns. For more information, check out the Grid Geometry Manager section of the TkDocs tutorial.

Now that you’ve got the fundamentals of geometry managers down for the Python GUI framework Tkinter, the next step is to assign actions to buttons to bring your applications to life.

Remove ads

Check Your Understanding

Expand the code block below for an exercise to check your understanding:

Exercise: Create an address entry formShow/Hide

Below is an image of an address entry form made with Tkinter:

Cara menggunakan python gui builder

Write a complete script that re-creates the window. You may use any geometry manager you like.

You can expand the code block below to see a solution:

Solution: Create an address entry formShow/Hide

There are many different ways to solve this exercise. If your solution generates a window identical to the one in the exercise statement, then congratulations! You’ve successfully solved the exercise! Below, you can look at two solutions that use the

>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager.

One solution creates a

>>> import tkinter as tk
05 and
>>> import tkinter as tk
17 widget with the desired settings for each field:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
6

There’s nothing wrong with this solution. It’s a bit long, but everything is very explicit. If you want to change something, then it’s clear to see exactly where to do so.

That said, the solution can be considerably shortened by recognizing that each

>>> import tkinter as tk
17 has the same width, and that all you need for each
>>> import tkinter as tk
05 is the text:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
7

In this solution, a is used to store the strings for each label in the form. They’re stored in the order that each form field should appear. Then,

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
03 gets both the index and string from each value in the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
04 list.

When you’re ready, you can move on to the next section.

Making Your Applications Interactive

By now, you have a pretty good idea of how to create a window with Tkinter, add some widgets, and control the application layout. That’s great, but applications shouldn’t just look good—they actually need to do something! In this section, you’ll learn how to bring your applications to life by performing actions whenever certain events occur.

Using Events and Event Handlers

When you create a Tkinter application, you must call

>>> import tkinter as tk
11 to start the event loop. During the event loop, your application checks if an event has occurred. If so, then it’ll execute some code in response.

The event loop is provided for you with Tkinter, so you don’t have to write any code that checks for events yourself. However, you do have to write the code that will be executed in response to an event. In Tkinter, you write functions called event handlers for the events that you use in your application.

Note: An event is any action that occurs during the event loop that might trigger some behavior in the application, such as when a key or mouse button is pressed.

When an event occurs, an event object is emitted, which means that an instance of a class representing the event is created. You don’t need to worry about instantiating these classes yourself. Tkinter will create instances of event classes for you automatically.

You’ll write your own event loop in order to better understand how Tkinter’s event loop works. That way, you can see how Tkinter’s event loop fits into your application, and which parts you need to write yourself.

Assume there’s a list called

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
06 that contains event objects. A new event object is automatically appended to
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
06 every time an event occurs in your program. You don’t need to implement this updating mechanism. It just automatically happens for you in this conceptual example. Using an infinite loop, you can continually check if there are any event objects in
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
06:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
8

Right now, the event loop that you’ve created doesn’t do anything with

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
09. Let’s change that. Suppose your application needs to respond to keypresses. You need to check that
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
09 was generated by a user pressing a key on their keyboard, and if so, pass
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
09 to an event handler function for keypresses.

Assume that

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
09 has a
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
13 attribute set to the string
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
14 if the event is a keypress event object, and a
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
15 attribute containing the character of the key that was pressed. Create a new
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
16 function and update your event loop code:

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
9

When you call

>>> import tkinter as tk
11, something like the above loop is run for you. This method takes care of two parts of the loop for you:

  1. It maintains a list of events that have occurred.
  2. It runs an event handler any time a new event is added to that list.

Update your event loop to use

>>> import tkinter as tk
11 instead of your own event loop:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
0

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
19 takes care of a lot for you, but there’s something missing from the above code. How does Tkinter know when to use
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
16? Tkinter widgets have a method called
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21 for just this purpose.

Remove ads

Using import tkinter as tk window = tk.Tk() label = tk.Label(text="Python rocks!") label.pack() window.mainloop() 21

To call an event handler whenever an event occurs on a widget, use

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21. The event handler is said to be bound to the event because it’s called every time the event occurs. You’ll continue with the keypress example from the previous section and use
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21 to bind
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
16 to the keypress event:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
1

Here, the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
16 event handler is bound to a
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
27 event using
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
28. Whenever a key is pressed while the application is running, your program will print the character of the key pressed.

Note: The output of the above program is not printed in the Tkinter application window. It’s printed to the .

If you run the program in IDLE, then you’ll see the output in the interactive window. If you run the program from a terminal, then you should see the output in your terminal.

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21 always takes at least two arguments:

  1. An event that’s represented by a string of the form
    import tkinter as tk
    
    window = tk.Tk()
    label = tk.Label(text="Python rocks!")
    label.pack()
    
    window.mainloop()
    
    30, where
    import tkinter as tk
    
    window = tk.Tk()
    label = tk.Label(text="Python rocks!")
    label.pack()
    
    window.mainloop()
    
    31 can be any of Tkinter’s events
  2. An event handler that’s the name of the function to be called whenever the event occurs

The event handler is bound to the widget on which

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21 is called. When the event handler is called, the event object is passed to the event handler function.

In the example above, the event handler is bound to the window itself, but you can bind an event handler to any widget in your application. For example, you can bind an event handler to a

>>> import tkinter as tk
16 widget that will perform some action whenever the button is pressed:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
2

In this example, the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
34 event on the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
35 widget is bound to the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
36 event handler. The
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
34 event occurs whenever the left mouse button is pressed while the mouse is over the widget. There are other events for mouse button clicks, including
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
38 for the middle mouse button and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
39 for the right mouse button.

Note: For a list of commonly used events, see the Event types section of the Tkinter 8.5 reference.

You can bind any event handler to any kind of widget with

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
21, but there’s a more straightforward way to bind event handlers to button clicks using the
>>> import tkinter as tk
16 widget’s
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 attribute.

Using import tkinter as tk window = tk.Tk() label = tk.Label(text="Python rocks!") label.pack() window.mainloop() 42

Every

>>> import tkinter as tk
16 widget has a
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 attribute that you can assign to a function. Whenever the button is pressed, the function is executed.

Take a look at an example. First, you’ll create a window with a

>>> import tkinter as tk
05 widget that holds a numeric value. You’ll put buttons on the left and right side of the label. The left button will be used to decrease the value in the
>>> import tkinter as tk
05, and the right one will increase the value. Here’s the code for the window:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
3

The window looks like this:

Cara menggunakan python gui builder

With the app layout defined, you can bring it to life by giving the buttons some commands. Start with the left button. When this button is pressed, it should decrease the value in the label by one. In order to do this, you first need to get answers to two questions:

  1. How do you get the text in
    >>> import tkinter as tk
    
    05?
  2. How do you update the text in
    >>> import tkinter as tk
    
    05?

>>> import tkinter as tk
05 widgets don’t have
>>> import tkinter as tk
70 like
>>> import tkinter as tk
17 and
>>> import tkinter as tk
18 widgets do. However, you can retrieve the text from the label by accessing the
>>> import tkinter as tk
34 attribute with a dictionary-style subscript notation:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
4

Now that you know how to get and set a label’s text, write an

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
55 function that increases the value in
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
56 by one:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
5

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
55 gets the text from
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
56 and converts it to an integer with
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
59. Then, it increases this value by one and sets the label’s
>>> import tkinter as tk
34 attribute to this new value.

You’ll also need

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
61 to decrease the value in
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
62 by one:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
6

Put

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
55 and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
61 in your code just after the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
65 statement.

To connect the buttons to the functions, assign the function to the button’s

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 attribute. You can do this when you instantiate the buttons. For example, update the two lines that instantiate the buttons to the following:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
7

That’s all you need to do to bind the buttons to

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
55 and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
61 and make the program functional. Try saving your changes and running the application! Click the buttons to increase and decrease the value in the center of the window:

Cara menggunakan python gui builder

Here’s the full application code for your reference:

Counter Application Full Source CodeShow/Hide

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
8

This app isn’t particularly useful, but the skills you learned here apply to every app you’ll make:

  • Use widgets to create the components of the user interface.
  • Use geometry managers to control the layout of the application.
  • Write event handlers that interact with various components to capture and transform user input.

In the next two sections, you’ll build more useful apps. First, you’ll build a temperature converter that converts a temperature value from Fahrenheit to Celsius. After that, you’ll build a text editor that can open, edit, and save text files!

Check Your Understanding

Expand the code block below for an exercise to check your understanding:

Exercise: Simulate rolling a six-sided dieShow/Hide

Write a program that simulates rolling a six-sided die. There should be one button with the text

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
69. When the user clicks the button, a random integer from
>>> window = tk.Tk()
39 to
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
71 should be displayed.

Hint: You can generate a random number using

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
72 in the module. If you’re not familiar with the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
73 module, then check out Generating Random Data in Python (Guide) for more information.

The application window should look something like this:

Cara menggunakan python gui builder

Try this exercise now.

You can expand the code block below to see a solution:

Solution: Simulate rolling a six-sided dieShow/Hide

Here’s one possible solution:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
9

Keep in mind that your code may look different.

When you’re ready, you can move on to the next section.

Building a Temperature Converter (Example App)

In this section, you’ll build a temperature converter application that allows the user to input temperature in degrees Fahrenheit and push a button to convert that temperature to degrees Celsius. You’ll walk through the code step by step. You can also find the full source code at the end of this section for your reference.

Note: To get the most out of this section, follow along in a Python shell.

Before you start coding, you’ll first design the app. You need three elements:

  1. >>> import tkinter as tk
    
    17: A widget called
    import tkinter as tk
    
    window = tk.Tk()
    label = tk.Label(text="Python rocks!")
    label.pack()
    
    window.mainloop()
    
    76 for entering the Fahrenheit value
  2. >>> import tkinter as tk
    
    05: A widget called
    import tkinter as tk
    
    window = tk.Tk()
    label = tk.Label(text="Python rocks!")
    label.pack()
    
    window.mainloop()
    
    78 to display the Celsius result
  3. >>> import tkinter as tk
    
    16: A widget called
    import tkinter as tk
    
    window = tk.Tk()
    label = tk.Label(text="Python rocks!")
    label.pack()
    
    window.mainloop()
    
    80 that reads the value from the
    >>> import tkinter as tk
    
    17 widget, converts it from Fahrenheit to Celsius, and sets the text of the
    >>> import tkinter as tk
    
    05 widget to the result when clicked

You can arrange these in a grid with a single row and one column for each widget. That gets you a minimally working application, but it isn’t very user-friendly. Everything needs to have labels.

You’ll put a label directly to the right of the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 widget containing the Fahrenheit symbol (℉) so that the user knows that the value
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 should be in degrees Fahrenheit. To do this, set the label text to
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
85, which uses Python’s named Unicode character support to display the Fahrenheit symbol.

You can give

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
80 a little flair by setting its text to the value
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
87, which displays a black arrow pointing to the right. You’ll also make sure that
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
78 always has the Celsius symbol (℃) following the label text
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
89 to indicate that the result is in degrees Celsius. Here’s what the final window will look like:

Cara menggunakan python gui builder

Now that you know what widgets you need and what the window is going to look like, you can start coding it up! First, import

>>> import tkinter as tk
20 and create a new window:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
0

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
91 sets the title of an existing window, while
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
92 with both arguments set to
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
93 makes the window have a fixed size. When you finally run this application, the window will have the text Temperature Converter in its title bar. Next, create the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 widget with a label called
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 and assign both to a
>>> import tkinter as tk
19 widget called
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
97:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
1

The user will enter the Fahrenheit value in

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76, and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 is used to label
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 with the Fahrenheit symbol. The
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
97 container groups
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 together.

You want

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 to be placed directly to the right of
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76. You can lay them out in
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
97 using the
>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager with one row and two columns:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
2

You’ve set the

>>> window.mainloop()
53 parameter to
>>> window.mainloop()
56 for
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 so that it always sticks to the rightmost edge of its grid cell. You also set
>>> window.mainloop()
53 to
>>> window.mainloop()
60 for
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 to keep it stuck to the leftmost edge of its grid cell. This ensures that
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
95 is always located immediately to the right of
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76.

Now, make the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
80 and the
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
78 for converting the temperature entered into
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76 and displaying the results:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
3

Like

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
97, both
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
80 and
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
78 are assigned to
>>> import tkinter as tk
03. Together, these three widgets make up the three cells in the main application grid. Use
>>> greeting = tk.Label(text="Hello, Tkinter")
98 to go ahead and lay them out now:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
4

Finally, run the application:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
5

That looks great! But the button doesn’t do anything just yet. At the top of your script file, just below the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
65 line, add a function called
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
25:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
6

This function reads the value from

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
76, converts it from Fahrenheit to Celsius, and then displays the result in
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
78.

Now go down to the line where you define

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
80 and set its
import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 parameter to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
30:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
7

That’s it! You’ve created a fully functional temperature converter app in just twenty-six lines of code! Pretty cool, right?

You can expand the code block below to see the full script:

Temperature Converter Full Source CodeShow/Hide

Here’s the full script for your reference:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
8

It’s time to kick things up a notch! Read on to learn how to build a text editor.

Building a Text Editor (Example App)

In this section, you’ll build a text editor application that can create, open, edit, and save text files. There are three essential elements in the application:

  1. A
    >>> import tkinter as tk
    
    16 widget called
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    32 for opening a file for editing
  2. A
    >>> import tkinter as tk
    
    16 widget called
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    34 for saving a file
  3. A
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    35 widget called
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    36 for creating and editing the text file

The three widgets will be arranged so that the two buttons are on the left-hand side of the window, and the text box is on the right-hand side. The whole window should have a minimum height of 800 pixels, and

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 should have a minimum width of 800 pixels. The whole layout should be responsive so that if the window is resized, then
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 is resized as well. The width of the frame holding the buttons should not change, however.

Here’s a sketch of how the window will look:

Cara menggunakan python gui builder

You can achieve the desired layout using the

>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager. The layout contains a single row and two columns:

  1. A narrow column on the left for the buttons
  2. A wider column on the right for the text box

To set the minimum sizes for the window and

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36, you can set the
>>> window.mainloop()
32 parameters of the window methods
>>> window.mainloop()
24 and
>>> window.mainloop()
23 to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
44. To handle resizing, you can set the
>>> window.mainloop()
31 parameters of these methods to
>>> window = tk.Tk()
39.

In order to get both buttons into the same column, you’ll need to create a

>>> import tkinter as tk
19 widget called
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48. According to the sketch, the two buttons should be stacked vertically inside of this frame, with
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 on top. You can do that with either the
>>> greeting = tk.Label(text="Hello, Tkinter")
98 or
>>> import tkinter as tk
10 geometry manager. For now, you’ll stick with
>>> greeting = tk.Label(text="Hello, Tkinter")
98 since it’s a little easier to work with.

Now that you have a plan, you can start coding the application. The first step is to create all of the widgets you need:

>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
9

Here’s a breakdown of this code:

  • Line 1 imports
    >>> import tkinter as tk
    
    20.
  • Lines 3 and 4 create a new window with the title
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    54.
  • Lines 6 and 7 set the row and column configurations.
  • Lines 9 to 12 create the four widgets you’ll need for the text box, the frame, and the open and save buttons.

Take a look at line 6 more closely. The

>>> window.mainloop()
32 parameter of
>>> window.mainloop()
24 is set to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
44, and
>>> window.mainloop()
31 is set to
>>> window = tk.Tk()
39:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
0

The first argument is

>>> import tkinter as tk
52, which sets the height of the first row to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
44 pixels and makes sure that the height of the row grows proportionally to the height of the window. There’s only one row in the application layout, so these settings apply to the entire window.

Let’s also take a closer look at line 7. Here, you use

>>> window.mainloop()
23 to set the
>>> import tkinter as tk
49 and
>>> window.mainloop()
31 attributes of the column with index
>>> window = tk.Tk()
39 to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
44 and
>>> window = tk.Tk()
39, respectively:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
1

Remember, row and column indices are zero-based, so these settings apply only to the second column. By configuring just the second column, the text box will expand and contract naturally when the window is resized, while the column containing the buttons will remain at a fixed width.

Now you can work on the application layout. First, assign the two buttons to the

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 frame using the
>>> greeting = tk.Label(text="Hello, Tkinter")
98 geometry manager:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
2

These two lines of code create a grid with two rows and one column in the

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 frame since both
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 and
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 have their
>>> greeting = tk.Label(text="Hello, Tkinter")
04 attribute set to
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48.
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 is put in the first row and
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 in the second row so that
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 appears above
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 in the layout, just you planned in your sketch.

Both

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 and
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 have their
>>> window.mainloop()
53 attributes set to
>>> window.mainloop()
76, which forces the buttons to expand horizontally in both directions and fill the entire frame. This ensures that both buttons are the same size.

You place five pixels of padding around each button by setting the

>>> window.mainloop()
12 and
>>> window.mainloop()
13 parameters to
>>> import tkinter as tk
61. Only
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 has vertical padding. Since it’s on top, the vertical padding offsets the button down from the top of the window a bit and makes sure that there’s a small gap between it and
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34.

Now that

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 is laid out and ready to go, you can set up the grid layout for the rest of the window:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
3

These two lines of code create a grid with one row and two columns for

>>> import tkinter as tk
03. You place
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 in the first column and
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 in the second column so that
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 appears to the left of
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 in the window layout.

The

>>> window.mainloop()
53 parameter for
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
48 is set to
>>> window.mainloop()
75, which forces the whole frame to expand vertically and fill the entire height of its column.
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 fills its entire grid cell because you set its
>>> window.mainloop()
53 parameter to
>>> window.mainloop()
78, which forces it to expand in every direction.

Now that the application layout is complete, add

>>> import tkinter as tk
11 to the bottom of the program and save and run the file:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
4

The following window is displayed:

Cara menggunakan python gui builder

That looks great! But it doesn’t do anything just yet, so you need to start writing the commands for the buttons.

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 needs to show a file open dialog and allow the user to select a file. It then needs to open that file and set the text of
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 to the contents of the file. Here’s an
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
03 function that does just this:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
5

Here’s a breakdown of this function:

  • Lines 5 to 7 use the
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    04 dialog from the
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    05 module to display a file open dialog and store the selected file path to
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    06.
  • Lines 8 and 9 check to see if the user closes the dialog box or clicks the Cancel button. If so, then
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    06 will be
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    08, and the function will
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    09 without executing any of the code to read the file and set the text of
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    36.
  • Line 10 clears the current contents of
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    36 using
    >>> import tkinter as tk
    
    71.
  • Lines 11 and 12 open the selected file and
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    13 its contents before storing the
    >>> import tkinter as tk
    
    34 as a string.
  • Line 13 assigns the string
    >>> import tkinter as tk
    
    34 to
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    36 using
    >>> import tkinter as tk
    
    72.
  • Line 14 sets the title of the window so that it contains the path of the open file.

Now you can update the program so that

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 calls
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
03 whenever it’s clicked. There are a few things that you need to do to update the program. First, import
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
04 from
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
05 by adding the following import to the top of your program:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
6

Next, set the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 attribute of
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
23 to
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
24:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
7

Save the file and run it to check that everything is working. Then try opening a text file!

With

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
32 working, it’s time to work on the function for
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34. This needs to open a save file dialog box so that the user can choose where they would like to save the file. You’ll use the
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
27 dialog in the
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
05 module for this. This function also needs to extract the text currently in
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
36 and write this to a file at the selected location. Here’s a function that does just this:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
8

Here’s how this code works:

  • Lines 19 to 22 use the
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    27 dialog box to get the desired save location from the user. The selected file path is stored in the
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    06 variable.
  • Lines 23 and 24 check to see if the user closes the dialog box or clicks the Cancel button. If so, then
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    06 will be
    >>> tk.Label()
    <tkinter.Label object .!label>
    
    >>> ttk.Label()
    <tkinter.ttk.Label object .!label2>
    
    08, and the function will return without executing any of the code to save the text to a file.
  • Line 25 creates a new file at the selected file path.
  • Line 26 extracts the text from
    >>> import tkinter as tk
    >>> import tkinter.ttk as ttk
    
    36 with
    >>> import tkinter as tk
    
    70 method and assigns it to the variable
    >>> import tkinter as tk
    
    34.
  • Line 27 writes
    >>> import tkinter as tk
    
    34 to the output file.
  • Line 28 updates the title of the window so that the new file path is displayed in the window title.

Now you can update the program so that

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 calls
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
39 when it’s clicked. Again, there are a few things you need to do in order to update the program. First, import
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
27 from
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
05 by updating the import at the top of your script, like so:

>>> from tkinter import *
>>> from tkinter.ttk import *

>>> Label()
<tkinter.ttk.Label object .!label>

>>> Text()
<tkinter.Text object .!text>
9

Finally, set the

import tkinter as tk

window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()

window.mainloop()
42 attribute of
>>> import tkinter as tk
>>> import tkinter.ttk as ttk
34 to
>>> tk.Label()
<tkinter.Label object .!label>

>>> ttk.Label()
<tkinter.ttk.Label object .!label2>
44:

>>> import tkinter as tk
00

Save the file and run it. You’ve now got a minimal yet fully functional text editor!

You can expand the code block below to see the full script:

Text Editor Application Full Source CodeShow/Hide

Here’s the full script for your reference:

>>> import tkinter as tk
01

You’ve now built two GUI applications in Python and applied many of the skills that you’ve learned throughout this tutorial. That’s no small achievement, so take some time to feel good about what you’ve done. You’re now ready to tackle some applications on your own!

Conclusion

In this tutorial, you learned how to get started with Python GUI programming. Tkinter is a compelling choice for a Python GUI framework because it’s built into the Python standard library, and it’s relatively painless to make applications with this framework.

Throughout this tutorial, you’ve learned several important Tkinter concepts:

  • How to work with widgets
  • How to control your application layout with geometry managers
  • How to make your applications interactive
  • How to use five basic Tkinter widgets:
    >>> import tkinter as tk
    
    05,
    >>> import tkinter as tk
    
    16,
    >>> import tkinter as tk
    
    17,
    >>> import tkinter as tk
    
    18, and
    >>> import tkinter as tk
    
    19

Now that you’ve mastered the foundations of Python GUI programming with Tkinter, the next step is to build some of your own applications. What will you create? Share your fun projects down in the comments below!

Additional Resources

In this tutorial, you touched on just the foundations of creating Python GUI applications with Tkinter. There are a number of additional topics that aren’t covered here. In this section, you’ll find some of the best resources available to help you continue on your journey.

Tkinter References

Here are some official resources to check out:

  • The official Python Tkinter reference documentation covers Python’s Tkinter module in moderate depth. It’s written for more advanced Python developers and isn’t the best resource for beginners.
  • Tkinter 8.5 reference: a GUI for Python is an extensive reference covering the majority of the Tkinter module. It’s exhaustive, but it’s written in the reference style without commentary or examples.
  • The Tk Commands reference is the definitive guide to commands in the Tk library. It’s written for the Tcl language, but it answers a lot of questions about why things work the way they do in Tkinter.

Additional Widgets

In this tutorial, you learned about the

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17,
>>> import tkinter as tk
18, and
>>> import tkinter as tk
19 widgets. There are several other widgets in Tkinter, all of which are essential for building real-world applications. Here are some resources to continue learning about widgets:

  • The TkDocs Tkinter Tutorial is a fairly comprehensive guide for Tk, the underlying code library used by Tkinter. Examples are presented in Python, Ruby, Perl, and Tcl. You can find several examples of widgets beyond those covered here in two sections:
    • Basic Widgets covers the same widgets as this tutorial, plus a few more.
    • More Widgets covers several additional widgets.
  • The official Python docs cover additional widgets:
    • ttk themed widgets covers the Tk themed widget set.
    • Scrolled Text Widget details a
      >>> import tkinter as tk
      
      18 widget combined with a vertical scroll bar.

Application Distribution

Once you’ve created an application with Tkinter, you probably want to distribute it to your colleagues and friends. Here are some tutorials to get you going with that process:

  • Using PyInstaller to Easily Distribute Python Applications
  • 4 Attempts at Packaging Python as an Executable
  • Building Standalone Python Applications with PyOxidizer

Other GUI Frameworks

Tkinter isn’t your only choice for a Python GUI framework. If Tkinter doesn’t meet the needs of your project, then here are some other frameworks to consider:

  • How to Build a Python GUI Application With wxPython
  • Python and PyQt: Building a GUI Desktop Calculator
  • Building a Mobile Application With the Kivy Python Framework
  • PySimpleGUI: The Simple Way to Create a GUI With Python

Take the Quiz: Test your knowledge with our interactive “Python GUI Programming With Tkinter” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Mark as Completed

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Cara menggunakan python gui builder

Send Me Python Tricks »

About David Amos

Cara menggunakan python gui builder
Cara menggunakan python gui builder

David is a writer, programmer, and mathematician passionate about exploring mathematics through code.

» More about David


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Cara menggunakan python gui builder

Aldren

Cara menggunakan python gui builder

Bartosz

Cara menggunakan python gui builder

Geir Arne

Cara menggunakan python gui builder

Jaya

Cara menggunakan python gui builder

Joanna

Cara menggunakan python gui builder

Kate

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

Apa itu Python GUI?

Tkinter adalah graphic user interface (GUI) standar python digunakan untuk membuat tampilan aplikasi dengan komponen-komponen yang ada di modul tkinter seperti Button, Textbox, Label, Frame, Window yang mana sangat mendukung dalam penciptaan aplikasi GUI .

Apakah Python bisa membuat aplikasi desktop?

Python adalah bahasa pemrograman multi-platform yang bersifat free dan open-source, dan dapat digunakan untuk mengembangkan aplikasi aplikasi desktop maupun web. Python memiliki pustaka standar (Python Standard Library) yang sangat lengkap sehingga dapat memenuhi berbagai macam permasalahan-permasalahan riil di dalam ...

Aplikasi apa yang bisa dibangun dengan Python?

Sebagai bahasa pemrograman favorit, Python memiliki banyak kegunaan. Bahasa pemrograman ini digunakan untuk berbagai kepentingan, mulai dari pembuatan game offline dan online, maintenance website, machine learning, sampai aplikasi media sosial.

Apakah bisa membuat aplikasi Android dengan Python?

Anda beruntung, bahasa Python telah bisa digunakan untuk membuat aplikasi Android, iOS atau framework lainnya. Paket yang digunakan adalah Kivy.