0

Another late night project. I was trying to make a simple login screen (the credentials will be used later). Now I want to store the username and password as the variables USERNAME and PASSWORD in the "login screen". For some reason it doesnt work. I tried so many things, like 'global', 'return' etc.

Is there a way to store the inputs in those variables without changing the code dramasticly? I will be modifying the code later on quite a bit and need to understand and explain this too numerous people.

EDIT: in the dropdown menu there is a option called "-------". I never put it there, but it keeps popping up. Is there a reason why it always pops up? And how can I delete it?



import os
import smtplib
from tkinter import *
import tkinter.messagebox

#USERNAME
#PASSWORD

root = Tk()
root.geometry("500x300")

root.title("E-mail-Sending program EBF")

# *** FUNCTIONS ***

def setLoginCredentials():
    USERNAME = entryLoginUsername.get()
    PASSWORD = entryLoginPassword.get()
    print(USERNAME)
    print(PASSWORD)

def loginCredentials(event):
    #Create another screen
    loginScreen = Toplevel(root)
    loginScreen.title("login-screen")
    loginScreen.geometry("300x300")

    #LABELS LOGIN SCREEN
    labelLoginUsername = Label(loginScreen, text="E-mail:")
    labelLoginUsername.grid(row=0,column=0, sticky=E)
    labelLoginPassword = Label(loginScreen, text="Password:")
    labelLoginPassword.grid(row=1,column=0, sticky=E)

    #ENTRIES LOGIN SCREEN
    entryLoginUsername = Entry(loginScreen)
    entryLoginUsername.grid(row=0,column=1)
    entryLoginPassword = Entry(loginScreen)
    entryLoginPassword.grid(row=1,column=1)

    #LOGIN BUTTON
    loginButton1 = Button(loginScreen,text="Login",command=setLoginCredentials)
    # loginButton1.bind("<Button-1>", setLoginCredentials)
    loginButton1.grid(row=2,column=1, sticky=W)


def previewEmail():
    tkinter.messagebox.showinfo('Email preview','Dear professor <NAME>\n\n\nThis email is on behalf of the Academy Committee of EBF Groningen, which is responsible for the booksale of the Economics and Business Faculty of the University of Groningen.\n\nSince you are the coordinator of the course <NAME>, we were wondering if any alterations were made regarding the compulsory literature that has not been listed on the latest version of Ocasys yet.\n\nWe would like the confirmation if the course literature on Ocasys is up to date or if any alterations are needed. This way we are able to contact the suppliers of these books and ensure that inconveniences, due to providing the wrong books, can be avoided.\n\n\nMet vriendelijke groet,\nKind Regard,\n\n<SENDER> - <FUNCTION>\nAcademy Committee\nEBF Groningen\n')





# *** LABELS HOMESCREEN ***
labelSender = Label(root, text="Sender:")
labelSender.grid(row=0,column=0, sticky=E)

labelFunction = Label(root, text="Function:")
labelFunction.grid(row=1,column=0, sticky=E)

labelEmail = Label(root, text="Email:")
labelEmail.grid(row=2,column=0, sticky=E)

labelProfessor = Label(root, text="Professor:")
labelProfessor.grid(row=3,column=0, sticky=E)

labelCourse = Label(root, text="Course:")
labelCourse.grid(row=4,column=0, sticky=E)



# *** ENTRIES  MAINSCREEN***
entrySender = Entry(root)
entrySender.grid(row=0,column=2, columnspan=2)

entryFunction = Entry(root)
entryFunction.grid(row=1,column=2, columnspan=2)

entryEmail = Entry(root)
entryEmail.grid(row=2,column=2, columnspan=2)

entryProfessor = Entry(root)
entryProfessor.grid(row=3,column=2, columnspan=2)

entryCourse = Entry(root)
entryCourse.grid(row=4,column=2, columnspan=2)

# *** ENTRIES LOGINSCREEN ***


# *** BUTTONS ***
loginButton = Button(root, text="Login")
loginButton.bind("<Button-1>", loginCredentials)
loginButton.grid(row=6,column=0, sticky=E)


# *** MAIN MENU ***

menu= Menu(root)
root.config(menu=menu)

subMenu = Menu(root)
menu.add_cascade(label="Menu", menu=subMenu)
subMenu.add_command(label="Preview", command=previewEmail)



root.mainloop()

2 Answers 2

1

writing to a global variable inside a function works like this:

a = None

def foo():
    global a
    a = 42

a = 3
foo()
print(a)

Output:

42
Sign up to request clarification or add additional context in comments.

5 Comments

yes I know, but for some reason it doesnt work. I keep on getting: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\allar\anaconda3\lib\tkinter_init_.py", line 1705, in call return self.func(*args) File "C:\Users\V2.py", line 27, in setLoginCredentials USERNAME = entryLoginUsername.get() NameError: name 'entryLoginUsername' is not defined
yes, because entryLoginUsername is defined neither in 'setLoginCredentials()` nor globally. Its defined in loginCredentials.
im going to be honoust, I dont know what you really mean. None of the entries are defined. How should I change this? If it try to define it, I get:Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\allar\anaconda3\lib\tkinter_init_.py", line 1705, in call return self.func(*args) File "C:\Users\allar\Desktop\Opruimmapje\Python\Boekenprogramma V2.py", line 32, in setLoginCredentials USERNAME = entryLoginUsername.get() AttributeError: 'str' object has no attribute 'get'
I strongly recommend you to go through a python tutorial. I cannot help you, if you don't know what a definition is.
I do know what a definition is and I tried a lot of things. For some reason it just doesnt work
0

The root of the problem is that you're using local variables everywhere but expecting them to be global.

If you want a variable to be accessible outside of the scope it was created in, you must define it as global1

def setLoginCredentials():
    global USERNAME
    global PASSWORD
    global entryLoginUsername
    global entryLoginPassword
    ...

def loginCredentials(event):
    global entryLoginUsername
    global entryLoginPassword
    ...

1 Strictly speaking this isn't true - global variables can be read without being declared global, but they can't be modified. However, since your intent is to use global variables, declaring them as global even when you don't need to will clarity to your code.

For more information see What are the rules for local and global variables in Python? in the official python documentation.

2 Comments

yes, that was my first approach, to just make everything global. But I still keep getting that entryLoginUsername.get() is not defined. All the other x.get() do work, except that one. Isnt entryLoginUsername declared in nthe function loginCredentials?
@AllardvanGasteren: your question doesn't mention any problem with a function not being defined. This answers the question that you asked. However, if you're getting that error, that either means it's truly not defined (ie: the code that defines it hasn't run yet) or you haven't defined it as global, or there's a typo in your code. The bottom line is that you must define something before you use it, and if it's defined in one place and used in another, it must be declared as global if you aren't using classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.