Kivy:从Python代码

时间:2016-01-10 12:28:30

标签: python python-2.7 kivy

简短版本: 如何从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文件:

#:kivy 1.8.0

<ScreenOne>:

    BoxLayout:
        orientation: "vertical"
        size: root.size
        spacing: 20
        padding: 20

        Label:
            text: "Main Menu"
        Button:
            text: "Button 1"
            on_release: root.manager.current = "screen2"

<ScreenTwo>:        

    BoxLayout:
        orientation: "vertical"
        size: root.size
        spacing: 20
        padding: 20

        Label:
            id: label
            text: root.questionText
        Button:
            id: button1
            text: root.button1Text
            on_release: root.button1Pressed()           
        Button:
            id: button2
            text: root.button2Text
            on_release: root.button2Pressed()            
        Button:
            id: button3
            text: root.button3Text
            on_release: root.button3Pressed()           

<Manager>:
    id: screen_manager

    screen_one: screen_one
    screen_two: screen_two

    ScreenOne:
        id: screen_one
        name: "screen1"
        manager: screen_manager

    ScreenTwo:
        id: screen_two
        name: "screen2"
        manager: screen_manager

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

提前感谢您的时间和智慧。

1 个答案:

答案 0 :(得分:1)

您需要将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]]

请将所有其他地方的Screen替换为self,因为你将变量放在kivy类上并且很奇怪;)

我希望这会对你有所帮助