Python- Changing from pack() to .grid() results in AttributeError: 'NoneType' object has no attribute 'get'

烂漫一生 提交于 2021-02-17 05:16:13

问题


While developing a tkinter interface for a small encryption program I switched it from .pack(), which I only used while beginning to write out the buttons e.g., to .grid(). Previously I had set up an Entry widget, two buttons and two functions linked to them. All they did was print whatever was in the Entry widget to the console.

I changed the code to this: ##Please ignore the commented out stuff

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() 

And I get an error-

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'

This error is meant to appear when you are trying to reference (in this case get) something which is empt. However I don't understand why it comes up as soon as I run the .py file. I don't even have to click either of the two buttons.

Help is appreciated


回答1:


In tkinter, Widget.pack and Widget.grid both return None. So, Input_Box's value is None. You want to create and grid your widget on separate lines.

Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=0)


来源:https://stackoverflow.com/questions/28242117/python-changing-from-pack-to-grid-results-in-attributeerror-nonetype-ob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!