How to detect if a variable is null?

3

I'm trying to validate a simple form in Python 3 and I want to prevent it from registering the information if the nome field is empty, but when I make a if to check that the name variable is empty, it it returns me that it is not null.

Follow the code:

import Imports
import Dados

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

Imports.Connect_db()


class Gravar(object):
    def __init__(self):
        # Abre o clipboard
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        self.entry = Gtk.Entry()

        # Abre o xml
        builder = Gtk.Builder()
        builder.add_from_file("form.glade")

        # Pega a janela
        self.window = builder.get_object("window1")

        # Pega a area de texto
        self.text_name = builder.get_object("text_name")
        self.text_passwd = builder.get_object("text_passwd")
        self.text_numb = builder.get_object("text_numb")

        # abre a janela de dialogo
        self.about = builder.get_object("about_dialog")

        # Abre as janelas
        self.window.show()

        builder.connect_signals({"Gtk_main_quit": Gtk.main_quit,
                                 "on_submit_clicked": self.cadastro,
                                 "on_text_name_activate": self.cadastro,
                                 "on_text_passwd_activate": self.cadastro,
                                 "on_text_numb_activate": self.cadastro,
                                 "on_delete_activate": self.delete_text,
                                 "on_about_activate": self.about_window})

    def cadastro(self, widget):
        name = self.text_name.get_text()
        passwd = self.text_passwd.get_text()
        numb = self.text_numb.get_text()

        if name == None:
            print("Nula")
        else:
            print("n nula")

        Dados.commit(name, passwd, numb)

        Gravar.delete_text(self, widget)

    def delete_text(self, widget):
        self.text_name.set_text("")
        self.text_passwd.set_text("")
        self.text_numb.set_text("")

    def about_window(self, widget):
        self.about.run()

        self.about.hide()


if __name__ == "__main__":
    win = Gravar()

    Gtk.main()

Glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkAboutDialog" id="about_dialog">
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="modal">True</property>
    <property name="type_hint">dialog</property>
    <property name="skip_taskbar_hint">True</property>
    <property name="urgency_hint">True</property>
    <property name="gravity">center</property>
    <property name="program_name">Formulario</property>
    <property name="version">2.0</property>
    <property name="comments" translatable="yes">Formulario integrado com python3 e mysql</property>
    <property name="authors">Guilherme</property>
    <property name="artists">Guilherme</property>
    <property name="logo_icon_name">image-missing</property>
    <child internal-child="vbox">
      <object class="GtkBox" id="aboutdialog-vbox1">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox" id="aboutdialog-action_area1">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Cadastro user</property>
    <property name="resizable">False</property>
    <property name="window_position">center</property>
    <property name="gravity">center</property>
    <signal name="destroy" handler="Gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="menuitem2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Editar</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="delete">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_delete_activate" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem4">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Aj_uda</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="about">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_about_activate" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkGrid" id="grid2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="margin_top">36</property>
            <property name="row_spacing">14</property>
            <property name="column_homogeneous">True</property>
            <child>
              <object class="GtkEntry" id="text_name">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">48</property>
                <property name="input_purpose">name</property>
                <signal name="activate" handler="on_text_name_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="text_passwd">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">47</property>
                <property name="visibility">False</property>
                <property name="invisible_char">*</property>
                <property name="input_purpose">password</property>
                <signal name="activate" handler="on_text_passwd_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="text_numb">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">47</property>
                <property name="input_purpose">phone</property>
                <signal name="activate" handler="on_text_numb_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">2</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="nome">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Nome: </property>
                <property name="wrap">True</property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="passwd">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Senha: </property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="numb">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Telefone: </property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButtonBox" id="buttonbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <property name="layout_style">start</property>
            <child>
              <object class="GtkButton" id="submit">
                <property name="label" translatable="yes">Gravar</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="margin_top">30</property>
                <property name="margin_bottom">32</property>
                <signal name="clicked" handler="on_submit_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

I leave the field name blank and it returns me the print("n nula") .

    
asked by anonymous 31.07.2017 / 02:11

3 answers

4

It's working right. You need to decide whether you want to know if the variable is null or empty, are completely different concepts. The title asks one thing, the comment asks for another.

A null variable is as if there is no value, nothing.

An empty string variable has a value and its value is empty, which is unlike anything.

Think of a box. If it does not exist it is null, if it exists and has no things inside it is just empty.

So if you want to know if the variable is empty, and this can only be in specific types like string , check this:

if name == "":

A string is always enclosed in the code by a couple of quotation marks, so if it is empty it only has the quotation marks and nothing inside. What his code did was verify the nullity and it was never null, it was existing but empty. The "" is like a 0.

    
31.07.2017 / 02:37
3

To check if the variable is null, in addition to the information that has been passed above, you can do so too:

if name:
    [...]
    
31.07.2017 / 13:37
0

If string can do if "string" == "": For objects can be done if objeto == None:

    
31.07.2017 / 06:59