按下按钮时将var +1添加到标签(kivy)

时间:2018-05-04 15:17:15

标签: python kivy

来源func - add_click

通过按下按钮

将self.click添加到Label(self.RightBar)

我为游戏编写了一个函数(clicker),其实质是通过按下标签中的按钮,添加了一个计数器。

    def add_click(self, instance):

        self.click += 1

    def build(self):

        self.click = "0"

        Body = BoxLayout(orientation = "vertical",
                                            size_hint = [1,.8],
                                            spacing = 0.7)

        Land = BoxLayout()

        LeftBar = Image(source = "/storage/emulated/0/kivy/image/84.jpg", size_hint = [None, None], size = [1080, 610])

        self.RightBar = Label(text = "MONEY:" + "\n" + "\nUNITS:" + "\n" + "\nDAY:" + "\n" + "\nCLICKS:" + "  " + self.click,
                                        size_hint = [.3,1],
                                        valign = "top",
                                        halign = "left",
                                        text_size = [750,900])

        Land.add_widget(LeftBar);
        Land.add_widget(self.RightBar);

        Body.add_widget(Land);

        NavBar = BoxLayout(size_hint = [1, .55],
                                                spacing = 0.8)

        Body.add_widget(NavBar);

        Body.add_widget(Button(text = "*click*",
                                        font_size = 20,                                                                 background_normal = "",
                                        background_color = [.11,.11,.10,.4],
                                        size_hint = [1,1.7],
                                        on_press = self.add_click));

        return Body

2 个答案:

答案 0 :(得分:0)

我通过重写函数找到了一个解决方案:

def add_click(self, instance):
    self.click = int(self.click)
    self.click += 1
    self.click = str(self.click)
    self.RightBar.text = str("MONEY:"+"\n"+"\nUNITS:"+"\n"+"\nDAY:"+"\n"+"\nCLICKS:"+" "+self.click)

答案 1 :(得分:0)

更改以下内容:

第1步

来自字符串:

self.click = "0"

要数字:

self.click = 0

第2步

自:

self.RightBar = ... + self.click, ...

要:

self.RightBar = ... + str(self.click), ...

片段

def build(self):
    self.click = 0
    ...
    self.RightBar = Label(text = "MONEY:" + "\n" + "\nUNITS:" + "\n" + "\nDAY:" + "\n" + "\nCLICKS:" + "  " + str(self.click)

输出

Img01