tkinter Text.get()引发了一个TypeError异常

时间:2014-06-02 16:20:53

标签: python tkinter

from tkinter import *

def save_d():
    files = open("mp3list.txt","a")
    files.write("title :\n")
    files.write("%s" % lists.get())
    files.write("appraisal :\n")
    files.write("%s\n" % appraisal.get())
    lists.delete(0,END)
    appraisal.delete("1.0",END)

def read_f(file):
    lists = []
    lists_f = open(file)
    for line in lists_f:
        lists.append(line.rstrip())
    return lists

app = Tk()
app.title(" MP3 Player " )

Label(app,text = "lists:").pack()
lists = StringVar()
lists.set(None)
options = read_f("mp3.txt")
OptionMenu(app,lists,*options).pack()

Label(app,text = "appraisal:").pack()
appraisal = Text(app)
appraisal.pack()

Button(app,text = "Save",command = save_d).pack()
app.mainloop()

例外:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
    return self.func(*args)
  File "C:\Users\J\Desktop\mp3.py", line 8, in save_d
    files.write("%s\n" % appraisal.get())
TypeError: get() missing 1 required positional argument: 'index1'

为什么会出现错误?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您需要向Text.get()函数传递一个开始和停止索引,指示您想要的Text窗口小部件中的文本部分。

如果您想获取Text小部件中的所有文字,可以使用

appraisal.get('1.0', END)

Documentation for Text.get