try: except нужен для обработки ошибок, в данном же случае в первую очередь нужна просто проверка.
Код ниже для примера, то что вы ищете это
messagebox. Для новичка хороший туториал -
тут
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import showerror, showinfo
root = Tk()
root.geometry("250x200")
def clear():
entry_username.delete(0, END)
entry_password.delete(0, END)
def open_info():
showinfo(title="Info", message="Connected")
def open_error():
showerror(title="Error", message="Incorrect input")
def connect_to_db():
accepted_user = 'admin'
accepted_password = 'admin'
try:
print('Connect to DB')
username = entry_username.get()
password = entry_password.get()
if accepted_user == username and password == accepted_password:
open_info()
else:
open_error()
except Exception:
open_error()
finally:
clear()
label_username = ttk.Label(text = "USERNAME")
label_username.pack(anchor=NW, padx=6, pady=6)
entry_username = ttk.Entry()
entry_username.pack(anchor=NW, padx=6, pady=6)
label_password = ttk.Label(text = "PASSWORD")
label_password.pack(anchor=NW, padx=6, pady=6)
entry_password = ttk.Entry()
entry_password.pack(anchor=NW, padx=6, pady=6)
display_button = ttk.Button(text="Connect", command=connect_to_db)
display_button.pack(side=LEFT, anchor=N, padx=6, pady=6)
clear_button = ttk.Button(text="Clear", command=clear)
clear_button.pack(side=LEFT, anchor=N, padx=6, pady=6)
root.mainloop()