为什么窗口旋转大小调整会干扰UI布局,使图像包含奇异果

时间:2019-06-29 04:03:39

标签: python kivy kivy-language

旋转图像并调整窗口大小后,图像未移动任何假定位置。以我的假设,图像总是移动到左侧疼痛的中心。请告诉我解决方法。

此代码实现了在左侧和右侧同时产生痛苦(区域窗口)的窗口。假定左侧为菜单栏。假设右侧是通过旋转来操纵图像。如果在窗口上移动鼠标,图像将根据您的鼠标距离旋转。

MyEnv。

  • MacOS:10.14.5
  • 基辅:1.11.1
  • Python:conda的3.6.8。
from cmath import sqrt
from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import LoadIdentity
from kivy.input import MotionEvent
from kivy.properties import NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.lang import Builder

Builder.load_string("""
<Example>:
    orientation:"horizontal"
    BoxLayout:
        orientation:"vertical"
        size_hint:0.5,1 # rate of win, this is menubar.
        canvas.before:
            Color:
                rgba: 0, 0, 0.5, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            size:1,0.5
        Button:
            size:1,0.5
    FloatLayout:
        id: near_layout
        pos_hint:  {'x':0.5,'y':0}
        size_hint:0.5,1
        canvas.before:
            Color:
                rgba: 0, 1, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Image:
            id:img
            center: near_layout.center

            canvas.before:
                Rotate:
                    angle: root.angle
                    axis: 0,0,1
                    origin: self.center
            source: 'kivy.jpeg'
""")


class Example(App, BoxLayout):
    angle = NumericProperty(0)

    def build(self):
        Window.bind(on_resize=self.on_resize)
        return self

    def on_resize(self, obj, x, y):
        LoadIdentity()
        self.img = self.ids.img  # type:Image
        self.img.reload()  # Necessary,if not, image disappeared

    def on_touch_move(self, touch):
        self.angle += sqrt((touch.ppos[0] - touch.pos[0]) ** 2 + (touch.ppos[1] - touch.pos[1]) ** 2).real
        print("pos:%s,size:%s" % (self.ids.near_layout.pos, self.ids.near_layout.size))

if __name__ == "__main__":
    Example().run()

0 个答案:

没有答案