简体   繁体   中英

Python 2.7/Kivy - Run a script clicking on a button

I am using Kivy for Python to create my first interface for a script. The interface works but I have not understood how to run the script inside the def sender(self) clicking on the button "Go!" that I have created.

EDIT1: added in bold the solution offered by Qback.. the code is not running at the moment (indentation error on self.button.bind inside the OutlookSend class)

Any suggestion? Below my code

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
import win32com.client as win32

class OutlookSend(GridLayout):

    def __init__(self, **kwargs):
        super(OutlookSend, self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label(text='Insert the date (format: DD-MM-YYYY, 00:00 AM)'))
        self.date = TextInput(multiline=False)
        self.add_widget(self.date)
        self.button = Button(text='Go!')
        self.button.bind(on_release=self.sender)
        self.add_widget(self.button)

class OutlookSender(App):

    def build(self):
        return OutlookSend()

###HERE STARTS THE SCRIPT###

def sender(self):

    outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")

    outbox = outlook.GetDefaultFolder(5) 

    messages = outbox.Items.restrict("[SentOn] > '11/06/2017 9:00 AM'")

    for message in messages:
       # print message - use this to verify if the restrict works before launching the script
       NewMsg = message.Forward()
       NewMsg.Body = message.Body
       NewMsg.Subject = message.Subject
       NewMsg.To = "mail@mail.com"
       NewMsg.Send()

###HERE THE SCRIPT ENDS###

if __name__ == '__main__':
    OutlookSender().run()

after that:

self.button = Button(text='Go!')

add that:

self.button.bind(on_release=sender)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM