0

Hey I am using Tkinter Python and I have a name entry.

If an invalid name is entered it will come up with a error in a new window. I want the original window or button to be greyed out while this error is open, and then go back to normal once it is closed. Is this possible? I don't want the button to be spammed and open multiple error boxes.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    You can use `tkinter.messagebox.showerror()` which will disable the parent window when it is shown. – acw1668 Sep 04 '20 at 03:58

1 Answers1

0

I will tell you 2 approaches to do that.

I would suggest using a tkinter.messagebox to show the error(or info ) to the user because it will disable the root window until it remains open. It plays sounds as per system preferences and allows pressing enter to close out the pop-up

Like this::

import tkinter as tk
import tkinter.messagebox as msgb   # use your preferred import style

def submit():
    if e.get() == 'something':  # check input here
        doSomething()
    else:
        msgb.ShowError('Invalid input', 'seems like you entered incorrect name')

root = tk.Tk()

e = tk.Entry(root)
e.pack()
btn = tk.Button(root, text='Submit', command=submit)
btn.pack()
root.mainloop()

2nd Approach:

If you really need to only use a Toplevel window and have to grey out entry box (and/or the button) - here is how

import tkinter as tk

def submit():
    if e.get() == 'something':  # check input here
        doSomething()
    else:
        e.configure(state=tk.DISABLED)
        btn.configure(state=tk.DISABLED)
        newWindow = tk.Toplevel()
        showSomethingHere()

root = tk.Tk()

e = tk.Entry(root)
btn = tk.Button(root, text='Submit', command=submit)
e.pack()
btn.pack()
root.mainloop()
P S Solanki
  • 1,033
  • 2
  • 11
  • 26