Python—从pack()更改为.grid()会导致AttributeError:';非类型';对象没有属性';获取';

Python—从pack()更改为.grid()会导致AttributeError:';非类型';对象没有属性';获取';,python,tkinter,Python,Tkinter,在为一个小型加密程序开发tkinter接口时,我将它从.pack()切换到了.grid(),我只在开始写按钮时使用它。 之前我已经设置了一个条目小部件,两个按钮和两个链接到它们的函数。他们所做的只是将条目小部件中的内容打印到控制台 我将代码更改为:##请忽略注释掉的内容 import time from tkinter import * def doNothing(): ##To when a menu item is clicked print("Program did nothin

在为一个小型加密程序开发tkinter接口时,我将它从.pack()切换到了.grid(),我只在开始写按钮时使用它。 之前我已经设置了一个条目小部件,两个按钮和两个链接到它们的函数。他们所做的只是将条目小部件中的内容打印到控制台

我将代码更改为:##请忽略注释掉的内容

import time
from tkinter import *

def doNothing(): ##To when a menu item is clicked
    print("Program did nothing")
root = Tk()



menu = Menu(root)
root.config(menu=menu)

##subMenu = Menu(menu)
##menu.add_cascade(label="File", menu=subMenu)
##subMenu.add_command(label="New project", command=doNothing)
##subMenu.add_command(label="New", command =doNothing)
####subMenu.add_seperator()
##subMenu.add_command(label="Exit", command=doNothing)
##
##editMenu = Menu(menu)
##menu.add_cascade(label="Edit", menu=editMenu)
##editMenu.add_command(label="Redo", command=doNothing)
##
####Toolbar
##toolbar = Frame(root, bg="blue")
##insertButton = Button(toolbar, text="Insert image", command = doNothing)
##insertButton.pack(side=LEFT, padx=5, pady=5) ##Five pixels of space to pad the button
##printButton = Button(toolbar, text="Print", command = doNothing)
##printButton.pack(side=LEFT, padx=5, pady=5)
##
##toolbar.pack(side=TOP, fill=X) ##However wide the window is toolbar will take up the x axis
##
####Statusbar
##status = Label(root, text="Prepearing to do nothing", bd=1, relief=SUNKEN, anchor=W) ##bd => border
##status.pack(side=BOTTOM, fill=X)
##
##Text box
Input_Box = Entry(root, bg="grey").grid(row=0, column=0)
##Input_Box.pack(anchor=CENTER)
def Encrypt_Button_Press():
    User_Input = Input_Box.get()
    print(User_Input)
def Decrypt_Button_Press():
    User_Input = Input_Box.get()
    print(User_Input)

Encrypt_Button = Button(text="Encrypt", command=Encrypt_Button_Press).grid(row=1)
##Encrypt_Button.pack(anchor=CENTER, side=LEFT)

Decrypt_Button = Button(text="Decrypt", command=Decrypt_Button_Press).grid(row=2)
##Decrypt_Button.pack(anchor=CENTER, side=LEFT)
root.mainloop() 
我得到一个错误-

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:\Users\Theo_2\Google Drive\Computer science\Encryption and decryption work\Cipher 2\Tkinter 10 toolbar and statusbar.py", line 41, in Encrypt_Button_Press
    User_Input = Input_Box.get()
AttributeError: 'NoneType' object has no attribute 'get'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:\Users\Theo_2\Google Drive\Computer science\Encryption and decryption work\Cipher 2\Tkinter 10 toolbar and statusbar.py", line 41, in Encrypt_Button_Press
    User_Input = Input_Box.get()
AttributeError: 'NoneType' object has no attribute 'get'
当您试图引用(在本例中是获取)空的内容时,会出现此错误。但是,我不明白为什么在运行.py文件时它会出现。我甚至不必点击这两个按钮中的任何一个


tkinter、
Widget.pack和
Widget.grid中的帮助都返回
None
。因此,
Input\u Box
的值是
None
。您希望在单独的行上创建和网格化小部件

Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=0)
可能重复的