的StackOverflow 当我试图从地图册中加载图片时,我对kivy有点问题:
D:\Games\Projects\python>py -2 test.py
[INFO ] [Logger ] Record log in C:\Users\%username%\.kivy\logs\kivy_18-03-05_70.txt
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[DEBUG ] [App ] Loading kv <.\my.kv>
[DEBUG ] [App ] kv <.\my.kv> not found
[DEBUG ] [Atlas ] Load <images\a.atlas>
[DEBUG ] [Atlas ] Need to load 1 images
[DEBUG ] [Atlas ] Load <images\a-0.png>
[DEBUG ] [ImageSDL2 ] Load <D:\Games\Projects\python\images\a-0.png>
[TRACE ] [Image ] u'D:\\Games\\Projects\\python\\images\\a-0.png', populate to textures (1)
a.atlas:
{
"a-0.png": {
"logo": [1291, 728, 326, 93],
"jill": [0, 0, 644, 364]
}
}
test.py:
# -*- coding: utf-8 -*-
from kivy import resources
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.graphics import Color, Rectangle
from kivy.atlas import Atlas
from kivy.uix.image import Image
from kivy.config import Config
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '720')
Config.set('kivy', 'log_level', 'trace')
class MyApp(App):
def build(self):
tt = Atlas('./images/a.atlas')
return Image(source='atlas://images/a/logo', pos=[100, 100], size=[326, 93])
if __name__ == '__main__':
MyApp().run()
现在它只是一个测试。我正在努力学习kivy,但出了点问题 任何人都知道为什么会发生这种情况?
答案 0 :(得分:0)
我认为问题在于目前形式的储蓄需要 创建一个纹理,并在这个测试中,它发生没有opengl 已创建上下文,一个简单的解决方法是添加:
from kivy.core.window import Window
或
from kivy.base import EventLoop EventLoop.ensure_window()
位于顶部 的剧本。之后它适用于我。
我不认为这实际上是一个错误,因为我们不承诺图像 没有创建opengl上下文的操作,但也许我们 可以在Image.save()中调用ensure_window()。
所以你应该只使用以下内容:
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.atlas import Atlas
from kivy.uix.image import Image
from kivy.core.window import Window
class MyApp(App):
def build(self):
tt = Atlas('images/a.atlas')
return Image(texture=tt["logo"], pos=[100, 100], size=[326, 93])
if __name__ == '__main__':
MyApp().run()
此外,如果您不想使用它,您可以取消使用Atlas:
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.image import Image
class MyApp(App):
def build(self):
return Image(source='atlas://images/a/logo', pos=[100, 100], size=[326, 93])
if __name__ == '__main__':
MyApp().run()