[英]How to pass a variable from one function to another function in python
[英]How to pass a variable from one function to another function
我正在制作基于 python 的 Email 广播,其中我创建了 email 之类的条目,通过,还有 csv 浏览器,它将浏览一个 Email_list_container 文件和一个提交按钮,它将调用发送邮件 function 来发送批量 8370828 问题 function是当浏览器用于从 csv 获取电子邮件时,它存储到一个变量,然后返回到 function,但是当我在发送邮件中调用此变量时,function 不允许我在那里使用它。 与附件 function 相同,也不会发送邮件 function。
我试过全球
newvar = browse()
并调用 new var 但这调用整个 function 再次弹出 new window 以打开另一个没有任何意义的文件。 帮帮我伙计们。
from tkinter import *
import tkinter.messagebox as msg
import smtplib as smtp
import csv
from itertools import chain
#browse function which stores value from csv file
def browse():
from itertools import chain
file_path=filedialog.askopenfilename(title="Open CSV file")
with open(file_path) as csvfile:
read = csv.reader(csvfile)
for row in read:
ini_list.append(row)
flatten_list = list(chain.from_iterable(ini_list))
rcvr_emails =list(flatten_list)
# print(rcvr_emails)
file_label = Label(window,text=file_path, border=0, bg='#BAE1E3',font="inter 10", fg="grey").place(x=330,y=230)
recemail = rcvr_emails
#what i want is submit function to grab a variable from browse function as email list
def submit():
try:
email = login_email.get()
pass_word = login_pass.get()
subject = email_subject.get()
body = email_body.get()
server = smtp.SMTP("smtp.gmail.com",587)
server.starttls()
server.ehlo()
server.login(email,pass_word)
massage = "subject:{}\n\n{}".format(subject,body)
server.sendmail(email,recemail,massage)
server.quit()
msg.showinfo("Status","Mails have been sent to the Targatted Email's List.\nThank You for using our services.")
except:
msg.showwarning("ERROR","SMTP API could not login the credentials,\nPlease check Email & Password then try again.")
只需从browse
器 function 返回recemail
,然后将其作为参数传递给submit
function:
def browse():
from itertools import chain
file_path=filedialog.askopenfilename(title="Open CSV file")
with open(file_path) as csvfile:
read = csv.reader(csvfile)
for row in read:
ini_list.append(row)
flatten_list = list(chain.from_iterable(ini_list))
rcvr_emails =list(flatten_list)
# print(rcvr_emails)
file_label = Label(window,text=file_path, border=0, bg='#BAE1E3',font="inter 10", fg="grey").place(x=330,y=230)
recemail = rcvr_emails
return recemail
def submit(email_list):
// your code
然后在你的主程序中:
received_email = browse()
submit(received_email)
或者在一行中:
submit(browse())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.