【问题标题】:How to correctly specify the relative path to kivy atlas and the image in it?如何正确指定 kivy atlas 的相对路径及其中的图像?
【发布时间】:2021-01-08 19:29:26
【问题描述】:

我正在尝试根据此问题的答案更改 Popup 背景: How to change popup color in kivy

在我的项目目录中有一个文件夹Images和一个文件ma​​in.py。在 Images 文件夹中有 2 64 x 64 图片:popup_background.png, popup_background_green.png

首先,我使用以下命令创建图集:

python -m kivy.atlas myatlas 256x256 popup_background.png popup_background_green.png

执行后,文件夹中出现必要的文件

这是我的代码:

cnt = Button(text='Close', size_hint=(0.3, 0.2))
popup = Popup(title='AlertTitle',
              content=cnt,
              size_hint=(.5, .5),
              background='atlas://Images/myatlas/popup_background')

Popup 启动时,它是全黑(标题、按钮和分隔符清晰可见),而不是图集中预期的图像。 p>

我尝试了很多实验来获得预期的图像,但没有任何效果。相反,我不明白如何,但这条路径工作并改变颜色:'atlas://data/images/defaulttheme/button_pressed' 我假设它在库本身的文件中找到图像,但是如何?

我做错了什么,我该如何解决这个问题?

【问题讨论】:

  • 查看 myatlas.atlas 文件(应该只是 json 文本)。应该有一本字典。顶级字典应该有一个键,它是一个包含popup_background.pngpopup_background_green.png 的png 文件的名称(类似于myatlas-0.png)。内部字典的键应该是popup_backgroundpopup_background_green。那是你看到的吗? myatlas-0.png 是否在同一个文件夹中?
  • myatlas.atlas 文件的内容:{"myatlas-0.png": {"popup_background": [2, 190, 64, 64], "popup_background_green": [68, 190, 64, 64]}} 文件中提到的所有图像都在图像文件夹和图集中。注意我指出的第二个选项,它以某种方式起作用,我需要知道为什么。
  • 我刚刚创建了一个新项目并做了同样的事情,一切正常。相对路径效果很好,但是在之前的项目中有些东西阻止了它,我会在弄清楚问题时写下它。

标签: python-3.x popup kivy


【解决方案1】:

我详细查看了我的代码并意识到问题在于创建和显示弹出窗口,以及在单独的线程中更改其属性,我错过了。因此,背景没有改变。

如果您还需要更改不同线程中的属性,请在所需函数上使用 @mainthread 装饰器。

进口:

from kivy.clock import mainthread

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多