【问题标题】:Text based game Kivy- text not updating基于文本的游戏 Kivy-文本未更新
【发布时间】:2018-10-20 21:18:46
【问题描述】:

我目前正在使用 Python 制作基于文本的游戏,并且一直想使用 Kivy 为其制作图形界面。到目前为止,我一直无法让它工作。

原因是我已经从 Print and Input 更改为 self.label1 = Label(text='Hello world') 等(有多个变量 - label2、3 和 4。)然后对于输入,一个文本输入框,当按钮被按下时,该输入由函数使用(目前不涉及文本框,因为我首先只是测试按钮是否有效。)。问题是,我需要一种方法来用新值更新显示的文本。例如,我希望 label1 更改为“这些是控件”。但是当单击按钮时,不会发生更改-我希望通过更改标签变量的值来使用新文本更新 GUI。我相信,由于这些被返回,上面的代码不再循环。我得到的想法是将不同的部分放入函数中,或者使用线程。有没有人有任何提示可以将我推向正确的方向。我知道问的可能太多了,如果是这样,我将继续自己寻找解决方案。如果需要,我可以展示一些代码。

import kivy.uix.boxlayout
import kivy.uix.textinput
import kivy.uix.label
import kivy.uix.button
from kivy.app import App
from random import shuffle
import time
from kivy.uix.button import Button
from kivy.clock import Clock

alive = 1
buttonPressed = 0


class SimpleApp(App):
    def build(self):
        global alive
        global buttonPressed
        donext = 0
        alive = 1

        def callback(self):
            global buttonPressed
            buttonPressed = 1

        self.label1 = kivy.uix.label.Label(text="")
        self.label2 = kivy.uix.label.Label(text="")
        self.label3 = kivy.uix.label.Label(text="You have found yourself in a dungeon, somewhere is your escape path, will you make it out, and if so, what with?")
        self.label4 = kivy.uix.label.Label(text="")
        print(buttonPressed)
        if buttonPressed == 1:
            print("Has been pressed should work theoretically")
            self.label1 = kivy.uix.label.Label(text="These are the basic controls-")
            self.label2 = kivy.uix.label.Label(text="The controls-")
            self.label3 = kivy.uix.label.Label(text="A- approach enemy/ attack enemy")
            self.label4 = kivy.uix.label.Label(text="C- Go to chest")
        print("Press enter to continue.")

        self.boxLayout = kivy.uix.boxlayout.BoxLayout(orientation="vertical")
        self.boxLayout.add_widget(self.label1)
        self.boxLayout.add_widget(self.label2)
        self.boxLayout.add_widget(self.label3)
        self.boxLayout.add_widget(self.label4)
        self.btn1 = Button(text='Hello world 1', on_press=callback)
        self.boxLayout.add_widget(self.btn1)

        return self.boxLayout # Causes script not to continue
if __name__ == "__main__":
    simple = SimpleApp()
    simple.run()

【问题讨论】:

  • 好的,我会做的。对于那个很抱歉。谢谢
  • 我现在已经完成了@eyllanesc。谢谢你告诉我
  • 首先你谈了一个问题但你没有明确指出它是什么,然后指出你被建议使用其他东西等等,但你没有解释你的目标是什么,在开始时应该出现什么,在什么动作之前应该改变 GUI 等等。另一方面,我看到你已经定义了 self.label1、self.label2、self.label3、self.label4 但我看不到在哪里你已经定义了你所说的 self.label。
  • 总而言之,我们不知道你想做什么,也就是说,是什么让你的游戏。

标签: python kivy python-3.6 kivy-language


【解决方案1】:

如果您被建议使用线程,那么您的顾问似乎不了解 GUI,在 GUI 中,任务是通过事件异步完成的,也就是说,GUI 将为您提供方法来指示何时发生了某事GUI,例如 on_press 事件会在按下按钮时通知您,因此它们会将信号连接到该事件。另一方面,GUI 具有很高的面向对象编程、面向事件编程的成分,并且在 .kv 中是一种声明性语言,所以我建议您阅读这些概念,并且 kivy 提供了很好的文档和示例,审查他们。如果要更新 Label 至少必须在整个类中都可以访问,因此它必须是类的属性并使用 text 属性,另一方面,如果要显示多行文本,请使用 @987654321 @表示有跳线。

综合以上情况,解决方法如下:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label


class SimpleApp(App):
    def build(self):
        self.label = Label(text="You have found yourself in a dungeon,\nsomewhere is your escape path,\nwill you make it out, and if so, what with?")
        self.button = Button(text="Press Me", on_press=self.on_clicked, size_hint=(1.0, None))
        layout = BoxLayout(orientation="vertical")
        layout.add_widget(self.label)
        layout.add_widget(self.button)
        return layout

    def on_clicked(self, instance):
        self.label.text = "These are the basic controls-\nThe controls-\nA- approach enemy/ attack enemy\nC- Go to chest"


if __name__ == "__main__":
    SimpleApp().run()

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2015-09-10
    • 2023-01-08
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多