Python从pack()更改为.grid()会导致AttributeError:“NoneType”对象没有属性“get”

2024-10-05 12:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在为一个小的加密程序开发tkinter接口时,我将它从.pack()中切换过来,我只在开始编写按钮时使用它,例如,to.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() 

我得到一个错误-

^{pr2}$

当您试图引用(在本例中为get)空的内容时,将出现此错误。但是我不明白为什么我一运行.py文件就会出现这个问题。我甚至不需要点击这两个按钮中的任何一个。在

感谢帮助


Tags: textaddinputbuttonrootsidelabelcommand

热门问题