Reputation: 13
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()
Upvotes: 0
Views: 54
Reputation: 386342
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.
Upvotes: 0
Reputation: 470
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
Upvotes: 1