TKinter Tutorial – Build GUI in Python With TKinter

In this Python TKinter Tutorial we are going to learn about Build GUI in Python With TKinter, we will learn that how you can create your first Python GUI window in TKinter, and also how you can work with different widgets in Python TKinter.

 

 

 

What is TKinter ?

Python is very powerful programming language. It ships with the built-in tkinter module. In only a few lines of code (four, to be precise) we can build our first Python GUI. the IDE that we are using is Pycharm Community Edition IDE. OK we have said that TKinter is a built in library in Python it means that when you have installed Python, TKinter will be installed by default so now we are going to create our first window in TKinter.

 

 

 

 

How to Create Window in TKinter

This is the code for creating window in python tkinter.

 

 

First you need to create the instance of TKinter.

 

 

If you want to add title for the window, you can use this code.

 

 

Also you can give color for your window.

 

 

This is used for adding icon to the window.

 

 

You can use this code for preventing window resize.

 

 

 

Run the code and this is the result.

TKinter Tutorial - Build GUI in Python With TKinter
TKinter Tutorial – Build GUI in Python With TKinter

 

 

 

Create TKinter Window Using OOP

OK now we want to create our window using Python Object Oriented Programming, now this is the complete code.

 

In the above code we have created main class for our Window, this class extends from tkinter.Tk.

__init__” is a reserved method in python classes. It is called as a constructor in object oriented programming. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.

The super() function is used to give access to methods and properties of a parent class. And the super() function makes class inheritance more manageable.

The word ‘self’ is used to represent the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python.

 

 

Run the complete code and this is the result.

TKinter Tutorial - Build GUI in Python With TKinter
TKinter Tutorial – Build GUI in Python With TKinter

 

 

 

How to Create Label in TKinter

In this section we want to learn creating of label in python tkinter, so using label you can create and add text in your tkinter window. now this is the complete code for creating of  label in python.

In this code we have Object Oriented Programming in Python, first we have created a class, after that our class extends from TK.

 

 

For creating of the label you can use Label class, and you can pass the text for the label, foreground and also background of the label.

 

 

You can se the font in here. 

 

 

Also you can use layout management in tkinter, there are different layouts in tkinter, we will talk about this in the later part, in here we use grid layout, and we want to add our label in the grid layout, you need to specify the row and column in the grid.

 

 

At the end create the object of your Root class and start your main loop.

 

 

 

Run the code and this is the result.

TKinter Tutorial - Build GUI in Python With TKinter
TKinter Tutorial – Build GUI in Python With TKinter

 

 

 

How to Create Button in TKinter 

OK now let’s create button in tkinter, also we will learn how you can interact with buttons in tkinter and how you can add commands for the buttons. this is the code for creating button in tkinter.

 

 

 

Using this code we have created a label, also we have added the button in the grid layout.

 

 

In here we have created our button, you need to give text for the button and you can see that we have given command for the button and this command is connected with click_me() method.

 

 

This is the method that we have already connected in the command section, basically in here when a user clicks the button iam going to change the text of the label with the button text. and for that you can use configure() method with the respected widgets.

 

 

 

Run the code and this is the result.

TKinter Tutoria Creating Button
TKinter Tutorial Creating Button

 

 

 

 

How to Create TextBox in Python TKinter

In this part we want to create TextBox in TKinter, this is the complete code for creating the TextBox.

 

 

You can use tkinter.Entry() for creating of the TextBox or EntryBox in tkinter, also you can give the width of the textbox.

 

 

This is used for focusing on the textbox.

 

 

You can add your textbox in the grid layout using this code.

 

 

 

In here we have created our button , we have added a command in the button, because we want to connect this command with the click_me() method, also you need to add your button in the grid layout, specifying the row and column number.

 

 

If you want to disable a button in tkinter than you can use this code.

 

 

And this is the method that we have already connected with the command button, basically in this method we are printing the textbox input in the label.

 

 

 

Run the code and this is the result.

TKinter Tutorial Creating TextBox
TKinter Tutorial Creating TextBox

 

 

 

 

How to Create SpinBox in TKinter

You can use SpinBox() class for creating of spinbox in tkinter, so now this is the complete code for this section.

 

 

 

In here we have created our SpinBox, and you can add the spinbox value, for example we have added from 0 up to 10, also we have added a command to the spinbox, we have connected that with spin_callback() method.

 

 

Also we are using ScrolledText, you need to add the width and height for the ScrolledText.

 

 

And this is our method for the spinbox, we have already connected this method with the command of spinbox. in this method first we are going to get the value from the spinbox and after that we insert the value in the ScrolledText.

 

 

 

Run the code and this is the result.

Tkinter Tutorial SpinBox
Tkinter Tutorial SpinBox

 

 

 

