Kivy:如何在按钮on_press上调用某些东西

时间:2014-03-19 15:44:19

标签: android python kivy

我正在创建一个生成门票的应用。生成脚本已经完成。

按下按钮" Generera" ('生成'英文)它应该打印生成的票证(现在在终端中)。

一切都在运行但是当我按下Generate按钮时出现此错误:

  

AttributeError:StockholmTicket实例没有调用方法

我不知道如何在gen_btn印刷机上致电StockholmTicket并感谢任何帮助。

以下是代码:

from kivy.app import App

from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

from datetime import datetime, timedelta
import random


class Fejkbiljett(App):

    def build(self):
        gen_btn = Button(text='Generera',
                         size_hint=(.90, .10),
                         pos=(5, 5),
                         font_size=21)

        gen_btn.bind(on_press=StockholmTicket())

        l = BoxLayout()

        l.add_widget(gen_btn)

        return l


class StockholmTicket():

    def getMessage():
        # gNumbers
        gNumbers = random.randint(100, 999)
        gNumbers = str(gNumbers)

        gLetters = 'EAOXE'
        gLetters = gLetters * 2
        gLetters = ''.join(random.sample(gLetters, len(gLetters)))
        gLetters2 = 'EAOXA'
        gLetters2 = gLetters2 * 2
        gLetters2 = ''.join(random.sample(gLetters2, len(gLetters2)))
        gLetters3 = 'EAOXA'
        gLetters3 = gLetters3 * 2
        gLetters3 = ''.join(random.sample(gLetters3, len(gLetters3)))

        sCode = '123456789123456'
        sCode = ''.join(random.sample(sCode, len(sCode)))
        sCode = sCode[:6] + ' ' + sCode[6:]

        iPrice = '20'
        sPriceText = 'Red pris'
        sPricetype = 'R'
        mZones = 'A'
        mTime = datetime.now()
        mTime = mTime + timedelta(hours=1, minutes=15)
        mTime = str(mTime)
        mTime = mTime[11:16]
        mDate = str(datetime.now())
        mDate = mDate[:10]

        print((sPricetype + "-" + mZones + " " + mTime + " "
              + gNumbers + "\n\n"

              + gLetters + "\n"
              + gLetters2 + "\n"
              + gLetters3 + "\n"
              + "EEEEEEEEEE\n\n"

              + "SL biljett giltig till " + mTime + ", " + "\n" + mDate + "\n"
              + sPriceText + " " + iPrice + " kr ink 6% moms\n"

              + sCode + "\n"
              + "m.sl.se"))

    getMessage()

if __name__ == "__main__":
    Fejkbiljett().run()

4 个答案:

答案 0 :(得分:1)

不确定为什么(我猜它是这样定义的类),但是当调用不属于您绑定的同一个类的方法时,它不起作用。您可以在类中创建一个方法,并在另一个类中调用该方法。像这样:

from kivy.app import App

from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout


class Fejkbiljett(App):

    def build(self):
        self.gen_btn = Button(text='Generera',
                         size_hint=(.90, .10),
                         pos=(5, 5),
                         font_size=21)
        self.gen_btn.bind(on_press=self.getMessageFejkbiljett)
        l = BoxLayout()
        l.add_widget(self.gen_btn)
        return l

    def getMessageFejkbiljett(self, *args):
        print "1. this is called on the method getMessageFejkbiljett of Fejkbiljett"
        st = StockholmTicket()
        st.getMessage(self)


class StockholmTicket():

    def getMessage(self, source):
        source.gen_btn.text = "the event was called"
        print "2. this is called on the method getMessage of StockholmTicket"

if __name__ == "__main__":
    Fejkbiljett().run()

答案 1 :(得分:0)

" StockholmTicket()"这是一个不可调用的类(除非你创建它,但这是另一个讨论)。尝试使用此代码,它将创建一个实例,用于保存要调用的函数。

    gen_btn.bind(on_press=StockholmTicket().getMessage)

而不是

    gen_btn.bind(on_press=StockholmTicket())

您还需要重新定义" getMessage"功能因为它还没有接受参数,但它需要。尝试

class StockholmTicket():
    def getMessage(self, * args):

希望有帮助吗?干杯

答案 2 :(得分:0)

但这有效吗?

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


class Fejkbiljett(App):

    def build(self):
        gen_btn = Button(text='Generera',
                         size_hint=(.90, .10),
                         pos=(5, 5),
                         font_size=21)
        gen_btn.bind(on_press=StockholmTicket.getMessage)
        l = BoxLayout()
        l.add_widget(gen_btn)
        return l


class StockholmTicket(object):

    def getMessage(self):
        print "2. this is called on the method getMessage of StockholmTicket"

if __name__ == "__main__":
    Fejkbiljett().run()

答案 3 :(得分:0)

您只需键入gen_btn.on_press =您的函数