Python tkinter text widget in another definition does not allow insert

Question:

I'm new to programming and python. I would like to be able to insert in a definition box information that was created in another definition. How is this possible? According to the code, which is attached, the program gives an error. Thanks

import tkinter as tk
from tkinter import ttk

class Demo1:
    def __init__(self, master):

        self.master = master
        self.frame = tk.Frame(self.master)
        self.frame.grid()

        grupo1 = ttk.LabelFrame(self.frame, text='Nomes Pilotos')
        grupo1.grid(row = 0, column = 0, padx=10, pady=10, sticky = 'w')
        botao1 = ttk.Button(grupo1, width=18, text='Amaral SILVA', command = lambda : self.escreve('Amaral'))
        botao1.grid(padx=10, pady=5)
        botao2 = ttk.Button(grupo1, width=18, text='Jorge CASTRO', command = lambda : self.escreve('Jorge'))
        botao2.grid(padx=10, pady=5)

        grupo2 = ttk.LabelFrame(master, text = 'Informação:')
        grupo2.grid(row = 1, column = 0, columnspan = 4, padx = 10, pady = 1, sticky = 'w')
        texto = tk.Text(grupo2, height=10, width=20, fg='white', bg='#3D7475',font=('Consolas', 12))
        texto.grid()

    def escreve(self, jogador):

        texto.insert(END, jogador, ' é fixe!')

def main():
    root = tk.Tk()
    Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Answer:

I believe the problem is happening because the texto variable doesn't have a reference inside the class, so the escreve method can't access that variable.

If this is the problem, a possible solution is to create (and use) the texto variable as a property of the Demo1 class, example: self.texto instead of texto .

Below is the program with this modification:

#!python
# -*- coding: utf-8 -*-

import Tkinter as tk
import ttk

class Demo1:
    def __init__(self, master):

        self.master = master
        self.frame = tk.Frame(self.master)
        self.frame.grid()

        grupo1 = ttk.LabelFrame(self.frame, text='Nomes Pilotos')
        grupo1.grid(row = 0, column = 0, padx=10, pady=10, sticky = 'w')
        botao1 = ttk.Button(grupo1, width=18, text='Amaral SILVA', command = lambda : self.escreve('Amaral'))
        botao1.grid(padx=10, pady=5)
        botao2 = ttk.Button(grupo1, width=18, text='Jorge CASTRO', command = lambda : self.escreve('Jorge'))
        botao2.grid(padx=10, pady=5)

        grupo2 = ttk.LabelFrame(master, text = 'Informação:')
        grupo2.grid(row = 1, column = 0, columnspan = 4, padx = 10, pady = 1, sticky = 'w')

        # Aqui, mudar de texto para self.texto
        self.texto = tk.Text(grupo2, height=10, width=20, fg='white', bg='#3D7475',font=('Consolas', 12))
        self.texto.grid()

    def escreve(self, jogador):
        # Aqui, mudar de texto para self.texto, indicar o prefixo tk.END.
        # Concatenar o nome do jogador com a string através do operador '+'
        self.texto.insert(tk.END, jogador + ' é fixe!\n')

def main():
    root = tk.Tk()
    Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

tested with Python 2.7.11

Scroll to Top