如何插入tkinter文本小部件

时间:2020-09-15 20:39:35

标签: python tkinter

我正在尝试将txt文件读取变量插入tkinter文本小部件,但是,出现此错误:_tkinter.TclError: bad window path name ".!toplevel"

查看我的代码:

textInfo = tk.Text(accountPopup, width=40, height=10, font=("Helvetica", 12))
            textInfo.pack(pady=20)

            path = './Contas/Info/'+txtName+'.txt'

            textFile = open(path , 'r')
            readedText = textFile.read()

            finalRead = str(readedText)

            textInfo.insert(tk.END, finalRead)

1 个答案:

答案 0 :(得分:1)

它应该是tk.END而不是END,这仅仅是因为您的导入是import tkinter as tk,所以您应该在END后缀tk。您也可以说'end'而不是tk.END

因此得出结论:

tk.END #import tkinter as tk
END #from tkinter import *
'end' #you can use this with any of the above type of imports

希望这可以清除您的错误,如果还有其他疑问或错误,请告诉我。

欢呼