How to Create RadioButton in TKinter

Now we want to create RadioButton in TKinter, you can use RadiButton class, this is the complete code for tkinter radiobutton, basically in this code we want to change the window background based on user selection value from the radio items.

 

 

In here we want to get the value from tkinter radiobutton.

 

 

After that we are going to create our RadioButton, and we add the value for the radiobutton, also you can add the command for the radiobutton, and we have connected the command with rad_event() method. and at the end add the radiobutton in to grid layout.

 

 

 

And this is our method, we have connected this method with command of radiobutton, basically in this method we want to change our window color based on user selection. 

 

 

 

 

Run the code and this is the result.

Python TKinter - Creating RadioButton
Python TKinter – Creating RadioButton

 

 

 

How to Create MessageBox in TKinter

In this part we want to create MessageBox in TKinter, there are different types of messagebox that you can use in tkinter, so we have Information, Warning and Error MessageBox. this is the complete code for MessageBox.

 

 

This is the button, basically we want to create three buttons for three messagebox, also we have connected the command of button with the specific messagebox method.

 

 

And these are the three types of messagebox method that we have already connected with the command of buttons.

 

 

 

Run the code and this is the result.

TKinter Tutorial MessageBox
TKinter Tutorial MessageBox

 

 

 

How to Create MultiChoiceBox in TKinter

In this part we want to create MultiChoiceBox in Python TKinter, so this is the complete code.

 

 

 

This is the create_button() method, because i want when a user clicks the button iam going to show a MultiChoiceBox with different choices. you can see that we have connected this method with our choiceBox() method.

 

 

This is the process of MultiChoiceBox creation.

 

 

Run the complete code and this is the result.

Python TKinter MultiChoice Box
Python TKinter MultiChoice Box

 

 

 

How to Create ProgressBar in TKinter

In this part we want to learn creating or ProgressBar in TKinter, this is the complete code for creating of TKinter ProgressBar.

 

 

 

In here we have created two buttons, one for starting of the progressbar and the second for stopping the progressbar, we have connected buttons command with the specific methods of start_progress() and stop_progress(), these are the method for starting and stopping of the ProgressBar. also add these buttons in a grid layout.

 

 

 

This is our ProgressBar in TKinter, you can use ProgressBar from ttk, eventough we have the progressbar in tkinter but it is old style, you need to add the orientation for the progressbar also the lenght. and at the end add the ProgressBar in the GridLayout.

 

 

And these are the two method for starting and stopping of the Tkinter ProgressBar, we have already connected these methods with the specific button.

 

 

 

Run the complete code and this is the result.

Python TKinter - Build ProgressBar in TKinter
Python TKinter – Build ProgressBar in TKinter

 

 

 

How to Create LabelFrame in TKinter

In this part we want to learn creating LabelFrame in TKinter, so a labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts. this is the complete code for creating LabelFrame in Python GUI (TKinter).

 

 

 

Using this code we have created LabelFrame, also we have added the LabelFrame in the GridLayout.

 

 

As i have already said that LabelFrame acts as a container for widgets, so now we need to add some widgets in the LabelFrame, iam goint to add three labels.

 

 

 

Run the complete code and this is the result.

How to Create LabelFrame in TKinter
How to Create LabelFrame in TKinter

 

 

 

How to Create ListBox in TKinter 

In this part we want to create ListBox in TKinter, so a list box is a graphical control element that allows the user to select one or more items from a list contained within a static, multiple line text box. this is the complete code for tkinter listbox.

 

 

 

You can use ListBox class from tkinter for creating listbox, and after that you need to insert items to the listbox, make sure that you have specified the index of the items.

 

 

Also you need to pack the listbox, so pack is another kinds of layouts that you can use in tkinter.

 

 

 

Run the complete code and this is the result.

TKinter ListBox
TKinter ListBox

 

 

 

How to Create ColorChooser in TKinter

In this part we want to learn creating ColoChooser in Python TKinter, so using ColoChooser you can chose a color that you want. this is the complete code for TKinter ColorChooser.

 

 

 

This is our button and we have connected this button with the color_chooser() method.

 

 

And this is our method, basically it create a color dialog for us.

 

 

 

Run the code and this is the result.

TKinter Tutorial Color Chooser
TKinter Tutorial Color Chooser

 

 

 

How to Create Canvas in TKinter

In this part we want to create Canvas in TKinter, so  Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text,widgets or frames on a Canvas. we can create Different shapes like arc, polygon, image, line.​

 

 

You can use tkinter.Canvas() class for creating canvas in tkinter, you can give the color for the canvas, also the width and height for the canvas.

 

 

We are going to create an arc in the canvas.

 

 

 

Run the code and this is the result.

TKinter Canvas
TKinter Canvas

 

 

Leave a Comment