Kivy:从 Python 代码更新显示的按钮文本
Kivy: updating displayed button text from Python code
简短版本:
如何从 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 的初学者,所以如果您能准确地告诉我我需要更改的内容,包括应该使用的特定语法,我将不胜感激。
提前感谢您的时间和智慧。
您需要使 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 class 及其怪异的 ;)
希望对您有所帮助
简短版本:
如何从 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 的初学者,所以如果您能准确地告诉我我需要更改的内容,包括应该使用的特定语法,我将不胜感激。
提前感谢您的时间和智慧。
您需要使 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 class 及其怪异的 ;)
希望对您有所帮助