如何拖动/移动TextInput框(TextBox),同时还能够在Kivy Python中添加/更改其文本?

时间:2018-10-28 22:29:30

标签: python kivy

import math
from kivy.uix.togglebutton import ToggleButton
from kivy.graphics import *
from uwidgets import *
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget

class UToolButton(ToggleButton):

    def on_touch_down(self,touch):
        ds=self.parent.drawing_space
        if self.state=='down' and ds.collide_point(touch.x,touch.y):
            (x,y)=ds.to_widget(touch.x,touch.y)
            self.draw(ds,x,y)

            return True
        return super(UToolButton, self).on_touch_down(touch)

    def draw(self,ds,x,y):
        pass

class UTextBox(UToolButton):

    def draw(self,ds,x,y):

        (self.ix,self.iy)=(x,y)

        self.widget=DraggedWidget(pos=(x,y),size=(100,100),size_hint=(None,None))
        self.text_=TextInput(multiline=False)
        self.widget.add_widget(self.text_)

        ds.add_widget(self.widget)
        ds.bind(on_touch_move=self.update_text)
        ds.bind(on_touch_up=self.end_text)

    def update_text(self, ds,touch):
        if ds.collide_point(touch.x,touch.y):
            (x,y)=ds.to_widget(touch.x,touch.y)
            ds.remove_widget(self.widget)
            self.widget.remove_widget(self.text_)
            self.widget=DraggedWidget(pos=(self.ix,self.iy),size=(abs(x-self.ix),abs(y-self.iy)),size_hint=(None,None),multiline=False)
            self.widget.add_widget(self.text_)
            ds.add_widget(self.widget)

    def end_text(self,ds,touch):
        ds.unbind(on_touch_move=self.update_text)
        ds.unbind(on_touch_up=self.end_text)
        ds.remove_widget(self.widget)
        self.widget.remove_widget(self.text_)
        (fx,fy)=ds.to_widget(touch.x,touch.y)
        self.widget = DraggedWidget(pos=(self.ix, self.iy), size=(abs(fx - self.ix), abs(fy - self.iy)),size_hint=(None,None), multiline=False)
        self.widget.add_widget(self.text_)
        ds.add_widget(self.widget)

这是DraggedWidget的代码:

from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *

class DraggedWidget(RelativeLayout):

    def __init__(self,**kwargs):
        self.selected=None
        super(DraggedWidget,self).__init__(**kwargs)

    def on_touch_down(self, touch):
        if self.collide_point(touch.x,touch.y):
            self.select()
            return True
        return super(DraggedWidget, self).on_touch_down(touch)

    def select(self):
        if self.selected==None:
            self.ix=self.center_x
            self.iy=self.center_y
            with self.canvas:

                self.color1=Color(1,1,1,1)
                self.center_circle=Ellipse(pos=(self.width/2-5,self.height/2-5),size=(10,10))
                self.color2=Color(1,0,0,1)
                self.selected=Line(rectangle=(0,0,self.width,self.height),dash_offset=2)

    def on_touch_move(self, touch):
        (x,y)=self.parent.to_parent(touch.x,touch.y)
        if self.selected != None and self.parent.collide_point(x-self.width/2,y-self.height/2):

            self.translate(touch.x-self.ix,touch.y-self.iy)

            return True


    def translate(self,x,y):
        print('k')
        self.ix=self.ix+x ; self.iy=self.iy+y
        self.center_x=self.ix ; self.center_y= self.iy

    def on_touch_up(self, touch):
        if self.selected!=None:
            self.unselect()

            return True
        return super(DraggedWidget, self).on_touch_up(touch)

    def unselect(self):
        if self.selected!=None:
            self.canvas.remove(self.selected)
            self.canvas.remove(self.color2)
            self.canvas.remove(self.center_circle)

            self.selected=None

好吧,所以我想做的是创建一个TextBox,它的大小可以变化,只要ds中有on_touch_move事件触发,其中ds是我为对象绘制的空间。触发on_touch_up事件时,将确定TextBox的大小。然后将该小部件添加到另一个名为DraggedWidget的小部件中,当on_touch_down事件触发时,该小部件将允许拖动TextBox。

问题是设置大小后,我无法在文本框中输入文本。我相信这是因为触发了DraggedWidget on_touch_down事件,该事件将覆盖TextBox on_touch_down事件,因此我无法输入任何文本。如果我将DraggedWidget替换为普通的窗口小部件,则可以在TextBox中添加文本,但是无法拖动。

在确定大小后如何拖动文本框后如何在文本框中更改/添加文本?

0 个答案:

没有答案