Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter – GUI: user text input with button that checks input for exceptions and closes window

I am creating a GUI that will accept user input in the text fields, and then after pressing the "Enter Data" button the program will check if the user input is a numeric (the input is later used later down the code). I am trying to make the button only close the window if no exception is raised (i.e. both text fields have text and it’s numeric). Where should I place the "window.destroy()" method?

Right now the program closes no matter what, and just spits out the exception warnings.

import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def enter_data_close():
    global user_input_1
    global user_input_2

    user_input_1=user_input_1_entry.get()
    user_input_2=user_input_2_entry.get()

    if user_input_1 and user_input_2:
        try:
            float(user_input_1)
            user_input_1=float(user_input_1)
            if user_input_1<=0:
                tkinter.messagebox.showwarning(title="Value Error!", message="user_input_1 must be a positive number(>0). Please provide a different value!")
        except ValueError:
            tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_1!")
        
        try:
            float(user_input_2)
            user_input_2=float(user_input_2)
            if user_input_2<=0:
                tkinter.messagebox.showwarning(title="Value Error!", message="user_input_2 must be a positive number(>0). Please provide a different value!")
        except ValueError:
            tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_2!")

        window.destroy()

    else:
        tkinter.messagebox.showwarning(title="Error", message="Provide values for all entry boxes!")

#GUI 

window = tkinter.Tk()
window.title("Dimensions Data Entry")
window.resizable(0,0)

frame = tkinter.Frame(window)
frame.pack()

def disable_event():
    pass
window.protocol("WM_DELETE_WINDOW", disable_event)

#DIMENSIONS

dimensions_frame =tkinter.LabelFrame(frame, text="Dimensions")
dimensions_frame.grid(row= 0, column=0, padx=20, pady=10)

user_input_1_label = tkinter.Label(dimensions_frame, text="user_input_1:")
user_input_1_label.grid(row=0, column=0)
user_input_2_label = tkinter.Label(dimensions_frame, text="user_input_2:")
user_input_2_label.grid(row=0, column=1)

user_input_1_entry = tkinter.Entry(dimensions_frame)
user_input_2_entry = tkinter.Entry(dimensions_frame)
user_input_1_entry.grid(row=1, column=0)
user_input_2_entry.grid(row=1, column=1)

for widget in dimensions_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)

# ENTER DATA AND CLOSE BUTTON
button = tkinter.Button(frame, text="Enter Data", command=enter_data_close)
button.grid(row=3, column=0, sticky="news", padx=20, pady=10)

window.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re really close. Just add return to the end of your messageboxes.

def enter_data_close():
    global user_input_1
    global user_input_2

    user_input_1=user_input_1_entry.get()
    user_input_2=user_input_2_entry.get()

    if user_input_1 and user_input_2:
        try:
            float(user_input_1)
            user_input_1=float(user_input_1)
            if user_input_1<=0:
                tkinter.messagebox.showwarning(title="Value Error!", message="user_input_1 must be a positive number(>0). Please provide a different value!")
                return
        except ValueError:
            tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_1!")
            return
        
        try:
            float(user_input_2)
            user_input_2=float(user_input_2)
            if user_input_2<=0:
                tkinter.messagebox.showwarning(title="Value Error!", message="user_input_2 must be a positive number(>0). Please provide a different value!")
                return
        except ValueError:
            tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_2!")
            return

        window.destroy()

    else:
        tkinter.messagebox.showwarning(title="Error", message="Provide values for all entry boxes!")
        return
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading