Kivy:怪异的按钮on_touch_down行为?

时间:2018-12-23 11:31:20

标签: python kivy

我正在构建Trivia应用,每个问题都有4个选项。对于每个选项,它与一个OptionButton类相关联。 4个OptionButton实例存储在网格布局Choices中。我想要的是:如果用户按下正确的按钮,则它应该打印selt.text, self.answer。但是似乎所有按钮都在同一时间共享用户的触摸,这是为什么?

完整代码:

import random
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ListProperty


trivias = (("What is the capital city of Singapore?", ["Singapore", \
                                                       "Bukit Timah", \
                                                       "Raffles Place", \
                                                       "Orchard Road"], "Singapore") , \
            ("What year is Indonesia's Independence", ["1946", \
                                                        "1964", \
                                                        "1945", \
                                                        "1899"], "1945") )


class OptionButton(Button):
    def __init__(self, option):
        super().__init__(text = option)

    def on_touch_down(self, touch):
        super().on_touch_down(touch)
        if self.parent.answer == self.text:
            print(self.text, self.parent.answer)


class Question(Label):
    pass


class Choices(GridLayout):
    options = ListProperty([""])
    def __init__(self, options, answer):
        super().__init__(rows = 2, cols = 2)
        self.a = OptionButton(option = options[0])
        self.b = OptionButton(option = options[1])
        self.c = OptionButton(option = options[2])
        self.d = OptionButton(option = options[3])
        self.add_widget(self.a)
        self.add_widget(self.b)
        self.add_widget(self.c)
        self.add_widget(self.d)
        self.answer = answer
        self.options = options

    def on_options(self, instance, new_options):
        self.a.text = new_options[0]
        self.b.text = new_options[1]
        self.c.text = new_options[2]
        self.d.text = new_options[3]


class AppGrid(FloatLayout):
    def __init__(self):
        super().__init__()
        rand = random.randint(0, 1)
        self.question = Question(text = trivias[rand][0])
        self.choices = Choices(trivias[rand][1], trivias[rand][2])
        self.add_widget(self.question)
        self.add_widget(self.choices)
        self.question.size_hint = (0.5, 0.3)
        self.question.pos_hint = {'x': 0.25, 'y': 0.7}
        self.choices.size_hint = (0.5, 0.6)
        self.choices.pos_hint = {'x': 0.25, 'y': 0.1}

    def newquestion(self):
        rand = random.randint(0, 1)
        self.question.text = trivias[rand][0]
        self.choices.options = trivias[rand][1]
        self.choices.answer = trivias[rand][2]



class TriviaApp(App):
    def build(self):
        root = AppGrid()
        return root


app = TriviaApp()
app.run()

1 个答案:

答案 0 :(得分:1)

您不应该将on_touch事件与按钮一起使用。除非您打算自己处理触摸是否在特定边界之内。 on_touch影响整个窗口。

请改用按钮on_presson_release的方法。