作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个程序,用户可以上传 .csv 文件,然后显示内容,然后操作该文件,然后将操作的文件作为电子邮件的附件发送。
我的问题是,python 没有记忆起 SortingCSV 和 UploadAction 函数中的“fname”。所以我无法对显示的 .csv 文件内容执行任何操作。
这是我收到的错误:
NameError: name 'fname' is not defined
def load_file(self):
#Upload a CSV file and display its contents
fname = askopenfilename(filetypes=(("CSV files", "*.csv"),
("HTML files", "*.html;*.htm"),
("All files", "*.*") ))
print(fname)
print('The file has been uploaded, contents are displayed below')
import csv
with open(fname) as f:
reader = csv.reader(f)
for row in reader:
print(" ".join(row))
def SortingCSV(self):
#Allows user to switch the contents of the file to the desired ings
print(fname)
print("Tkinter is easy to use!")
def UploadAction(self):
#Allows user to send the converted file to website
import smtplib
content = 'Here is your completed file'
attach = (fname.csv)
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('[insert email]','[insert password]')
mail.sendmail('[insert email]','[insert recipient]',content)
mail.close()
print("The file has been sent to your inbox")
if __name__ == "__main__":
MyFrame().mainloop()
最佳答案
将 global fname
添加为使用该变量的所有函数的第一行。这将使 fname
成为全局可见的变量。不过,全局变量是邪恶的。更好的解决方案是将所有 GUI 功能打包到一个类中。
关于Python 程序不记得变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146327/
在不使用 ANT 的情况下,有没有办法让 Eclipse 在我每次发布 Android 应用程序(我使用导出向导)时不提示我输入 keystore 密码(两次!)? p.s.-我的开发 PC 位于 a
我是一名优秀的程序员,十分优秀!