Python 2.7 / Kivy - 单击按钮运行脚本

时间:2017-06-11 11:29:40

标签: python user-interface kivy

我正在使用Kivy for Python来创建脚本的第一个界面。 界面有效,但我还没有理解如何在def sender(self)点击“Go!”按钮内运行脚本。我创造了。

EDIT1:以粗体添加了Qback提供的解决方案..代码目前没有运行(self.button.bind类内OutlookSend上的缩进错误)

有什么建议吗?在我的代码下面

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()

1 个答案:

答案 0 :(得分:1)

之后:

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

补充一点:

self.button.bind(on_release=sender)