简体   繁体   中英

How to insert into tkinter Text widget

I'm trying to insert a txt file read variable into the tkinter Text widget, but, I'm having this error: _tkinter.TclError: bad window path name ".!toplevel"

Check out my code:

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)

It should be tk.END and not END simply because your import is import tkinter as tk , so you should suffix END with tk . Also you could say 'end' instead of tk.END .

So to conclude:

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

Hope this cleared your error, do let me know if any more doubts or errors.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM