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, размер которого будет изменяться, пока есть событие on_touch_move, запускаемое в ds, гдеDS это мое пространство для рисования для моих объектов.Размер TextBox завершается, когда происходит событие on_touch_up.Затем виджет добавляется в другой виджет, известный как DraggedWidget, который позволяет перетаскивать TextBox при возникновении события on_touch_down.
Проблема в том, что я не могу ввести текст в TextBox после установки размера.Я полагаю, это потому, что событие DraggedWidget on_touch_down запускается, который переопределяет событие TextBox on_touch_down, поэтому я не могу ввести какой-либо текст.Если я заменю свой DraggedWidget обычным виджетом, я могу добавить текст в TextBox, но затем его нельзя будет перетаскивать.
Как я могу изменить / добавить текст в TextBox после того, как размер будет завершен, а затем разрешить емубыть втянутым?