Python Forum
[Tkinter] check if entry is null
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] check if entry is null
#1
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().
Reply
#2
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()
rwahdan likes this post
Reply
#3
Thanks,
It helped a lot.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,463 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,493 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020