Can I use Tkinter on my website

Python course

Tkinter

Hello Tkinter label


We start our Tkinter tutorial with the simplest Tk (Tkinter) widget, i.e. a label. A label is a Tkinter class that can be used to display text or an image. A label is a widget that the user can only look at, but no interaction is possible.

There is hardly a book, tutorial or introduction to a programming language that does not start with the "Hello World" example. We too start our introduction traditionally, but will change the output slightly.

The following Python script uses Tkinter to generate a window with the text "Hello Tkinter".
Attention: Under Python3 you have to write Tkinter lower case, so "from tkinter import *":

from Tkinter import * root = Tk () w = Label (root, text = "Hello Tkinter!") w.pack () root.mainloop ()

Call of the example program

If you have saved the script under the name hello.py, you can start it as follows: $ python hello.py If you start the command under Gnome and Linux, the window looks like this:



Windows has a different appearance:

Explanations

The Tkinter module contains the Tk toolkit and it must always be imported. Note that the module is capitalized in the Python 2.x versions while it is lowercase in Python 3.x. So you have to import it as "from tkinter import *".

The Tkinter module must always be imported if you want to work with Tk / Tkinter. In our example we import everything into our Tkinter namespace with the asterisk ("*"). This saves us typing. from Tkinter import * To initialize Tkinter we need to create a Tkinter root widget. This is done with the Tk () call. This widget provides the title bar and the decorations provided by the window manager used. The root widget must be created before any other widgets are used. There can only be one root widget in each application. root = Tk () The next line deals with the definition of the label widget. The first parameter of the Label method contains the parent widget, in our case "root". So our label widget is a child of the root widget. The string to be displayed is assigned to the keyword parameter "text": w = Label (root, text = "Hello Tkinter!") The pack method is necessary to embed the label in the existing widgets: w.pack () The label but is only displayed when we use the mainloop method on the root widget: root.mainloop () The window created by our script remains in the event loop until we close the window.

Images in labels

As we mentioned earlier, labels can contain both text and images. The following example includes two labels, one with text and the other with an image.

from Tkinter import * root = Tk () logo = PhotoImage (file = "../ images / python_logo_small.gif") w1 = Label (root, image = logo) .pack (side = "right") explanation = "" " At present, only GIF and PPM / PGM formats are supported, but an interface exists to allow additional image file formats to be added easily. "" "W2 = Label (root, justify = LEFT, padx = 10, text = explanation). pack (side = "left") root.mainloop ()

If you start this script it looks like this:



The "justify" parameter can be used to position the text left (LEFT) or right (RIGHT) or to center it using CENTER.

padx can be used to provide the label with an additional space (padding) in a horizontal direction to the left and right. The default value for padding is 1 pixel. pady behaves analogously, however, in the vertical direction. The previous example without justify (default is CENTER) and without padding looks like this:



Would you like to print the text over the picture? No problem! You then only use one label and set the image and text options at the same time. By default, however, the picture is taken and not the text. In order to also output the text, you have to set the compound option. If you set compund to CENTER, the text is output centered above the image: from Tkinter import * root = Tk () logo = PhotoImage (file = "../ images / python_logo_small.gif") explanation = "" "At present, only GIF and PPM / PGM formats are supported, but an interface exists to allow additional image file formats to be added easily. "" "w = Label (root, compound = CENTER, text = explanation, image = logo) .pack (side = "right") root.mainloop ()



We can also bring the image to the right and the text centered on the left on the left:

w = label (root, justify = LEFT, compound = LEFT, padx = 10, text = explanation, image = logo) .pack (side = "right")

If the compound option is set to LEFT, RIGHT, TOP or BOTTM, the image is placed on the left, right, above or below the text accordingly.

Colored labels in different fonts

With some widgets, such as the label, text or canvas widget, it is possible to specify certain fonts for display. You can set the "font" option accordingly. Fonts are some of several areas that are not platform independent.

The attribute fg can be used to output the text in a certain color. bg is the attribute that can be used to change the background color.

from Tkinter import * root = Tk () Label (root, text = "Red Text in Times Font", fg = "red", font = "Times"). pack () Label (root, text = "Green Text in Helvetica Font ", fg =" light green ", bg =" dark green ", font =" Helvetica 16 bold italic "). Pack () Label (root, text =" Blue Text in Verdana bold ", fg =" blue ", bg = "yellow", font = "Verdana 10 bold"). pack () root.mainloop () The result looks like this:



Dynamic content in a label widget

The following script shows an example in which a label is dynamically incremented by 1 until the stop button is pressed:

import Tkinter as tk counter = 0 def counter_label (label): def count (): global counter counter + = 1 label.config (text = str (counter)) label.after (1000, count) count () root = tk. Tk () root.title ("Counting Seconds") label = tk.Label (root, fg = "green") label.pack () counter_label (label) button = tk.Button (root, text = 'Stop', width = 25, command = root.destroy) button.pack () root.mainloop () The result of the previous script: