Python Forum
[Tkinter] check if entry is null - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] check if entry is null (/thread-34098.html)



check if entry is null - rwahdan - Jun-25-2021

Hi,

I am trying to check if an entry is null but not succeeded to do so.

cap_entry = sec_entry.get()
    if cap_entry in str(image_rand):
        complete_purchase()
    elif not cap_entry:
        cvv_screen.destroy()
        cart()
    else:
        cvv_screen.destroy()
        cart()
if I leave the entry field empty it goes to first if statement which goes to complete_purchase().


RE: check if entry is null - Yoriz - Jun-25-2021

Here is an example code that prints either Entry has a value or Entry is empty
import tkinter as tk
from tkinter import ttk


class TkApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.entry = ttk.Entry(self)
        self.entry.pack(padx=5, pady=5)
        self.button = ttk.Button(self, text='Print', command=self.on_button)
        self.button.pack(pady=5)

    def on_button(self):
        entry_value = self.entry.get()
        print(f'Entry contains: {entry_value}')
        if entry_value:
            print('Entry has a value')
        else:
            print('Entry is empty')


if __name__ == '__main__':
    app = TkApp()
    app.mainloop()



RE: check if entry is null - rwahdan - Jun-25-2021

Thanks,
It helped a lot.