DEV Community

Corentin-zrt
Corentin-zrt

Posted on

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

Today, we continue our course about create a tkinter application with Python. We are going to learn these 3 widgets : Input; Text; Canvas.

So, if you don't have already see the others part of the course, let's watch this.


Input:

An input widget is a single line entry. We can create with like this :

input_widget = Input(app)
input_widget.pack()
Enter fullscreen mode Exit fullscreen mode

And like the Label and Button widgets, you can add the arguments about text : fg, bg, font, width, height. The width isn't in pixel in character.

Text:

Now, a Text widget is an input but more wider then the Input because you can, like a notepad, write on multiple lines.

text = Text(app)
text.pack()
Enter fullscreen mode Exit fullscreen mode

Same arguments for text, ect...

Canvas:

A Canvas is a surface where you can draw or place images.
For an image :

canv = Canvas(app, width=500, height=300, bg="white") # width, height in pixels
img = PhotoImage(file="image.png") # Here we load an image
canv.create_image(250, 150, image = img) # 250 -> position x, 150 -> position Y
canv.pack()
Enter fullscreen mode Exit fullscreen mode

You must know that the 0;0 in a canvas isn't at the middle of it but in the left top corner.

For a circle :

canv = Canvas(app, width=500, height=300, bg="white")
canv.create_circle(200, 100, 300, 200) # (x0, y0, x1, y1)
canv.pack()
Enter fullscreen mode Exit fullscreen mode

Image description

For more informations about figures that tkinter can support. Check this out : http://tkinter.fdex.eu/doc/caw.html


Now, you know the main widgets of Tkinter. In the next post, we'll see the bind method and the command argument in Button widget. And this course will be finish.
So, have a nice day. And see you soon. Bye ! 👋👨‍💻

Top comments (0)