Python- Changing from pack() to .grid() results in AttributeError: 'NoneType' object has no attribute 'get'
Python- Changing from pack() to .grid() results in AttributeError: 'NoneType' object has no attribute 'get'
在为小型加密程序开发 tkinter 界面时,我将其从 .pack() 切换到 .grid(),我只在开始写按钮时才使用它。
之前我已经设置了一个 Entry 小部件、两个按钮和两个链接到它们的功能。他们所做的只是将 Entry 小部件中的任何内容打印到控制台。
我把代码改成这样:##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()
我得到一个错误-
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'
当您尝试引用(在本例中为 get)一些空的东西时,就会出现此错误。但是我不明白为什么它会在我 运行 .py 文件后立即出现。我什至不必点击这两个按钮中的任何一个。
感谢帮助
在 tkinter 中,Widget.pack
和 Widget.grid
都是 return None
。所以,Input_Box
的值为 None
。您想在不同的行上创建和网格化您的小部件。
Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=0)
在为小型加密程序开发 tkinter 界面时,我将其从 .pack() 切换到 .grid(),我只在开始写按钮时才使用它。 之前我已经设置了一个 Entry 小部件、两个按钮和两个链接到它们的功能。他们所做的只是将 Entry 小部件中的任何内容打印到控制台。
我把代码改成这样:##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()
我得到一个错误-
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'
当您尝试引用(在本例中为 get)一些空的东西时,就会出现此错误。但是我不明白为什么它会在我 运行 .py 文件后立即出现。我什至不必点击这两个按钮中的任何一个。
感谢帮助
在 tkinter 中,Widget.pack
和 Widget.grid
都是 return None
。所以,Input_Box
的值为 None
。您想在不同的行上创建和网格化您的小部件。
Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=0)