Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

python 3.x - How to correctly specify the relative path to kivy atlas and the image in it?

I am trying to change the Popup background as per the answer in this question: How to change popup color in kivy

In the directory of my project there is a folder Images and a file main.py. In the Images folder there are 2 64 by 64 images:popup_background.png, popup_background_green.png

First, I create an atlas using the command:

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

After execution, the necessary files appear in the folder

Here is my code:

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')

When the Popup is launched, it is completely black color (the title, button and separator are clearly visible) instead of the expected image in the atlas.

I tried a lot of experiments to get the expected image, but nothing worked. Instead, I don't understand how, but this path works and changes color:'atlas://data/images/defaulttheme/button_pressed' I assume that it finds the image in the files of the library itself, but how?

What am I doing wrong and how can I solve this problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I looked at my code in detail and realized that the problem is in creating and displaying popup, as well as changing its properties in a separate thread, I missed it. Therefore, the background did not change.

If you also have a need to change properties in a different thread, use the @mainthread decorator on the desired function.

Import:

from kivy.clock import mainthread

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...