Paul
Paul

Reputation: 73

How to define a variable to link my google sheets in Tkinter?

I'm learning Tkinter and i worked in a login interface, i have two entries for my username and password*, also one button for my login, I've defined a function for my entries and login button with sqlite database and it works good locally, but I want to store my usernames and passwords in a server so i choose to use Google sheets API in order to create a sheet and store there my usernames and passwords, I triede to define a funtion to link my entries and login button but it does'nt work. any advise thanks in advance. have a good day.

#THIS IS MY FUNCTION PART

def logear():

    scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
             "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
    creds = ServiceAccountCredentials.from_json_keyfile_name("testsheetsxxxxx.json", scope)

    client = gspread.authorize(creds)

    sheet = client.open("testing").sheet1    ########'testisng is my google sheet and 'sheet1' is what im 
                                                      using to store usernames and passwords###########

    user = entry_usuario.get()              ####entry_usuario is my username entry########
    contra = entry_contrasena.get()         ####entry_contrasena us my password entry######

    c = #####stucked here####

    if ####stucked here####:
        messagebox.showinfo(title='login correcto', message='usuario y contraseña correctos')
    else:
        messagebox.showerror(tittle=None, message='Contraseña Incorrecta') 

Upvotes: 0

Views: 360

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

Here is what works for me:

def logear():
    # Rest of required code 
    user = entry_usuario.get() 
    contra = entry_contrasena.get()
    for i in c: # Loop through the list
        if int(user) == i['contraseña'] and contra == i['password']:  # Check if password and username exists in the dict
            messagebox.showinfo(title='login correcto', message='usuario y contraseña correctos')
            break
    else:
        messagebox.showerror(tittle=None, message='Detalles incorrectos')

Here the only problem I see is, username always have to be an int, so I recommend changing it to str so that its more efficient.

I dont know why you switched from SQLite to Google Sheets both will be insecure as you are storing passwords as plaintext, I recommend hashing it pre-hand and storing the hashed values. And as far as hosting of a database is concerned there are other online servers which allow MySQL database to be hosted, take a look here

Upvotes: 1

Related Questions