Kivy:从pythoncod更新显示的按钮文本

2024-05-23 15:59:53 发布

您现在位置:Python中文网/ 问答频道 /正文

短版: 如何从Kivy应用程序的main.py文件更改屏幕上显示的按钮文本?在

加长版: 我用Kivy做了一个选择题游戏。在

我让游戏在python级别按我所希望的那样工作:当用户点击正确的答案按钮时,他们将获得分数,而附加在按钮上的答案也会发生变化。在引擎盖下一切似乎都很好。在

我的问题是我不知道如何从main.py文件更新屏幕上显示的按钮上的文本。这个游戏玩得很好,但是按钮上显示的文字永远不会改变。如何修复代码以实现此目的?在

下面是我要做的事情的简化版本:

我的main.py文件:

# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from functools import partial

import random


vocabDict = {'latte': 'milk', 'mela': 'apple', 'melograno': 'pomegranate', 'fragola': 'strawberry', 'acqua': 'water'}

vocabList = ['acqua', 'latte', 'mela', 'melograno', 'fragola']

currentWord = 'acqua'

score = 0

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):

    def __init__(self,**kwargs):
        super(ScreenTwo, self).__init__(**kwargs)
        Screen.currentWord = self.getCurrentWord()
        Screen.questionText = Screen.currentWord
        Screen.vocabDict = self.getVocabDict()
        Screen.currentScore = self.getCurrentScore()
        Screen.possibleAnswerList = [Screen.currentWord]
        self.playHand(Screen.currentWord,Screen.vocabDict,Screen.currentScore)

    def getCurrentWord(self):
        return currentWord

    def getVocabDict(self):
        return vocabDict 

    def getCurrentScore(self):
        return score

    def playHand(self,currentWord,vocabDict,score):
        possibleAnswerList = [currentWord]
        currentWord = currentWord
        vocabDict = vocabDict
        currentScore = score
        while len(possibleAnswerList) < 3:
            potentialChoice = random.choice(vocabDict.keys())
            if potentialChoice not in possibleAnswerList:
                possibleAnswerList.append(potentialChoice)
        random.shuffle(possibleAnswerList)          

        # How do I visualize these changes on screen?
        Screen.button1Text = vocabDict[possibleAnswerList[0]]
        Screen.button2Text = vocabDict[possibleAnswerList[1]]
        Screen.button3Text = vocabDict[possibleAnswerList[2]]   

        Screen.possibleAnswerList = possibleAnswerList

        print "Screen.button1Text = " + Screen.button1Text
        print "Screen.button2Text = " + Screen.button2Text
        print "Screen.button3Text = " + Screen.button3Text

    def button1Pressed(instance):
        print "Screen.possibleAnswerList[0] = " + Screen.possibleAnswerList[0]
        print "Screen.currentWord = " + Screen.currentWord
        if Screen.possibleAnswerList[0] == Screen.currentWord:
            print "Correct!"  
            Screen.currentScore += 1
            print Screen.currentScore
            instance.playHand(Screen.currentWord,Screen.vocabDict,Screen.currentScore)
        else:
            print "Incorrect!"

    def button2Pressed(instance):
        if Screen.possibleAnswerList[1] == Screen.currentWord:
            print "Correct!"  
            Screen.currentScore += 1
            print instance.currentScore
            instance.playHand(Screen.currentWord,Screen.vocabDict,Screen.currentScore)
        else:
            print "Incorrect!"

    def button3Pressed(instance):
        if instance.possibleAnswerList[2] == currentWord:
            print "Correct!"  
            instance.currentScore += 1
            print instance.currentScore
            instance.playHand(Screen.currentWord,Screen.vocabDict,Screen.currentScore)
        else:
            print "Incorrect!"

class Manager(ScreenManager):

    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class ScreensApp(App):

    def build(self):
        m = Manager(transition=NoTransition())
        return m

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

我的screens.kv文件:

^{pr2}$

很明显,我是Kivy的初学者,所以如果你能告诉我我需要改变什么,包括应该使用的特定语法,我将非常感激。在

提前感谢你的时间和智慧。在


Tags: 文件instanceimportselfifmaindefscreen
1条回答
网友
1楼 · 发布于 2024-05-23 15:59:53

您需要使button1/2/3Text成为StringProperty,例如

from kivy.properties import StringProperty # <   (:

class ScreenTwo(Screen):
    button1Text = StringProperty ('some text')
    button2Text = StringProperty ('some text2')
    button3Text = StringProperty ('some text3')



    def __init__(...):
        ...

    #notice that Screen was replaced by "self"

    # How do I visualize these changes on screen?
    self.button1Text = vocabDict[possibleAnswerList[0]]
    self.button2Text = vocabDict[possibleAnswerList[1]]
    self.button3Text = vocabDict[possibleAnswerList[2]]

请在其他地方也用self替换Screen,因为您将变量放在kivy类上,而且它很奇怪;)

我希望这对你有帮助

相关问题 更多 >