Build A Tkinter App (Part 2 / Widgets-1) - Python
Published: 2022-03-10 19:31:12

Hey there ! Here is the second of [this course](https://dev.to/corentinzrt/build-a-tkinter-app-part-1-python-8e3) on [Tkinter](https://docs.python.org/fr/3/library/tkinter.html). Today we are going to learn the different widgets that the module offers to us and how to display them on the screen.

---

# <u>The syntax :</u>
So first, every widgets are made with the same syntax. You have to tell wich one you use by its name and give it few arguments. The main one is the app where your widget will be display. Example :

```
_This is an example of a widget in tkinter :_
Widget(app)
```

There are many different arguments for all widgets. Today we are going to learn 5 of these widgets :
   - Label
   - Button
   - Input
   - Text
   - Canvas

---

# <u>How to display a Widget ?</u>
To display a widget on the screen we can use two different ways.
We can use the `pack` command :

```python
my_widget = Widget(myApp)
my_widget.pack() # Display on screen
```

Or we can use the `grid` method :

```python
my_widget = Widget(myApp)
my_widget.grid() # Display on screen
```

The difference between these two methods is that the `grid` acts like a grid WOW. You have to put a certain column and row like that :

```python
my_widget = Widget(myApp)
my_widget.grid(column=0, row=0) # Display on screen at first column cell and first row cell
```

The pack method, put the widget in the center of the screen and below the others.
Here are just a little of what these methods can do. So if you want more informations, go here : http://tkinter.fdex.eu/doc/gp.html

Now you know how to display something on the screen !

# - <u>Label :</u>

The Label is just a widget that display a text. And to create one you have just to paste this code in your script : 
_PS : during the whole course, i'll use the script we used before..._

```python
from tkinter import *

app = Tk()
app.title("Our Application")
app.geometry("600x400")

# The Label Widget :
label = Label(app, text="My awesome Label !")
label.pack() # We display it

app.mainloop()
```

Yes, it's not hard as you see... Surely there are many arguments that a Label can take. We have :
   - bg : for the background ('name of the color')
   - fg : the color of the text
   - font : A tuple with the font and the size. Ex : ('Arial', 25)

```python
from tkinter import *

app = Tk()
app.title("Our Application")
app.geometry("600x400")

# The Label Widget :
label = Label(app, text="My awesome Label !", fg='red', font=('Arial', 25))
label.pack() # We display it

app.mainloop()
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh0e2xe7r40ygn5dhmut.png) 

# - <u>Button :</u>

Now you have understand the logic of the widgets, you could know how to create a button right ? 
You just have to paste that under the label :

```python
btn = Button(app, text="My Button !", fg='blue', font=('Arial', 20))
btn.pack() # We display it
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0gaz7j1296lcugrlv4a.png) 

As you see, the arguments seem to be the same. But there is one of these that will be interesting : the `command`. But yeah, it's not for now because it enters in input section. So you'll see it in two posts because this one is already long. So I split it in two parts.

In the next one, we'll see the others widgets. Don't worry !

So it's time to say goobye ! 👋👨‍💻