Exception in Tkinter callbackTraceback most recent call last File cUsers57160Anaconda3libtkinter__init__py line 1702 in __call__ return selffuncargs File ipython-input-19-950751c9690e line 7 in l
This error occurs because the variable txt is a string and not a tkinter StringVar object. The .set() method can only be used on StringVar objects to set their value.
To fix this error, you need to create a StringVar object and set it as the value of the Entry widget. Then, in the command of the Button widget, you can update the value of this StringVar object using the .set() method.
Here's an example code snippet:
from tkinter import *
from tkinter import filedialog
def openfile():
filename = filedialog.askopenfilename()
return filename
window = Tk()
txt = StringVar()
entry = Entry(window, textvariable=txt)
entry.pack()
btn = Button(window, text="导入csv数据集", command=lambda: txt.set(openfile()))
btn.pack()
window.mainloop()
In this example, we create a StringVar object txt and set it as the textvariable of the Entry widget. Then, in the command of the Button widget, we update the value of txt using the .set() method. This way, the value of the Entry widget will also be updated
原文地址: http://www.cveoy.top/t/topic/eDvK 著作权归作者所有。请勿转载和采集!