I'm trying to take a number from inputtxt (which is Text widget) , I need to test the input ; if it's not a number the program should gives the user another chance to enter the correct input. I did this ; the problem is the warning message shows but the program proceeds . I need a way to give the user another chance without quitting the program.
import tkinter
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
from tkinter import messagebox
import subprocess
from tkinter import filedialog
window = Tk()
window.title("Test")
window.geometry('650x400')
frame = Frame(window)
# Gets both half the screen width/height and window width/height
positionRight = 350
positionDown = 100
# Positions the window in the center of the page.
window.geometry(" {} {}".format(positionRight, positionDown))
inputtxt2 = Text(window, height = 1, width =24,bg = "light yellow")
inputtxt2.pack()
inputtxt2.place(x=325, y=108)
def run_button():
num_of_compare_points= inputtxt2.get('1.0', 'end-1c').strip().replace("\n","")
if not (num_of_compare_points.isnumeric()):
messagebox.showwarning("Warning","Please Enter a Valid Integer Value for Group Size!")
print (int(num_of_compare_points) 1)
runButton = tkinter.Button(window, text ="Run",font = "Times", command = run_button)
runButton.pack()
runButton.place(x=260, y=320)
window.mainloop()
CodePudding user response:
It's not really clear what your problem is. If the data is invalid, you can just return from the function without doing anything.
def run_button():
num_of_compare_points= inputtxt2.get('1.0', 'end-1c').strip().replace("\n","")
if not (num_of_compare_points.isnumeric()):
messagebox.showwarning("Warning","Please Enter a Valid Integer Value for Group Size!")
return
print (int(num_of_compare_points) 1)