Equality Strings tkinter Python

Hello

A beginner tkinter Python question, because I really don’t understand… When I run the code below, it returns the “else” side of the if-else function, if I enter two equal emailaddresses… Can you guys tell me what I did wrong ?

from tkinter import *
from tkinter import messagebox

def callback():
print(email.get())
return True
def Equal():
if email == email2:
print(“Your registration is completed”)
else:
print(“You entered two different emailaddresses”)

root = Tk()
root.title(‘Login’)
root.geometry(‘400x200’)
email = StringVar()
email2 = StringVar()
password = StringVar()
intro = Label(root, text="Do you already have an account? “).grid(column=0, row=0)
empty = Label(root, text=” ").grid(column=1,row=0)
L = Label(root, text="Emailaddress: ").grid(column=0, row=1)
E = Entry(root, textvariable=email, validate=“focusout”, validatecommand=callback).grid(column=1, row=1)
M = Label(root, text="Emailaddress: ").grid(column=0, row=2)
F = Entry(root, textvariable=email2, validate=“focusout”, validatecommand=callback).grid(column=1, row=2)
P_label = Label(root, text=“Password: “).grid(column=0, row=3)
P_Entry = Entry(root, show=”*”, textvariable=password).grid(column=1, row=3)

button1 = Button(root, text=“Login”, command=Equal).grid(column=2, row=4)
root.mainloop()

In the original code the tab spaces are correct, I see this isn’t in the published post here…

Thank you

Mattaqua

Can you try formatting your code by nesting it in triple backticks? You can use the tools at the top as well. I’d like to help but I’ll have to edit this code a lot to run it. Thanks!

Screenshot 2023-09-03 082302

1 Like
from tkinter import *
from tkinter import messagebox

def callback():
print(email.get())
return True
def Equal():
if email == email2:
print(“Your registration is completed”)
else:
print(“You entered two different emailaddresses”)

root = Tk()
root.title(‘Login’)
root.geometry(‘400x200’)
email = StringVar()
email2 = StringVar()
password = StringVar()
intro = Label(root, text="Do you already have an account? “).grid(column=0, row=0)
empty = Label(root, text=” ").grid(column=1,row=0)
L = Label(root, text="Emailaddress: ").grid(column=0, row=1)
E = Entry(root, textvariable=email, validate=“focusout”, validatecommand=callback).grid(column=1, row=1)
M = Label(root, text="Emailaddress: ").grid(column=0, row=2)
F = Entry(root, textvariable=email2, validate=“focusout”, validatecommand=callback).grid(column=1, row=2)
P_label = Label(root, text=“Password: “).grid(column=0, row=3)
P_Entry = Entry(root, show=”*”, textvariable=password).grid(column=1, row=3)

button1 = Button(root, text=“Login”, command=Equal).grid(column=2, row=4)
root.mainloop()

Is this what you mean?

1 Like

Did you copy/paste this from your previous post or from your editor?

The indentation and quotes still got mangled

def draw_cat_plot():
  # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
  df_cat = df, id_vars=['cardio'], value_vars=['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight']

You can see here the quotes and indentation are intact

Ooh ok, got it, sorry :upside_down_face:

from tkinter import *
from tkinter import messagebox

def callback():
    print(email.get())
    return True

def Equal():
    if email == email2:
        print("Your registration is complete")
    else:
        print("Both e-mail addresses should be equal")
    
root = Tk()
root.title('Login')
root.geometry('400x200')

email = StringVar()
email2 = StringVar()
password = StringVar()

intro = Label(root, text="Please register to log in: ").grid(column=0, row=0)
L = Label(root, text="E-mail address: ").grid(column=0, row=1)
E = Entry(root, textvariable=email, validate="focusout", validatecommand=callback).grid(column=1, row=1)
M = Label(root, text="Type in your e-mail address again: ").grid(column=0, row=2)
F = Entry(root, textvariable=email2, validate="focusout", validatecommand=callback).grid(column=1, row=2)

P_label = Label(root, text="Password: ").grid(column=0, row=3)
P_Entry = Entry(root, show="*", textvariable=password).grid(column=1, row=3)
 
button1 = Button(root, text="Register", command=Equal).grid(column=2, row=4)
root.mainloop()

Thanks for willing to help by the way, much appreciated!

1 Like

I added

print(email, email2)

before the IF. Output:

test
test
PY_VAR0 PY_VAR1
Both e-mail addresses should be equal

So, you can’t compare StringVar() this way. PY_VAR0 != PY_VAR1 You need to use a method on the StringVar(). You need to use email.get(). This will return the email and you can compare.

https://www.pythontutorial.net/tkinter/tkinter-stringvar/

Thanks, so I changed my callback function and added button1.pack()

from tkinter import *
from tkinter import messagebox

def callback():
    print(email.get())
    print(email2.get())
    if email.get() == email2.get():
        print("Your registration is complete")
    else:
        print("Both e-mail addresses should be equal")
    
    
root = Tk()
root.title('Login')
root.geometry('400x200')

email = StringVar()
email2 = StringVar()
password = StringVar()

intro = Label(root, text="Please register to log in: ").grid(column=0, row=0)

L = Label(root, text="E-mail address: ").grid(column=0, row=1)
E = Entry(root, textvariable=email, validate="focusout", validatecommand=callback).grid(column=1, row=1)
M = Label(root, text="Type in your e-mail address again: ").grid(column=0, row=2)
F = Entry(root, textvariable=email2, validate="focusout", validatecommand=callback).grid(column=1, row=2)

P_label = Label(root, text="Password: ").grid(column=0, row=3)

P_Entry = Entry(root, show="*", textvariable=password).grid(column=1, row=3)

button1 = Button(root, text="Register", command=callback).grid(column=2, row=4)
button1.pack(padx=2,past=2)

root.mainloop()

So, probably it will be the most obvious answer ever, but how do I rephrase the if function so it uses the values of email.get() and email2.get()?

I tried E == F, print(email.get()) == print(email2.get(), Entry(email.get()) == Entry(email2.get().,.

Could you look into it, please?

The code has multiple errors. It’s not preferred to combine pack and grid. My preference is the grid manager as I feel it gives more control. Here is an example of your code re-written. Hope it helps.

import tkinter as tk
from tkinter import messagebox


def callback():
    ''' callback function for comparing email '''
    if email1.get() == email2.get():
        messagebox.showinfo('Message', 'Registration complete')
    else:
        messagebox.showerror('Error', 'Emails to don\'t match')

'''
Starting tkinter layout. To get variables from the entry fields, you can't
have the grid or pack attached. Must be a seperate line
'''
root = tk.Tk()
root['padx'] = 10
label = tk.Label(root, text='Please register to login', bg='#555555', fg='white')
label['font'] = 'tk.HEADER 25 bold'
label.grid(column=0, columnspan=2, row=0, sticky='new', pady=(0, 10))

label = tk.Label(root, text='E-mail:', anchor='w')
label['font'] = 'tk.LIST 14 bold'
label.grid(column=0, row=1, sticky='new', padx=2, pady=5)

email1 = tk.Entry(root)
email1['font'] = 'tk.MENU 14 normal'
email1.grid(column=1, row=1, sticky='new', padx=2, pady=5)

label = tk.Label(root, text='E-mail:', anchor='w')
label['font'] = 'tk.LIST 14 bold'
label.grid(column=0, row=2, sticky='new', padx=2, pady=5)

email2 = tk.Entry(root)
email2['font'] = 'tk.MENU 14 normal'
email2.grid(column=1, row=2, sticky='new', padx=2, pady=5)

label = tk.Label(root, text='Password:', anchor='w')
label['font'] = 'tk.LIST 14 bold'
label.grid(column=0, row=3, sticky='new', padx=2, pady=5)

passwd = tk.Entry(root)
passwd['font'] = 'tk.MENU 14 normal'
passwd.grid(column=1, row=3, sticky='new', padx=2, pady=5)

button = tk.Button(root, text='Submit')
button['command'] = callback
button.grid(column=0, row=4, pady=(2,10))

root.mainloop()

Thank you menator01, for your reply. Your feedback helped me too.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.