3

like in HTML tag attribute required=required

I want make an Entry widget mandatory, the user must enter data in it, otherwise don't proceed to next step.

How to do it with tkinter?

martineau
  • 119,623
  • 25
  • 170
  • 301
Sreekanth
  • 87
  • 2
  • 8

2 Answers2

4

There is no attribute "required" in Tkinter, you need to write a function to check whether the user entered data in the entry or not. Then use this function as the command of the "Next" button.

import tkinter as tk

def next_step():
    if mandatory_entry.get():
        # the user entered data in the mandatory entry: proceed to next step
        print("next step")
        root.destroy()
    else:
        # the mandatory field is empty
        print("mandatory data missing")
        mandatory_entry.focus_set()

root = tk.Tk()

mandatory_entry = tk.Entry(root)

tk.Label(root, text="Data *").grid(row=0, column=0)
mandatory_entry.grid(row=0, column=1)
tk.Button(root, text='Next', command=next_step).grid(row=1, columnspan=2)

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
1

Without your own function its not possible

from tkinter import *
def check_empty() :
     if entry.get():
         pass     #your function where you want to jump
     else:
        print(' input required') 


mw=Tk()
Txt=Lable(mw, text='enter data').pack()
entry=Entry(mw, width=20).pack()
Btn=Button(mw, text='click', command=check_empty).pack()
mw.mainloop()

If you have single field then jump to a new function or class else if multiple entry blocks then use pass if successfully written some data in the entry field

Remember above code will check multiple fields at the same time after clicking the button.

Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
Er M S Dandyan
  • 364
  • 3
  • 3