'Function Object Has No Attribute Text: Kivy and Smtplib
How is everyone doing?
I'm pretty pretty new to Kivy and I'm trying to turn this script I made into a Kivy app
import smtplib
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
username = input("What's your gmail username? ")
password = input("\nWhat's your gmail password? ")
gmail = input("What's your email? ex: [email protected]: ")
message = input("\nWhat message do you want to send?")
to = [
'[email protected]', '[email protected]', '[email protected]',
'[email protected]', '[email protected]', '[email protected]', '[email protected]',
'[email protected]', '[email protected]', '[email protected]', '[email protected]'
'[email protected]', '[email protected]'
]
server.login(username, password)
server.sendmail(gmail, to, message)
print("Message sent :)")
server.quit()
And it's interesting because the code here works and does exactly what I want it to do but when I try to turn it into a kivy app using kinda the same formula, I keep getting errors. I know i'm doing something wrong (thus the errors) but I can't put my finger on it.
This is my Kivy code
import smtplib
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
import ssl
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
to = ['[email protected]']
class SayHello(App):
def build(self):
self.window = GridLayout()
self.window.cols = 1
self.window.size_hint = (0.6, 0.7)
self.window.pos_hint = {"center_x": 0.5, "center_y": 0.5}
self.window.add_widget(Image(source="SosTok\sostok.png",
width= 55,
size_hint_y = 2.8,
allow_stretch = True,
keep_ratio = False
))
self.username = TextInput(
multiline=False,
size_hint = (.2, None),
height = (30),
text = ("Enter Gmail username")
)
self.window.add_widget(self.username)
self.password = TextInput(
multiline=False,
size_hint = (.2, None),
height = (30),
text = ("Enter Gmail password"))
self.window.add_widget(self.password)
self.email = TextInput(
multiline=False,
size_hint = (.2, None),
height = (30),
text = ("Enter Email ex: [email protected]"))
self.window.add_widget(self.email)
self.message = TextInput(
multiline=True,
size_hint = (1, 1.5),
)
self.window.add_widget(self.message)
self.button = Button(
text="Send Message",
size_hint = (1, 0.5),
bold = True,
background_color = '#EB1160',
background_normal = ""
)
self.button.bind(on_press=self.callback)
self.window.add_widget(self.button)
return self.window
def callback(self,instance):
self.login.text = server.login.text(self.username,
self.password)
self.sendmail.text = server.sendmail.text(
self.email,
to,
self.message
)
self.messagesent.text = "Message Sent"
server.quit()
if __name__ == "__main__":
SayHello().run()
This is the error I'm getting
Traceback (most recent call last):
File "c:\Users\Strai\OneDrive\Documents\C Projects\SosTok\SosTok.py", line 86, in <module>
SayHello().run()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\behaviors\button.py", line 151,
in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1172, in kivy._event.EventObservers._dispatch
File "c:\Users\Strai\OneDrive\Documents\C Projects\SosTok\SosTok.py", line 72, in callback
self.login.text = server.login.text(self.username,
AttributeError: 'function' object has no attribute 'text'
PS C:\Users\Strai\OneDrive\Documents\C Projects>
I took bits and pieces from some tutorials, my already working script and tried to make that and I feel like I'm close, but something is wrong. The last error I got before this was
Traceback (most recent call last):
File "c:\Users\Strai\OneDrive\Documents\C Projects\SosTok\SosTok.py", line 86, in <module>
SayHello().run()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\behaviors\button.py", line 151,
in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1172, in kivy._event.EventObservers._dispatch
File "c:\Users\Strai\OneDrive\Documents\C Projects\SosTok\SosTok.py", line 72, in callback
self.login = server.login(self.username,
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 734, in login
(code, resp) = self.auth(
File "C:\Users\Strai\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 636, in auth
response = encode_base64(initial_response.encode('ascii'), eol='')
AttributeError: 'TextInput' object has no attribute 'encode'
PS C:\Users\Strai\OneDrive\Documents\C Projects>
Any help would be apricated, Kivy is a different beast.
Solution 1:[1]
I believe the problem is that you are passing TextInput widgets to the server.login() method in the code:
self.login = server.login(self.username,
self.password)
You should be passing text strings. Probably like this:
self.login = server.login(self.username.text, self.password.text)
Where self.username.text is the text that has been entered into the self.username TextInput widget. Similar for the self.password widget.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Anderson |
