【问题标题】:Python- Changing from pack() to .grid() results in AttributeError: 'NoneType' object has no attribute 'get'Python- 从 pack() 更改为 .grid() 会导致 AttributeError: 'NoneType' object has no attribute 'get'
【发布时间】:2015-03-30 07:06:45
【问题描述】:

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

我把代码改成这样:##请忽略注释掉的东西

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 文件就会出现它。我什至不必单击这两个按钮中的任何一个。

感谢您的帮助

【问题讨论】:

标签: python tkinter


【解决方案1】:

在 tkinter 中,Widget.packWidget.grid 都返回 None。所以,Input_Box 的值为None。您想在不同的行上创建和网格化您的小部件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-15
    • 2017-10-20
    • 2023-01-16
    • 2019-06-04
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多