在Kivy中更新标签 [英] Updating a Label In Kivy

查看:95
本文介绍了在Kivy中更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新python中的标签.这是一个简单的Guess num游戏.目前,它会在PC中打印计算机的响应,但我希望将其打印到标签上.

I am trying to update a label in python. It is a simple Guess the num game. Currently it prints the Computer responses in PC but I want it to print those to a label.

这是主要的.py文件

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random


secret=random.randint(1,100)

class Application(BoxLayout):
    label1=""

    def button_press(self):
        global label1
        if (int(self.user_guess.text)<secret):
            print("Higher")
            self.label1="Higher"
        elif(int(self.user_guess.text)>secret):
            print("Lower")
            self.label1="Lower"
        elif(int(self.user_guess.text)==secret):
            print("You WOn")
            self.label1="You WOn"

class WeatherApp(App):
    pass

if __name__ == '__main__':
    WeatherApp().run()

.kv文件

Application:

<Application>:
size:(480,30)
user_guess:guess
size_hint:(None,None)
text:""
orientation: "vertical"
BoxLayout:
    Button:
        text:'Play'
        on_press:root.button_press()
    TextInput:
        id:guess
        text:''
    Label:
        text:root.label1

推荐答案

我认为您应该使用

self.label1.text="Higher"

这篇关于在Kivy中更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