Skip to content

Modal forms with TKinter and Python


I keep on learning (and sharing with you) Tkinter, and today I am going to write about modal forms, of course, customized to my needs (you can modify my code to suit your needs). In order to settle my knowledge, I have started from this code: But I need some adjustments, in order to translate values from one form to another. A simple but tricky exercise.

Modal Forms

Every time you need a modal form is because you need new data to be use on another form, and that’s what I am explaing here. Let’s create a form, with a button and a label. When I click on the button, I will be asked for a new value using a modal form, that it will be write on the label.

First, let’s create the main form with a class named main, creating the button and the label. When clicking on the button, let’s transfer the value of the label. As you can see, and due to the characteristic of the variable, a StringVar, as you type the value change. If you need to validate the value, the solution is un broke the link when creating the Entry, and do the verification process on the ok function.

The full code is here:

from Tkinter import *

class main(Frame):
    def __init__(self, master):
        Frame.__init__(self, master=None)
        self.master.title("Probando Dialogos - Manejando datos")
        Button(root, text="cambiar valor", command=self.dialogo).pack()
        self.valor = StringVar()
        self.valor.set("Hola Manejando datos")
        Label(self.master, textvariable=self.valor).pack()

    def dialogo(self):
        d = MyDialog(root, self.valor, "Probando Dialogo", "Dame valor")

class MyDialog:
    def __init__(self, parent, valor, title, labeltext = '' ):
        self.valor = valor = Toplevel(parent)
        if len(title) > 0:
        if len(labeltext) == 0: labeltext = 'Valor'
        Label(, text=labeltext).pack()"<Return>", self.ok)
        self.e = Entry(, text=valor.get())
        self.e.bind("<Return>", self.ok)
        self.e.bind("<Escape>", self.cancel)
        b = Button(, text="OK", command=self.ok)

    def ok(self, event=None):
        print "Has escrito ...", self.e.get()

    def cancel(self, event=None):

root = Tk()
a = main(root)

The methos to be aware of are:

  • wait_window is the method that launch the new form
  • grab_set is the function that force the form to be modal, and you can’t work with the base form until the modal for is destroyed


Tkinter Formularios modales

Tkinter Formularios modales


I hope you use it in your projects! And have a nice day!

Manejando Datos Newsletter

Noticias, entradas, eventos, cursos, … News, entrances, events, courses, …

Gracias por unirte a la newsletter. Thanks for joining the newsletter.