Python,Kivy:按钮没有更新文本

时间:2015-05-10 14:13:03

标签: android python python-3.x kivy

我试图为Android制作类似的应用程序:您是否愿意 (所有内容 斜体 不是必需的)
最后,我总共制作了4个应用程序(技术上重复),并且在kivy代码中有所不同。
最初的问题是带有选项的按钮,可以选择不触发功能或纹理消失,所有这些都已修复。

我现在遇到的问题是,按下后按钮没有更新。输出文件被更新,因此调用并执行该函数。 然而,按下按钮后的文本在按下按钮后不会更新 这是代码:

#Imports
import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from os import system as Sys




#Variables
QuestionsDict = {0:["Te faire couper une jambe","Te faire couper un bras"],1:["Lire les penses d'une personne, une fois par jour","Voir jusqu'à un jour dans le futur pendant maximum 1 heure"]}




#Complex variables
try:
    PlayerData = eval(open("TPLocal.dat","r").read()) #PlayerData is a base 3 number 0 = not answered question, 1 = chose option 1, 2 = chose option 2
except:
    PlayerData = str() # if file id not present (1st time opening app)
finally:
    PlayerData = list(str(PlayerData)) #for editing purposes

if len(PlayerData) < len(QuestionsDict):
    PlayerData += "0" * (len(QuestionsDict) - len(PlayerData)) #in case questions have been added to app since last open




#Functions
def b58encode(): #smaller text to send to us for statistical analysis (App is for a e-con student)
    number = int("".join(PlayerData),3)
    alphabet='0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnoqrstuvwxyz'
    base36 = str()

    while not number == 0:
        number, i = divmod(number, 58)
        base36 = alphabet[i] + base36

    return base58




#Classes
class TPGame(BoxLayout): 

    Question1 = StringProperty(QuestionsDict[PlayerData.index("0")][0])
    Question2 = StringProperty(QuestionsDict[PlayerData.index("0")][1])


    def Q1(self):
        PlayerData[PlayerData.index("0")] = "1"
        open("TPLocal.dat","w").write(str("".join(PlayerData)))

    def Q2(self):
        PlayerData[PlayerData.index("0")] = "2"
        open("TPLocal.dat","w").write(str("".join(PlayerData)))

    def copytoclipboard(self):
        Sys('echo|set /p=' + b58encode(PlayerData) + '|clip')


class TuPreferesApp(App):
    def build(self):
        return TPGame()




#Core
if __name__ in ('__main__','__android__'):
    TuPreferesApp().run()

.kv文件:

#:kivy 1.9.0

<TPGame>:
    orientation: "vertical"
    Label:
        text: u"Tu pr\u00E9f\u00E8res?"
        text_size: self.size
        halign: "center"
        valign: "middle"

    Button:
        background_normal: "Op1.png"
        background_down: "Op1D.png"
        text: root.Question1
        text_size: self.size
        halign: "center"
        valign: "middle"
        on_press: root.Q1()

    Button:
        id: B2
        background_normal: "Op2.png"
        background_down: "Op2D.png"
        text: root.Question2
        text_size: self.size
        halign: "center"
        valign: "middle"
        on_press: root.Q2()

    Button:
        background_normal: "Black.png"
        background_down: "Black.png"
        text: "Si vous voulez nous envoyer vos reponses\n Appuyez ici"  #translates to "if you want to sent us your answers press here"
        text_size: self.size
        halign: "center"
        valign: "bottom"
        on_press: root.copytoclipboard()

每次按下按钮,我都无法更改按钮上的文字。我尝试使用 Clock setter ,但没有成功 我想要做的是更新前两个按钮上的文本,以显示下两个可供选择的选项。 (稍后将在QuestionsDicts中提出更多问题,无限量)
抱歉,该应用中的文字是法语(我是法语) 提前谢谢你 斯蒂芬

1 个答案:

答案 0 :(得分:0)

那么也许您应该将按钮绑定到某个更新文本的功能? 像

Button(on_release = do.something) # do.something is a function

或.kv文件:

button:
on_release: root.something

当然你也可以使用on_press但这会削减按下按钮的奇特动画

编辑: 好的,我错了,现在我有一个问题:

def b58encode(*args): #smaller text to send to us for statistical analysis (App is for a e-con student)
number = int("".join(PlayerData),3)
alphabet='0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnoqrstuvwxyz'
base36 = str()

while not number == 0:
    number, i = divmod(number, 58)
    base36 = alphabet[i] + base36
return base58

base58在哪里?当我使用剪贴板时,我有一个错误,也许我应该是base36?

好吧也许这个: Kivy文件

<TPGame>:
orientation: "vertical"
button2: B2

和main.py文件:

class TPGame(BoxLayout):

Question1 = StringProperty(QuestionsDict[PlayerData.index("0")][0])
Question2 = StringProperty(QuestionsDict[PlayerData.index("0")][1])
button2 = ObjectProperty()
...
def Q2(self):
    PlayerData[PlayerData.index("0")] = "2"
    open("TPLocal.dat","w").write(str("".join(PlayerData)))
    self.button2.text = 'PRESSED'