【问题标题】:Can't get() from Text() object in tkinter (python 3.5) [duplicate]无法从 tkinter(python 3.5)中的 Text()对象中获取()[重复]
【发布时间】:2016-12-24 17:39:49
【问题描述】:

我正在 tkinter 中制作应用程序,由于某种原因,我无法从 Text() 类的对象调用函数“get()”:我收到错误

AttributeError: 'NoneType' object has no attribute 'get'

我做错了什么? 这是我的代码:

  1 import tkinter
  2 
  3 
  4 class Main():
  5 
  6     def __init__(self):
  7         #Defining Variables:
  8 
  9         background_color = '#%02x%02x%02x' % (223,219,195)
 10         menubar_color = '#%02x%02x%02x' % (191, 167, 126)
 11         menubar_active = '#AB936A'
 12 
 13         #Creating Window:
 14         root = tkinter.Tk()
 15         root.geometry('1000x600')
 16         root.configure(background=background_color)
 17 
 18         #Menu:
 19         menubar = tkinter.Menu(root,bg=menubar_color,activebackground=menuba    r_active,borderwidth=0,font='quicksand.otf')
 20         menubar.add_command(label='Open',command=self.open_file)
 21         menubar.add_cascade(label='Save')
 22         menubar.add_cascade(label='Save As')
 23         menubar.add_cascade(label='New File')
 24 
 25         root.config(menu=menubar)
 26 
 27         #TextEntry Box:
 28         self.textinput = tkinter.Text().grid(row=0,column=0)
 29         root.mainloop()
 30 
 31     def open_file(self):
 32         text = self.textinput.get()
 33         print(text)
 34 
 35 if __name__ == '__main__':
 36     Main()

感谢您的帮助!

【问题讨论】:

  • tkinter.Text().grid(row=0,column=0) .grid() 方法返回 Noneself.textinput = tkinter.Text() ; self.textinput.grid(...) 也是如此

标签: python object tkinter python-3.5 nonetype


【解决方案1】:

没关系!我碰巧想通了。而不是一行说

textinput = tkinter.Text().grid(row=0,column=0)

我把它变成了两行说

self.textinput = tkinter.Text()
self.textinput.grid(row=0,column=0)

而不是

self.textinput.get()

我用过

self.textinput.get('1.0','end-1c')

【讨论】:

    猜你喜欢
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多