Kiran_t
Kiran_t

Reputation: 45

Code Crashes when button triggers a function in kivy

I want to trigger an email when a button is pressed. The email() function sends an email via smtp mail server. On clicking the button, an email is sent but the app crashes immediately with an error

TypeError: email() takes 1 positional argument but 2 were given

How can I improve the code so that the app doesn't crash after pressing the button?

here's the code

# import kivy module
import kivy
import smtplib
kivy.require("1.11.1")

from kivy.app import App
from kivy.uix.button import Button


# class in which we are creating the button
class ButtonApp(App):
    def build(self):
        # use a (r, g, b, a) tuple
        btn = Button(text="Send Email !",
                     font_size="20sp",
                     background_color=(1, 1, 1, 1),
                     color=(1, 1, 1, 1),
                     size=(32, 32),
                     size_hint=(.2, .2),
                     # on_press = root.email,
                     pos=(300, 250))
        btn.bind(on_press = self.email)

        return btn

    def email(self):

        s = smtplib.SMTP('smtp.gmail.com', 587)
        s.starttls()

        s.login("[email protected]", "hjqqKJSN98")

        message = "\n Hello "
        s.sendmail("[email protected]", "[email protected]", message)

        # terminating the session
        s.quit()

        return

root = ButtonApp()
root.run()

Upvotes: 1

Views: 202

Answers (1)

Lothric
Lothric

Reputation: 1318

When you call function from Button it sends an object to that function, that's the second argument error is talking about. So you can put it in *args and forget about it:

def email(self, *args):

Upvotes: 1

Related Questions