Python Tkinter text widget in another setting does not allow Insert

Asked

Viewed 726 times

3

I am new to programming and python. I would like to insert a definition a box information that was created in another definition. How is that possible? According to the code, which is attached, the program gives error. Thank you

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()

1 answer

2

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

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

Below, follow 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.