DEV Community

Cover image for Build A Tkinter App (Part 2 / Widgets-1) - Python
Corentin-zrt
Corentin-zrt

Posted on

Build A Tkinter App (Part 2 / Widgets-1) - Python

Hey there ! Here is the second of this course on Tkinter. Today we are going to learn the different widgets that the module offers to us and how to display them on the screen.


The syntax :

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)
Enter fullscreen mode Exit fullscreen mode

There are many different arguments for all widgets. Today we are going to learn 5 of these widgets :

  • Label
  • Button
  • Input
  • Text
  • Canvas

How to display a Widget ?

To display a widget on the screen we can use two different ways.
We can use the pack command :

my_widget = Widget(myApp)
my_widget.pack() # Display on screen
Enter fullscreen mode Exit fullscreen mode

Or we can use the grid method :

my_widget = Widget(myApp)
my_widget.grid() # Display on screen
Enter fullscreen mode Exit fullscreen mode

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 :

my_widget = Widget(myApp)
my_widget.grid(column=0, row=0) # Display on screen at first column cell and first row cell
Enter fullscreen mode Exit fullscreen mode

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 !

- Label :

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...

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()
Enter fullscreen mode Exit fullscreen mode

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)
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()
Enter fullscreen mode Exit fullscreen mode

Image description

- Button :

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 :

btn = Button(app, text="My Button !", fg='blue', font=('Arial', 20))
btn.pack() # We display it
Enter fullscreen mode Exit fullscreen mode

Image description

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 ! 👋👨‍💻

Top comments (0)