【问题标题】:How do I display a variable image with a KV file?如何使用 KV 文件显示可变图像?
【发布时间】:2020-04-05 14:06:35
【问题描述】:

我正在尝试使用 Python 和 Kivy 制作纸牌游戏,但无法显示纸牌。到目前为止,ChaseTheAce.deal 随机选择一张牌并将其从牌组中移除。我不知道如何将字符串从卡片字典传递到 KV 文件中的图像。我是 Kivy 的新手,在 ID 以及哪些信息与 PY 文件中的信息匹配时遇到了问题。任何帮助表示赞赏,谢谢!

PY 文件

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
import random
import numpy as np

cards = ['ace_spades', 'king_spades']
dict = {'ace_spades':'ace_of_spades.png', 'king_spades':'king_of_spades2.png'}

class LoginScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class ScoreScreen(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class ChaseTheAce(App):
    cardimagefile = StringProperty()
    def deal(self):
        mycard = random.choice(cards)
        cards.remove(mycard)
        cardimagefile = (dict[mycard])
    def build(self):
        return kv

kv = Builder.load_file("cta1.kv")

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

KV 文件

<GameScreen>:
    ChaseTheAce:ChaseTheAce
    name: "GameScreen"
    GridLayout:

        rows: 3
        Image:
            id: cardimage
            source: ChaseTheAce.cardimagefile #<<<<<<<<<<<<<<<<<<<
            allow_stretch: True
        GridLayout:
            dealer:deal
            cols: 3

            Button:
                id: deal
                text: "Deal"
                on_release:
                    app.deal()

编辑删除不相关的代码部分。

【问题讨论】:

标签: python image kivy


【解决方案1】:

您的示例中有一些错误:

  • 您需要一个包含应用所有内容的根布局。
  • 您需要一个ScreenManager 来管理您的Screens(您必须将其放入其中)。
  • 要访问.py 文件中的cardimagefile 属性,您必须使用self.cardimagefile,否则您只是在创建一个新的、不同的本地cardimagefile 变量。
  • 要访问.kv 文件中的cardimagefile 属性,您必须使用app.cardimagefile

因为我没有你的图片,所以每当Imagesource 发生变化时,我就添加了一个print,并且效果很好..

py代码:

cards = ['ace_spades', 'king_spades']
dict = {'ace_spades':'ace_of_spades.png', 'king_spades':'king_of_spades2.png'}

Builder.load_file("cta1.kv")

class LoginScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class ScoreScreen(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class RootLayout(FloatLayout):  # create a root layout
    pass

class ChaseTheAce(App):
    cardimagefile = StringProperty()
    def deal(self):
        mycard = random.choice(cards)
        cards.remove(mycard)
        self.cardimagefile = (dict[mycard])  # the cardimagefile is not local needs self.
    def build(self):
        return RootLayout()  # you must return the root layout here

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

kv代码:

<RootLayout>:        # you need a root layout
    WindowManager:   # that contains a ScreenManager
        GameScreen:  # that manages the screens
            # ChaseTheAce:ChaseTheAce  # you don't need this
            name: "GameScreen"
            GridLayout:
                rows: 3
                Image:
                    id: cardimage
                    source: app.cardimagefile #<<<<<<<<<<<<<<<<<<<
                    allow_stretch: True
                    on_source: print(self.source)
                GridLayout:
                    # dealer:deal  # you don't need this
                    cols: 3

                    Button:
                        id: deal
                        text: "Deal"
                        on_release:
                            app.deal()

【讨论】:

  • 我必须将 RootLayout 导入到我的 PY 中吗? 'return kv' 会发生什么?
  • @Trippingbillies41 你必须导入FloatLayout from kivy.uix.floatlayout import FloatLayout。您仍然必须使用Builder.load_file("cta1.kv") 行。不过,您不需要将其分配到某个地方... [在我的答案中修复了它]
  • 我进行了编辑,但在我进行编辑后出现错误。有什么想法吗?
  • @Trippingbillies41 “离开登录屏幕”是什么意思?没有代码!我发布的代码解决了您发布的代码的问题。它有效,因此您可以接受它作为答案。对于其他问题,您应该创建一个不同的线程。对于会看到这一点的人来说,有一个不断变化的问题/答案……;o)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
相关资源
最近更新 更多