【问题标题】:Updating a Label In Kivy在 Kivy 中更新标签
【发布时间】:2015-06-09 21:35:49
【问题描述】:

我正在尝试更新 python 中的标签。这是一个简单的猜数字游戏。目前它在 PC 中打印计算机响应,但我希望它将它们打印到标签上。

这是主要的 .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

【问题讨论】:

    标签: python-3.x kivy


    【解决方案1】:

    我认为你应该使用

    self.label1.text="Higher"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      相关资源
      最近更新 更多