kivy - 使用 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()
解决方案
该行:
CanvasApp().run()
CanvasApp
在关闭之前不会返回。因此,在您 kill 之前,不会执行该行之后的任何内容CanvasApp
。
推荐阅读
- linq - Linq 结果类型?
- javascript - javascript 上的数组减少返回具有数组可链接性的错误值
- python - 在 Visual Studio 2017 中运行 Python,输出显示不正确?
- javascript - 如何使用jquery获取选定行的列
- aem - Javascript 中的 JCR_SQL2 示例?
- sql-server - 在 SQL Server 中将列类型从 TinyInt 转换为 Int
- function - gmake 函数/ifneq/else/endif
- typescript - 在 TypeScript 中扩展 3rd 方接口
- android - java.lang.IllegalArgumentException:未能找到包含 /data/data/ 的已配置根目录
- artifactory - 用于 jfrog 布局的 baserev 的自定义正则表达式