screenshot()给出:ImportError:无法在kivy中导入名称glReadPixels错误

时间:2014-03-31 07:17:02

标签: python kivy

要在kivy中截取一个窗口,我们可以根据文档使用屏幕截图(名称=' screenshot {:04d} .png')

http://kivy.org/docs/api-kivy.core.window.html?highlight=screenshot#kivy.core.window.WindowBase.screenshot

然而,当我尝试时,我得到以下错误:

追踪(最近一次通话):    文件" D:/ Paarth / Google_Drive / Google Drive / Codes / Python / Work / kivy / kivyBasics / Screenshots_SaveImage.py",第26行,in      MyApp的()。运行()    文件" C:\ Kivy-1.8.0-py2.7-win32 \ Python27 \ lib \ kivy \ app.py",第766行,在运行中      root = self.build()    File" D:/ Paarth / Google_Drive / Google Drive / Codes / Python / Work / kivy / kivyBasics / Screenshots_SaveImage.py",第19行,在构建中      Window.screenshot(名称=' {截图:04D} .PNG')    文件" C:\ Kivy-1.8.0-py2.7-win32 \ Python27 \ lib \ kivy \ core \ window \ window_pygame.py",第250行,截图      来自kivy.core.gl import glReadPixels,GL_RGBA,GL_UNSIGNED_BYTE  ImportError:无法导入名称glReadPixels

使用的代码:

import  kivy
kivy.require ( '1.8.0 ' )
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import WindowBase
from kivy.core.window import Window


class MyWidget(Widget):
    pass

class MyApp(App):
    def build(self):
        parent = MyWidget()

        a = Button(text="hello")
        parent.add_widget(a)
        Window.screenshot(name='screenshot{:04d}.png')

        return parent


if __name__ == '__main__':
    MyApp().run()

3 个答案:

答案 0 :(得分:1)

要明确inclement的答案,您需要执行以下操作,并在您的python文件中包含export_to_png。此

from kivy.graphics import Canvas, Translate, Fbo, ClearColor, ClearBuffers
kv = '''
cameraWidget:
    orientation: 'vertical'
    Camera:
        id: camera
        resolution: (640, 480)
        play: False
    ToggleButton:
        text: 'Play'
        on_press: camera.play = not camera.play
        size_hint_y: None
        height: '48dp'
    Button:
        text: "Take picture"
        on_press: root.TakePicture()
        height: '48dp'

'''
class cameraWidget(BoxLayout):
    def TakePicture(self, *args):
        self.export_to_png = export_to_png
        self.export_to_png(self.ids.camera, filename='test2.png')

class MyApp(App):
    def build(self):
        return Builder.load_string(kv)

if __name__ == '__main__':
    MyApp().run()

答案 1 :(得分:1)

您可以在glReadPixels中找到kivy.graphics.opengl。 在Python解释器中尝试以下内容:

>>> from kivy.graphics.opengl import glReadPixels

答案 2 :(得分:0)

这似乎是一个错误,我已经报道了一个问题。

与此同时(并且与您之前的问题相关),您可以使用Widget的新export_to_png方法,只需将其代码复制到您自己的小部件中,您就可以找到代码on github。< / p>