Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用输入到kivy GUI中的文本作为字符串在其他地方使用?_Python 3.x_Kivy - Fatal编程技术网

Python 3.x 如何使用输入到kivy GUI中的文本作为字符串在其他地方使用?

Python 3.x 如何使用输入到kivy GUI中的文本作为字符串在其他地方使用?,python-3.x,kivy,Python 3.x,Kivy,我已经在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.ui

我已经在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行,in_uuuinit__ TEXT=(谁、什么、在哪里、何时、如何) NameError:未定义名称“Who”

您必须在创建Textinput小部件时为其提供id,以便引用它们来提取文本。添加一个按钮,以便调用sendEmail方法。有关详细信息,请参考下面的示例

例子 main.py amapp.kv
#:kivy 1.10.0
:
方向:“垂直”
网格布局:
科尔斯:2
标签:
正文:“谁”
文本输入:
身份证:谁
多行:正确
标签:
文字:“什么”
文本输入:
身份证:什么
多行:正确
标签:
文本:“何处”
文本输入:
id:在哪里
多行:正确
标签:
文本:“何时”
文本输入:
id:什么时候
多行:正确
标签:
文本:“如何”
文本输入:
身份证:怎么办
多行:正确
按钮:
文本:“发送邮件”
发布时:root.send\u eMail()
输出

实际上,您与代码非常接近。我没有重写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.app导入应用程序
从kivy.uix.label导入标签
从kivy.uix.widget导入widget
从kivy.uix.gridlayout导入gridlayout
从kivy.uix.textinput导入textinput
从kivy.uix.button导入按钮#添加
电子邮件=“已添加”
密码=”#已添加
主类(网格布局):
定义初始(自我,**kwargs):
超级(主、自).\uuuuu初始(**kwargs)
self.cols=2
self.add_小部件(标签(text=“Who”))
self.Who=TextInput(多行=True)
self.add_小部件(self.Who)
self.add_小部件(标签(text=“What”))
self.What=TextInput(多行=True)
self.add_小部件(self.What)
self.add_小部件(标签(text=“Where”))
self.Where=TextInput(多行=True)
self.add_小部件(self.Where)
self.add_小部件(标签(text=“When”))
self.When=TextInput(多行=True)
self.add_小部件(self.When)
self.add_小部件(标签(text=“How”))
self.How=TextInput(多行=True)
self.add_小部件(self.How)
self.add_小部件(按钮(text=“Send”,on_press=self.sendmail))#已添加
#添加了可通过按键调用的函数:
def sendmail(self,*args):
FROM='me'
至=“”
主题=‘测试’
TEXT='\n'.join([self.Who.TEXT,self.What.TEXT,
self.Where.text、self.When.text、self.How.text])
服务器='smtp.gmail.com'
sendMail(FROM,[TO],SUBJECT,TEXT,SERVER)#注意TO参数
类AMAPP(应用程序):
def生成(自):
返回主管道()
def sendMail(发件人、收件人、主题、文本、服务器):
导入smtplib
“”“这是函数中的一些测试文档”“”
message=textwrap.dedent(“”)\
发件人:%s
发送至:%s
主题:%s
%
“”“”(FROM,,”.join(TO),SUBJECT,TEXT)#修复了dedent调用
#寄信
server=smtplib.SMTP(服务器)
server.starttls()
服务器登录(电子邮件、密码)
server.sendmail(发件人、收件人、邮件)
server.quit()
如果名称=“\uuuuu main\uuuuuuuu”:
AMAPP().run()

警告,如果您想将数据发送到kivy GUI,则需要更多的操作,因为kivy必须知道数据已更改。但这超出了这个问题的范围。

首先删除您在某个点(在sendmail函数结束和if!然后将sendmail函数放在主类中。然后在GUI中添加一个按钮来调用sendmail函数。(然后在此处更新代码以获取更多帮助)请注意您的textwrap.dedent呼叫!我在玩这段代码,发现如果字符串替换包含换行符,dedent将无法正常工作。您应该在应用%运算符之前删除格式字符串!(只需移动一个右括号)感谢您澄清这一点,尽管我在尝试发送邮件时仍然会遇到一个错误:AttributeError:“super”对象没有属性“\u getattr\u使用我提供的示例,我可以毫无问题地发送电子邮件。有关详细信息,请参考示例。
#: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()
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()