Tkinter文本小部件错误

时间:2018-03-10 20:20:31

标签: python tkinter

当我在文本框中输入文本值并单击从此代码生成的GUI表单中的生成按钮时,它不会打印所需的输出,而是我得到以下错误

Exception in Tkinter callback
   Traceback (most recent call last):
   File 
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
   return self.func(*args)
 File "/Users/Desktop/tk.py", line 28, in <lambda>
   b1 = Button(root, text='Generate', command=(lambda e=ents: 
fetch(e)))
 File "/Users/Desktop/tk.py", line 7, in fetch
   text  = text[1].get()
TypeError: get() missing 1 required positional argument: 'index1'

CODE

from tkinter import *
fields = 'Section1','Section2'    

def fetch(entries):
   for text in entries:
      field = text[0]
      text  = text[1].get()
      print('%s \n%s\n' % ("\n====================================\n" + field + "\n====================================\n", text))    

def makeform(root, fields):
   entries = []
   for field in fields:
      row = Frame(root)
      lab = Label(row, width=35, text=field, anchor='w')
      ent = Text(row, height=10)
      row.pack(side=TOP, fill=X, padx=15, pady=15)
      lab.pack(side=LEFT)
      ent.pack(side=RIGHT, expand=YES, fill=X)
      entries.append((field, ent))
   return entries    

if __name__ == '__main__':
   root = Tk()
   root.title('Form')
   #Text.get()
   ents = makeform(root, fields)
   root.bind('<Return>', (lambda event, e=ents: fetch(e)))
   b1 = Button(root, text='Generate', command=(lambda e=ents: fetch(e)))
   b1.pack(side=LEFT, padx=5, pady=5)
   b2 = Button(root, text='Quit', command=root.quit)
   b2.pack(side=LEFT, padx=5, pady=5)
   root.mainloop()

1 个答案:

答案 0 :(得分:0)

阅读文本Wiget文档并解决问题, 我在get方法

中使用了索引参数

电流:

text  = text[1].get()

MODIFY

text = text[1].get('1.0', 'end')