我在kivy中创建了一个非常简单的GUI,并尝试使用它向特定用户发送电子邮件,如何使用输入GUI的文本,因为我不熟悉GUI。 这是我目前的代码:
import textwrap
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class Main(GridLayout):
def __init__(self,**kwargs):
super(Main, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text = "Who"))
self.Who = TextInput(multiline = True)
self.add_widget(self.Who)
self.add_widget(Label(text = "What"))
self.What = TextInput(multiline = True)
self.add_widget(self.What)
self.add_widget(Label(text = "Where"))
self.Where = TextInput(multiline = True)
self.add_widget(self.Where)
self.add_widget(Label(text = "When"))
self.When = TextInput(multiline = True)
self.add_widget(self.When)
self.add_widget(Label(text = "How"))
self.How = TextInput(multiline = True)
self.add_widget(self.How)
class AMAPP(App):
def build(self):
return Main()
def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
import smtplib
"""this is some test documentation in the function"""
message = textwrap.dedent("""\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT))
# Send the mail
server = smtplib.SMTP(SERVER)
server.starttls()
server.login('E-mail', 'Password')
server.sendmail(FROM, TO, message)
server.quit()
FROM = 'me'
TO = 'you'
SUBJECT = 'test'
TEXT = (Who, What, Where, When, How)
SERVER = 'smtp.gmail.com'
sendMail(FROM,TO,SUBJECT,TEXT,SERVER)
if __name__ == "__main__":
AMAPP().run()
每当我运行此操作时,我都会遇到同样的错误:
第66行,在__init__ TEXT =(谁,什么,何地,何时,如何) NameError:名称'Who'未定义
答案 0 :(得分:0)
您必须在创建文本输入小部件时将 id 提供给它们,以便您可以引用它们来提取文本。添加一个按钮,以便调用sendEmail方法。有关详细信息,请参阅以下示例。
import textwrap
import time
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
FROM = 'me'
TO = 'you'
SUBJECT = 'test'
TEXT = ""
SERVER = 'smtp.gmail.com'
def sendMail(FROM, TO, SUBJECT, TEXT, SERVER):
import smtplib
"""this is some test documentation in the function"""
message = textwrap.dedent("""\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT))
# Send the mail
server = smtplib.SMTP(SERVER, 587)
server.starttls()
server.login('E-mail', 'Password')
server.sendmail(FROM, TO, message)
server.quit()
class Main(BoxLayout):
def send_eMail(self):
global TEXT
print(self.ids.Who.text)
print(self.ids.What.text)
print(self.ids.Where.text)
print(self.ids.When.text)
print(self.ids.How.text)
TEXT = (self.ids.Who.text, self.ids.What.text,
self.ids.Where.text, self.ids.When.text, self.ids.How.text)
sendMail(FROM, TO, SUBJECT, TEXT, SERVER)
class AMAPP(App):
def build(self):
return Main()
if __name__ == "__main__":
AMAPP().run()
#:kivy 1.10.0
<Main>:
orientation: "vertical"
GridLayout:
cols: 2
Label:
text: "Who"
TextInput:
id: Who
multiline: True
Label:
text: "What"
TextInput:
id: What
multiline: True
Label:
text: "Where"
TextInput:
id: Where
multiline: True
Label:
text: "When"
TextInput:
id: When
multiline: True
Label:
text: "How"
TextInput:
id: How
multiline: True
Button:
text: "Send Mail"
on_release: root.send_eMail()
答案 1 :(得分:0)
你实际上与你的代码非常接近。我没有重写kv代码(这可能是更好的做法),我只是添加了按钮,修复了你的dedent调用,添加了self.Field.text引用,它运行正常。这是编辑后的代码。
import textwrap
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button # ADDED
EMAIL="<senders email address>" # ADDED
PASSWORD="<senders password>" # ADDED
class Main(GridLayout):
def __init__(self,**kwargs):
super(Main, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text = "Who"))
self.Who = TextInput(multiline = True)
self.add_widget(self.Who)
self.add_widget(Label(text = "What"))
self.What = TextInput(multiline = True)
self.add_widget(self.What)
self.add_widget(Label(text = "Where"))
self.Where = TextInput(multiline = True)
self.add_widget(self.Where)
self.add_widget(Label(text = "When"))
self.When = TextInput(multiline = True)
self.add_widget(self.When)
self.add_widget(Label(text = "How"))
self.How = TextInput(multiline = True)
self.add_widget(self.How)
self.add_widget(Button(text="Send",on_press=self.sendmail)) # ADDED
# ADDED function callable by the button press:
def sendmail(self,*args):
FROM = 'me'
TO = '<receivers email address>'
SUBJECT = 'test'
TEXT = '\n'.join([self.Who.text, self.What.text,
self.Where.text, self.When.text, self.How.text])
SERVER = 'smtp.gmail.com'
sendMail(FROM,[TO],SUBJECT,TEXT,SERVER) # watch out for the TO argument
class AMAPP(App):
def build(self):
return Main()
def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
import smtplib
"""this is some test documentation in the function"""
message = textwrap.dedent("""\
From: %s
To: %s
Subject: %s
%s
""") % (FROM, ", ".join(TO), SUBJECT, TEXT) # FIXED the dedent call
# Send the mail
server = smtplib.SMTP(SERVER)
server.starttls()
server.login(EMAIL,PASSWORD)
server.sendmail(FROM, TO, message)
server.quit()
if __name__ == "__main__":
AMAPP().run()
警告,如果你想将数据发送到kivy GUI,它会更复杂,因为kivy必须知道数据已经改变。但这超出了这个问题的范围。