【问题标题】:Show MessageBox if Entry empty [duplicate]如果条目为空,则显示 MessageBox [重复]
【发布时间】:2023-03-03 00:06:01
【问题描述】:

谁能告诉我我做错了什么?!我正在尝试在 Python 3 中使用 tkinter 创建 GUI。如果用户单击按钮并且同时 Entry 为空,我想显示 messageBox。 下面是我使用的代码。

知道我有这个错误

if len(self.entryBox.get()) == 0:
AttributeError: 'NoneType' object has no attribute 'get'

如果self.entryBox is None:,我也尝试使用它,但在这种情况下,我在运行项目后立即看到 messageBox,现在是正确的。我真的很困惑。

代码:

    from tkinter import *

    class Application(Frame):
         def __init__(self, master):
            super(Application, self).__init__(master)
            self.grid()
            self.widgets()

         def widgets(self):
            self.entryBox = Entry(self).grid()
            Button(self, text="Submit", command=self.search()).grid()

         def search(self):
             if len(self.entryBox.get()) == 0:
                 tkinter.messagebox.showinfo("Warning!", "Box is empty! Write something")
             else:
                 do_something()

    # main
    root = Tk()
    root.title("Title")
    app = Application(root)
    root.mainloop()

【问题讨论】:

    标签: python python-3.x object tkinter attributeerror


    【解决方案1】:
    1. self.entryBox = Entry(self).grid() 行会将.grid() 的结果分配给self.entryBox。试试这个:

      self.entryBox = Entry(self)
      self.entryBox.grid()
      
    2. 通过执行Button(self, text="Submit", command=self.search()).grid(),您会遇到类似的问题,因为您运行一次search 方法,然后将该方法的结果绑定到command 参数。相反,这应该可以工作(注意没有()

      Button(self, text="Submit", command=self.search).grid()
      

    【讨论】:

    • 嗯,太奇怪了。我现在没有 AttributeError 但我在我的帖子中也描述了第二个问题。运行项目后,我立即看到 messageBox。只有当用户没有在 Entry 中写任何内容并单击提交按钮时,我才需要查看它。希望你能理解我。
    • @NurzhanNogerbek 抱歉,没有看到第二期。查看我的编辑。
    【解决方案2】:

    我认为有两个问题:

    (1) self.entryBox

    代码

    self.entryBox = Entry(self).grid()
    

    将使self.entryBox 成为grid() 的返回值,而不是Entry 小部件对象。将该行更改为

    self.entryBox = Entry(self)
    self.entryBox.grid()
    

    (2) 将命令绑定到按钮

    将回调函数绑定到按钮时,必须传递函数本身。改变

    Button(self, text="Submit", command=self.search()).grid()
    

    Button(self, text="Submit", command=self.search).grid()
    

    。此外,如果您将 Button 设置为属性,

    self.button = Button(self, text="Submit", command=self.search)
    self.button.grid()
    

    您可以通过其他方法控制按钮。

    以下示例适用于我的计算机。

    # -----
    # from tkinter import *
    from Tkinter import *
    import tkMessageBox
    # -----
    
    class Application(Frame, object):
         def __init__(self, master):
            super(Application, self).__init__(master)
            self.grid()
            self.widgets()
    
         def widgets(self):
            #self.entryBox = Entry(self).grid()
            self.entryBox = Entry(self)
            self.entryBox.grid()
    
            self.button = Button(self, text="Submit", command=self.search)
            self.button.grid()
    
         def search(self):
             if len(self.entryBox.get()) == 0:
                 # -----
                 # messagebox.showinfo("Warning!", "Box is empty! Write something")
                 tkMessageBox.showinfo("Warning!", "Box is empty! Write something")
                 # -----
             else:
                 # do_something()
                 # -----
                 # print(self.entryBox.get())
                 print self.entryBox.get()
                 # -----
    
    # main
    root = Tk()
    root.title("Title")
    app = Application(root)
    root.mainloop()
    

    (我现在没有python 3,所以我将一些行修改为python 2的样式。)

    【讨论】:

    • 您好!我按照你说的写了,但是我在 init.py 文件中有新的错误 (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: unknown option "-wrap" 你对此有什么想法吗?我真的很困惑。
    • 我安装了 python 3.5,并尝试了以下操作:pastebin.com/5W7Uj8YG(结果:tinypic.com/r/2vifk9u/9)我认为这段代码在我的 python 3 中运行良好。也许这个错误是由于代码之外的东西。
    • init.py 是你写的另一个模块吗?那么,发布那个模块的源码怎么样?
    【解决方案3】:

    原因是函数一被调用,按钮就被调用,替换为

    Button(self, text="Submit", command=lambda: self.search()).grid()
    

    这对我来说很好

    from tkinter import *
    import tkinter as tk
    
    class Application(Frame):
         def __init__(self, master):
            super(Application, self).__init__(master)
            self.grid()
            self.widgets()
    
         def widgets(self):
            self.entryBox = tk.Entry(self)
            self.entryBox.grid()
            print(self.entryBox.get())
            Button(self, text="Submit", command=lambda: self.search()).grid()
            print(self.entryBox)
         def search(self):
             print(self.entryBox.get())
             if len(self.entryBox.get()) == 0:
                 tk.messagebox.showinfo("Warning!", "Box is empty! Write something")
             else:
                 do_something()
    
    # main
    root = Tk()
    root.title("Title")
    app = Application(root)
    root.mainloop()
    

    【讨论】:

    • 我使用了你所说的 lambda,但是我在 init.py 文件中出现了新的错误,(widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: unknown option "-wrap" 你对此有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多