首页 > 解决方案 > 使用 kivy 模块不透明的画布中的图像

问题描述

我可以看到 CanvasWidget() 类的效果,但看不到 MyApp() 类的任何效果。为什么会这样

#Importing kivy module

import kivy

kivy.require("1.10.1")

from kivy.app import App

from kivy.uix.widget import Widget

from kivy.graphics import Rectangle, Color

from kivy.uix.image import Image

from kivy.config import Config 

#Creating the canvas

class CanvasWidget(Widget):

    def __init__(self, **kwargs):

        super(CanvasWidget, self).__init__(**kwargs)

        with self.canvas:

            Color(1, 1, 1, 1)

            self.rect = Rectangle(pos = self.center, size = (self.width / 2., self.height / 2.))

            self.bind(pos = self.update_rect, size = self.update_rect)
        
    def update_rect(self, *args):
        self.rect.pos = self.pos
        self.rect.size = self.size
        
class CanvasApp(App):
    
      def build(self):
        
         return CanvasWidget()
    
CanvasApp().run()

#Putting the image

Config.set("graphics", "resizable", True)

class MyApp(App):

    def build(self):

        self.img = Image(source = "voithos.jpg")

        self.img.allow_stretch = True

        self.img.keep_ratio = False

        self.img.size_hint_x = 1

        self.img.size_hint_y = 1

        self.img.pos = (200, 100)

        self.img.opacity = 1

        s = Widget()

        s.add_widget(self.img)
        
        return s

MyApp().run()

这是我得到的输出

标签: kivy

解决方案


该行:

CanvasApp().run()

CanvasApp在关闭之前不会返回。因此,在您 kill 之前,不会执行该行之后的任何内容CanvasApp


推荐阅读