Q:

How to assign a variable to tkinter's entry box using a function

Here's an example of code before I explain my issue.
from tkinter import *
from tkinter import messagebox

class Add:

    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.grid()

        self.entry_1 = Entry(self.frame)
        self.entry_1.grid(row=0, column=0)

        self.entry_2 = Entry(self.frame)
        self.entry_2.grid(row=1, column=0)

        self.button = Button(self.frame, text='Submit', command=self.submit)
        self.button.grid(row=0, column=1)

    def submit(self):
        # Checks if both entries are digits.
        if self.entry_1.get().isdigit() is False:
            messagebox.showerror('Error', 'Invalid Character(s).')
        elif self.entry_2.get().isdigit() is False:
            messagebox.showerror('Error', 'Invalid Character(s).')

        add_entry_1_and_2 = int(self.entry_1.get()) + int(self.entry_2.get())
        print(add_entry_1_and_2)

root = Tk()
Add(root)
root.mainloop()

This code works great as long as I have a number assigned to both entry boxes, but if I leave one of the boxes empty and press the submit button it raises this error.
ValueError: invalid literal for int() with base 10: ''

What I want is if one of the boxes is left empty I would like to assign that empty box to the value of 0. How could I achieve this?

A:

You could set a default value zero in each of the entry fields, and reset this value upon submitting.
Then you could check the validity of an entry value in a try-except block, and proceed to adding the values only if they conform, otherwise, raise an exception that triggers the Error popup.
something like this, maybe?
from tkinter import *
from tkinter import messagebox

class Add:

    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.grid()

        self.default_value = '0'
        self.entry_1 = Entry(self.frame)
        self.entry_1.grid(row=0, column=0)

        self.entry_2 = Entry(self.frame)
        self.entry_2.grid(row=1, column=0)

        self.button = Button(self.frame, text='Submit', command=self.submit)
        self.button.grid(row=0, column=1)

        self.reset_entry_defaults()

    def reset_entry_defaults(self):
        self.entry_1.delete(0,END)
        self.entry_2.delete(0,END)
        self.entry_1.insert(0, self.default_value)
        self.entry_2.insert(0, self.default_value)

    def submit(self):
        try:
            e1 = int(self.entry_1.get())   # <-- you may prefer to convert to float
            e2 = int(self.entry_2.get())   # <-- you may prefer to convert to float
        except ValueError:
            messagebox.showerror('Error', 'Invalid Character(s).')
        else:
            add_entry_1_and_2 = e1 + e2
            print(add_entry_1_and_2)
        self.reset_entry_defaults()

root = Tk()
Add(root)
root.mainloop()

Optionally, you could have your class Add inherit from tk.Frame (be a tk.Frame), and do something like this:
import tkinter as tk                   # <-- avoid star imports
from tkinter import messagebox

class Add(tk.Frame):                   # <-- inherit from tk.Frame

    def __init__(self, master):
        self.master = master
        super().__init__(self.master)  # <-- call the parent class

        self.default_value = '0'
        self.entry_1 = tk.Entry(self)
        self.entry_1.grid(row=0, column=0)

        self.entry_2 = tk.Entry(self)
        self.entry_2.grid(row=1, column=0)

        self.button = tk.Button(self, text='Submit', command=self.submit)
        self.button.grid(row=0, column=1)

        self.reset_entry_defaults()

    def reset_entry_defaults(self):
        self.entry_1.delete(0, tk.END)
        self.entry_2.delete(0, tk.END)
        self.entry_1.insert(0, self.default_value)
        self.entry_2.insert(0, self.default_value)

    def submit(self):
        try:
            e1 = int(self.entry_1.get())
            e2 = int(self.entry_2.get())
        except ValueError:
            tk.messagebox.showerror('Error', 'Invalid Character(s).')
        else:
            add_entry_1_and_2 = e1 + e2
            print(add_entry_1_and_2)
        self.reset_entry_defaults()

root = tk.Tk()
Add(root).grid()    # <-- call the tk geometry manager to place your frame in root
root.mainloop()

