Skip to main content
 首页 » 编程设计

python之Kivy:奇怪的 Button on_touch_down 行为

2024年12月31日6lonelyxmas

<分区>

我正在构建一个 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()