为什么当我单击时,kivy将我的小部件的尺寸更改为屏幕的尺寸?

时间:2018-07-11 20:43:03

标签: python-3.x kivy

我正在尝试创建一个矩形,可以单击侧面并拖动以调整其大小的类。仅左侧和下部会检测到单击,因为其他两侧都需要检测大小,并且只要有人单击,小部件的大小就会立即更改为屏幕的大小。当您开始从底部拖动或向左拖动时,除了它已经神奇地调整了大小外,它的工作原理与预期的完全一样。我怀疑这是kivy的问题,不是我的语法,但这是我的代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, ReferenceListProperty, NumericProperty
from kivy.graphics import *
from kivy.factory import Factory


class Root(FloatLayout):
    def __init__(self):
        FloatLayout.__init__(self)
        self.add_widget(SubWindow(pos=(100, 400), size=(200, 200), color=(1., .5, .5, 1.)))
        self.add_widget(SubWindow(pos=(0, 0), size=(100, 500), color=(.5, 0.0, 1., 1.)))

class TestApp(App):
    def build(self):
    return Root()


class SubWindow(Widget):
    x = NumericProperty(0)
    y = NumericProperty(0)
    tPosRel = ReferenceListProperty(x, y)
    bcolor = ListProperty()
    def __init__(self, **kwargs):
        Widget.__init__(self)
        f = kwargs['color']
        self.size = kwargs['size']
        self.pos = kwargs['pos']
        self.bcolor = [f[0], f[1], f[2], f[3]]
        print(self.bcolor)
        self.draw()

    def draw(self, *largs):
        print("size[0]: " + str(self.size[0]))
        print("size[1]: " + str(self.size[1]))
        print("pos[0]: " + str(self.pos[0]))
        print("pos[1]: " + str(self.pos[1]))
        print(self)
        print('Drawing...')
        self.canvas.clear()
        with self.canvas:
            Color(self.bcolor[0], self.bcolor[1], self.bcolor[2], self.bcolor[3])
            Rectangle(pos=self.pos, size=self.size)

    def on_touch_down(self, touch):
        #print(self.size)
        print("Mouse X: " + str(touch.x))
        print("Mouse Y: " + str(touch.y))
        if touch.x > self.pos[0] and touch.x < self.pos[0] + self.size[0] and touch.y > self.pos[1] + self.size[1] and touch.y < self.pos[1] + self.size[1] + 10.0:
            self.side = 'up'
            print("up hit detect...")
            self.yInit = self.pos[1]
            #self.tPosRel = touch - self.pos
            touch.grab(self)

        if touch.x > self.pos[0] + self.size[0] and touch.x < self.pos[0] + self.size[0] + 10.0 and touch.y > self.pos[1] and touch.y < self.pos[1] + self.size[1]:
            self.side = 'right'
            print("right hit detect...")
            self.xInit = self.pos[0]
            touch.grab(self)

        if touch.x > self.pos[0] and touch.x < self.pos[0] + self.size[0] and touch.y > self.pos[1] - 10 and touch.y < self.pos[1]:
            self.side = 'down'
            print("down hit detect...")
            self.yInit = self.pos[1]
            touch.grab(self)

        if touch.x > self.pos[0] - 10 and touch.x < self.pos[0] and touch.y > self.pos[1] and touch.y < self.pos[1] + self.size[1]:
            self.side = 'left'
            print("left hit detect...")
            print(self.size)
            self.xInit = self.pos[0]
            touch.grab(self)

    def on_touch_move(self, touch):
        if touch.grab_current is self:
            self.drag(touch)

    def on_touch_up(self, touch):
        if touch.grab_current is self:
            touch.ungrab(self)

    def drag(self, touch):
        def up():
            self.size[1] = touch.y - self.pos[1]
            self.draw()

        def right():
            self.size[0] = touch.x - self.pos[0]
            self.draw()

        def down():
            self.size[1] += self.yInit - touch.y
            self.pos[1] = touch.y
            self.draw()

        def left():
            print("New Size: " + str(self.wInit + self.xInit - touch.x))
            print("touch.x: " + str(touch.x))
            print("self.xInit: ", str(self.xInit))
            print("self.wInit: ", str(self.wInit))
            self.size[0] += self.xInit - touch.x
            self.pos[0] = touch.x
            self.draw()

        def window():
            self.pos += touch - self.tPosRel


        c = {'up': up, 'right': right, 'down': down, 'left': left, 'window': window}
        c[self.side]()


Factory.register('SubWindow', SubWindow)
TestApp().run()

通过这种方式,我在Windows 10上使用kivy 1.10.0的python 3.6,尽管由于某些支持库的事实,我的软件至今仍在使用kivy.deps.glew而不是kivy.deps.angle。尚未对其进行更新。

0 个答案:

没有答案