如果Entry为空,则显示MessageBox

时间:2016-05-09 11:30:19

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

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

知道我有错误

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

此外我尝试使用此self.entryBox is None:,但在这种情况下,我会在运行现在正确的项目后立即看到messageBox。我真的很感激。

CODE:

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

3 个答案:

答案 0 :(得分:1)

  1. self.entryBox = Entry(self).grid()会将.grid()的结果分配给self.entryBox。 Intead,试试这个:

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

答案 1 :(得分:1)

原因是一旦调用该函数就会调用该按钮,用

替换该行
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()

答案 2 :(得分:1)

我认为有两个问题:

(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样式。)