通过函数传输变量



另一个深夜项目。我试图制作一个简单的登录屏幕(稍后将使用凭据(。 现在我想将用户名和密码存储为"登录屏幕"中的变量用户名和密码。 由于某种原因,它不起作用。我尝试了很多东西,比如"全球","返回"等。

有没有办法在不大幅更改代码的情况下将输入存储在这些变量中?我稍后会修改代码,需要理解和解释太多人。

编辑: 在下拉菜单中有一个名为"-------"的选项。我从来没有把它放在那里,但它不断弹出。它总是弹出有什么原因吗?我该如何删除它?


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>nnnThis 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.nnSince 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.nnWe 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.nnnMet vriendelijke groet,nKind Regard,nn<SENDER> - <FUNCTION>nAcademy CommitteenEBF Groningenn')


# *** 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()

写入函数中的全局变量的工作方式如下:

a = None
def foo():
global a
a = 42
a = 3
foo()
print(a)

输出:

42

问题的根源在于你在任何地方都使用局部变量,但期望它们是全局的。

如果希望变量在创建它的作用域之外可访问,则必须将其定义为全局1

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

1严格来说,这不是真的 - 全局变量可以在不声明全局的情况下读取,但它们不能被修改。但是,由于您的目的是使用全局变量,因此即使不需要将它们声明为全局变量也会使您的代码更加清晰。

有关更多信息,请参阅 Python 官方文档中的 Python 中局部变量和全局变量的规则是什么?

最新更新