每当输入框为空时显示 MessageBox 错误

Showing a MessageBox error whenever an entry box is empty

我知道您可以通过检查输入框的长度来检查输入框是否为空。不过我很难实现它,因为我的输入框是动态创建的,你不能在一个循环中遍历所有输入框。

这是我的代码:

from tkinter import *

class Window(Canvas):
    def __init__(self,master=None,**kwargs):
        Canvas.__init__(self,master,**kwargs)
        self.frame = Frame(self)
        self.create_window(0,0,anchor=N+W,window=self.frame)
        self.row = 1

        self.input_x = []
        self.input_y = []

        self.x_values = []
        self.y_values = []

        self._init_entries()

    def _init_entries(self):
        x_value  = Label(self.frame, text='x', font='Helvetica 10 bold').grid(row = self.row, column = 2)
        y_value  = Label(self.frame, text='y', font='Helvetica 10 bold').grid(row = self.row, column = 3)
        self.row += 1

    def add_entry(self):

        def validate_int_entry(text):
            if text == "":
                return True
            try:
                value = int(text)
            except ValueError:
                return False
            return value
        vcmd_int = (root.register(validate_int_entry), "%P")

        x_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)
        x_value.grid(row = self.row, column = 2)

        y_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)         
        y_value.grid(row = self.row, column = 3)

        self.row += 1

        self.input_x.append(x_value)
        self.input_y.append(y_value)


    def save_entry(self):

        self.x_values.clear()
        self.y_values.clear()

        for entry in self.input_x:
            x = int(entry.get())
            self.x_values.append(x)
        print(self.x_values)

        for entry in self.input_y:
            x = int(entry.get())
            self.y_values.append(x)
        print(self.y_values)


if __name__ == "__main__":
    root = Tk()

    root.resizable(0,0)
    root.title('Lot')

    lot = Window(root)
    lot.grid(row=0,column=0)

    scroll = Scrollbar(root)
    scroll.grid(row=0,column=1,sticky=N+S)

    lot.config(yscrollcommand = scroll.set)
    scroll.config(command=lot.yview)
    lot.configure(scrollregion = lot.bbox("all"), width=1000, height=500)

    def add_points():
        lot.add_entry()
        lot.configure(scrollregion = lot.bbox("all"))

    b1 = Button(root, text = "Add points", command = add_points)
    b1.grid(row=1,column=0)

    def get_value():
        b1.destroy()
        lot.save_entry()

    b2 = Button(root, text = "Get value!", command = get_value)
    b2.grid(row=2,column=0)

    root.mainloop()

这是 GUI 示例,其中用户单击 'Add points' 按钮 5 次但忘记填写其中一个输入框。

因为我设置了输入框只能接受'int',所以会报错。如何在每次输入框为空时显示 MessageBox(如果可能,可以告诉用户哪个输入框为空)?

我在添加新行之前更改了您的 add_entry 函数,它将检查这两个字段,如果发现它为空,则会弹出警告消息。

def add_entry(self):

    for entry in self.input_x:
        if entry.get() is '':
            return messagebox.showwarning('Warning', 'Empty Fields')

    for entry in self.input_y:
        if entry.get() is '':
            return messagebox.showwarning('Warning', 'Empty Fields')

    def validate_int_entry(text):
        if text == "":
            return True
        try:
            value = int(text)
        except ValueError:
            return False
        return value
    vcmd_int = (root.register(validate_int_entry), "%P")

    x_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)
    x_value.grid(row = self.row, column = 2)

    y_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)
    y_value.grid(row = self.row, column = 3)

    self.row += 1

    self.input_x.append(x_value)
    self.input_y.append(y_value)