为什么当我使用 state='readonly' 时条目小部件不显示文本



我正在尝试创建一个表来使用条目小部件显示文本。我已经编写了有效的代码,但是当我尝试添加state=readonly时,它根本不会显示文本。

因此,当我运行此代码时,它运行良好,但入口小部件是可编辑的:

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

然而,每当我在self.e的括号内添加state='readonly'时,该表就不再显示文本了。有人知道为什么会发生这种事吗?

顺便说一下,我使用的是python 3.6。

当状态为readonly时,无法编辑小部件。这意味着对insert的任何调用都将失败。这就是它的工作原理。

如果希望小部件为只读,请在插入文本后设置状态

相关内容

最新更新