python-course.eu

11. Dialogs in Tkinter

By Bernd Klein. Last modified: 01 Feb 2022.

Introduction

Tkinter (and TK of course) provides a set of dialogues (dialogs in American English spelling), which can be used to display message boxes, showing warning or errors, or widgets to select files and colours. There are also simple dialogues, asking the user to enter string, integers or float numbers.

Dialogues

Let's look at a typical GUI Session with Dialogues and Message boxes. There might be a button starting the dialogue, like the "quit" button in the following window:

Window with quit and answer button

Pushing the "quit" button raises the Verify window:

Verify Window

Let's assume that we want to warn users that the "quit" functionality is not yet implemented. In this case we can use the warning message to inform the user, if he or she pushes the "yes" button:

Warning Dialoque

If somebody types the "No" button, the "Cancel" message box is raised:

Message box

Let's go back to our first Dialogue with the "quit" and "answer" buttons. If the "Answer" functionality is not implemented, it might be useful to use the following error message box:

Error Message box

Python script, which implements the previous dialogue widges:

import tkinter as tk
from tkinter import messagebox as mb

def answer():
    mb.showerror("Answer", "Sorry, no answer available")

def callback():
    if mb.askyesno('Verify', 'Really quit?'):
        mb.showwarning('Yes', 'Not yet implemented')
    else:
        mb.showinfo('No', 'Quit has been cancelled')

tk.Button(text='Quit', command=callback).pack(fill=tk.X)
tk.Button(text='Answer', command=answer).pack(fill=tk.X)
tk.mainloop()

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here

Message Boxes

The message dialogues are provided by the 'messagebox' submodule of tkinter.

'messagebox' consists of the following functions, which correspond to dialog windows:

Open File Dialogue

There is hardly any serious application, which doesn't need a way to read from a file or write to a file. Furthermore, such an application might have to choose a directory. Tkinter provides the module tkFileDialog for these purposes.

import tkinter as tk
from tkinter import filedialog as fd 

def callback():
    name= fd.askopenfilename() 
    print(name)
    
errmsg = 'Error!'
tk.Button(text='File Open', 
       command=callback).pack(fill=tk.X)
tk.mainloop()

The code above creates a window with a single button with the text "File Open". If the button is pushed, the following window appears:

Choosing a file

The look-and-feel of the file-open-dialog depends on the GUI of the operating system. The above example was created using a gnome desktop under Linux. If we start the same program under Windows 7, it looks like this:

Choosing a file the Windows 7 way

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here

Choosing a Colour

There are applications where the user should have the possibility to select a colour. Tkinter provides a pop-up menu to choose a colour. To this purpose we have to import the 'tkinter.colorchooser' module and have to use the method askColor:

result = tkinter.colorchooser.askcolor ( color, option=value, ...)

If the user clicks the OK button on the pop-up window, respectively, the return value of askcolor() is a tuple with two elements, both a representation of the chosen colour, e.g. ((106, 150, 98), '#6a9662')
The first element return[0] is a tuple (R, G, B) with the RGB representation in decimal values (from 0 to 255). The second element return[1] is a hexadecimal representation of the chosen colour.
If the user clicks "Cancel" the method returns the tuple (None, None).

The optional keyword parameters are:

color The variable color is used to set the default colour to be displayed. If color is not set, the initial colour will be grey.
title The text assigned to the variable title will appear in the pop-up window's title area. The default title is "Color".
parent Make the pop-up window appear over window W. The default behaviour is that it appears over the root window.

Let's have a look at an example:

import tkinter as tk
from tkinter.colorchooser import askcolor                  

def callback():
    result = askcolor(color="#6A9662", 
                      title = "Bernd's Colour Chooser") 
    print(result)
    
root = tk.Tk()
tk.Button(root, 
          text='Choose Color', 
          fg="darkgreen", 
          command=callback).pack(side=tk.LEFT, padx=10)
tk.Button(text='Quit', 
          command=root.quit,
          fg="red").pack(side=tk.LEFT, padx=10)
tk.mainloop()

The look and feel depends on the operating system (e.g. Linux or Windows) and the chosen GUI (GNOME, KDE and so on). The following windows appear, if you use Gnome:

Choosing a Colour Startmenu

Choosing a Colour with Tkinter and Python

Using the same script under Windows 7 gives us the following result:

Choosing a Colour the Windows 7 way

 

 

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here