import tkinter as tk
root=tk.Tk()
frm=tk.Frame(root)
text=tk.Text(frm,width=10,height=3).pack()
frm.pack()
tk.Button(text="btn",command=[print(text.get("1.0","end"))])
报错:AttributeError: 'NoneType' object has no attribute 'get'
你的text对象后面不要加pack(),这样的话返回的是NoneType。
这样改:
text=tk.Text(frm,width=10,height=3)
text.pack()
主要错在: text=tk.Text(frm,width=10,height=3).pack()
text=pack()返回了None值,不是Text控件的句柄
分两行写就好了:
text=tk.Text(frm,width=10,height=3)
text.pack()
测试如下:Button的command参数可改为函数
import tkinter as tk
def prntext():
print(text.get(1.0,"end"))
root=tk.Tk()
root.geometry('400x320')
frm=tk.Frame(root)
text=tk.Text(frm,width=10,height=3)
frm.pack()
text.pack()
text.insert('insert', "python")
btn = tk.Button(text="btn",command=prntext)
btn.pack()