Python: Unsupported operand type(s) for &: 'NoneType' and 'NoneType' whilst using lambda
Question: I'm currently using lambda to make a tkinter button do two things after each
other:

    
    
    def classManip():    
        cManip = tk.Toplevel()
        cManip.title('Class Manipulator')
        cManip.minsize(400,100)
        cManip.maxsize(400,100)
    
        databaseEntry = ttk.Entry(cManip, width = 25)
        databaseEntry.place(relx = .5, rely = .375, anchor = "c")
    
        entrySubmit = ttk.Button(cManip, text = "Enter", width = 20, command = lambda : connectDatabase(databaseEntry.get()) & cManip.destroy())
        entrySubmit.place(relx = .5, rely = .625, anchor="c")
        cManip.mainloop()
    

this is the function from my main code; I have a button on my main tkinter
window that has the command to run this function. The connect databaseEntry
function is form another file named databaseManip in a folder named scripts
which I import using:

    
    
    from scripts.databaseManip import connectDatabase
    

and the code within that file is:

    
    
    import sqlite3, sys, os
    import tkinter as tk
    from win32api import GetSystemMetrics
    
    #connects or creates database
    def connectDatabase(name):
        name = str(name)
        screenWidth =  GetSystemMetrics (0)
        screenHeight =  GetSystemMetrics (1)
    
        if os.path.isfile("classDbFiles/" + name + ".db"):
            conn = sqlite3.connect("classDbFiles/" + name + ".db")
            tk.messagebox.showinfo(message="Connected to %s successfully" % (str(name + ".db")), title = "File Found")
        else:   
            conn = sqlite3.connect("classDbFiles/" + name + ".db")
            tk.messagebox.showinfo(message = "The database file %s was created and opened successfully" % (str(name + ".db")), title = "Success")
    

What I want for the program to do is to run the database function creating or
opening the .db file and then to close the tkinter window afterwards and
funnily enough it does actually work but it returns the error:

    
    
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Program Files\Python\lib\tkinter\__init__.py", line 1475, in __call__
        return self.func(*args)
      File "C:\Users\Patrick\Dropbox\Computing Project\mainApp.py", line 52, in <lambda>
        entrySubmit = ttk.Button(cManip, text = "Enter", width = 20, command = lambda : connectDatabase(databaseEntry.get()) & cManip.destroy())
    TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'
    

I have searched for an answer but nothing seems to be for lambda so I'm lost.
What's wrng with the code?


Answer: `&` doesn't do what you think it does. It finds the the bitwise `and` of two
objects. Instead, try defining the function:

    
    
     def function():
         connectDatabase(databaseEntry.get())
         cManip.destroy()
    
     entrySubmit = ttk.Button(cManip, text="Enter", width=20, command=function)
    

You could also replace the `&` with an `and`, which would work if the first
function call does always only return `None` (or another false-valued value),
**but this is a nasty, hacky, clever, and unreadable way of doing what you
want** and will likely result in the confusion of you, and anyone reading your
code.

