Tkinter field in blank for Python

Viewed 56

I need your help to figure out how to make the following coding:

My idea is to get a message if fields name and quantity are in blank at the time I execute a function through a button. I printed what is the value if name and quantity are in blank and program says it is .!entry and .!entry2 respectively.

Unfortunately it doesn't work. What I am doing wrong?

if name == .!entry and quantity == .!entry2:
    message = Label(root, text = 'Name and Quantity required' , fg = 'red')
    message.grid(row = 4, column = 0, columnspan = 2, sticky = W + E)            
    return
1 Answers

As @JordyvanDongen pointed out in the comments you should use entry.get() to get the text inside the the entry object.

I suggest this for you code, assuming the entry objects are named name and quantity:

if not name.get() or not quantity.get():
    message = Label(root, text = 'Both name and quantity are required', fg = 'red')
    message.grid(row = 4, column = 0, columnspan = 2, sticky = W + E)            
    return None

I changed the conditional to or so that if either of the fields are blank it will be triggered but you may want to change it back to and depending on your purposes..

Related