Python tkinter text widget in another setting does not allow insert

3

I'm new to programming and to python. I would like to be able to enter a a box information that was created in another setting. 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()
    
asked by anonymous 27.02.2016 / 20:50

1 answer

2

I believe the problem is happening because the texto variable does not have a reference within the class, so the escreve method can not access this variable.

If the problem is this, a possible solution is to create (and use) the variable texto as property of class Demo1 , 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

    
27.02.2016 / 22:22