Search code examples
pythonpython-3.xtkintertk-toolkittkinter-entry

Why does the entry widget not display text when I use state='readonly'


I'm trying to create a table to display text using entry widgets. I've written code that work,s however when I try to add state=readonly it simply does not display the text.

So when I run this code, it works fine but the entry widgets are editable:

# Python program to create a table

from tkinter import *


class Table:

    def __init__(self, root):

        # code for creating table
        for i in range(total_rows):
            for j in range(total_columns):
                self.e = Entry(root, width=20, fg='blue',
                               font=('Arial', 16, 'bold'), state='readonly')

                self.e.grid(row=i, column=j)
                self.e.insert(END, lst[i][j])

            # take the data


lst = [(1, 'Raj', 'Mumbai', 19),
       (2, 'Aaryan', 'Pune', 18),
       (3, 'Vaishnavi', 'Mumbai', 20),
       (4, 'Rachna', 'Mumbai', 21),
       (5, 'Shubham', 'Delhi', 21)]

# find total number of rows and
# columns in list
total_rows = len(lst)
total_columns = len(lst[0])

# find total number of rows and
# columns in list
root = Tk()
t = Table(root)
root.mainloop()

However, whenever I add state='readonly' inside the brackets of self.e, the table just doesn't display text anymore. Does anyone know why this happens?

I'm using python 3.6 by the way.


Solution

  • When the state is readonly, the widget cannot be edited. That means that any call to insert will fail. That's how it's designed to work.

    If you want the widget to be read-only, set the state after inserting the text.