首页 > 解决方案 > 为什么 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为它更新了。

标签: python-3.xkivy

解决方案


推荐阅读