Search code examples
python-3.xtkintertkinter-entrytkmessagebox

Showing a MessageBox error whenever an entry box is empty


I know that you can check if the entry box is empty by checking its length. I'm having a hard time implementing it though, because my entry boxes are dynamically created and you can't traverse in all of it in a single loop.

Here is my code:

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

Here's the GUI example wherein the user clicked the 'Add points' button 5 times but forgot to fill one of the entry boxes.

GUI example Since I set that the entry boxes can only accept 'int', then it will throw an error. How can I show a MessageBox every time an entry box is empty (and if it's possible, can tell the user what entry box is empty)?


Solution

  • I have change your add_entry function before adding new row it will check the both fields if it find it empty the warning message will popup.

    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)