我正在使用python-2.7
和kivy
。有人帮助我,当我单击TextBox
时如何使用python或kivy代码选择文本?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)
class abc(BoxLayout):
pass
class Test(App):
def build(self):
return abc()
if __name__ == '__main__':
Test().run()
<abc>:
BoxLayout:
orientation: "vertical"
size_hint_y: .5
BoxLayout:
orientation: "horizontal"
spacing: 10, 10
size_hint_x: .6
Label:
text: "TEXT"
text_size: self.size
valign: 'middle'
size_hint_x: .2
TextInput:
size_hint_x: .4
答案 0 :(得分:1)
您必须在public class Transaction
{
public string Id { get; set; }
public int Order { get; set; }
public DateTime Time { get; set; }
public int Type { get; set; }
public decimal Lots { get; set; }
public string Symbol { get; set; }
public decimal Price { get; set; }
public decimal StopLoss { get; set; }
public decimal TakeProfit { get; set; }
public DateTime? CloseTime { get; set; }
public decimal ClosePrice { get; set; }
public decimal Swap { get; set; }
public decimal Profit { get; set; }
[StringLength(255)]
public string Comment { get; set; }
public decimal Commission { get; set; }
public DateTime? Expiration { get; set; }
public int MagicNumber { get; set; }
public string AccountNumber { get; set; }
}
旁边使用o n_touch_down
,如下所示:
select_all()
您可以通过类似的方式从python中进行操作。
*。py
#:import Clock kivy.clock.Clock
<abc>:
BoxLayout:
orientation: "vertical"
size_hint_y: .5
BoxLayout:
orientation: "horizontal"
spacing: 10, 10
size_hint_x: .6
Label:
text: "TEXT"
text_size: self.size
valign: 'middle'
size_hint_x: .2
TextInput:
size_hint_x: .4
on_touch_down: Clock.schedule_once(lambda dt: self.select_all())
*。kv
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)
class abc(BoxLayout):
pass
class MyTextInput(TextInput):
def on_touch_down(self, touch):
Clock.schedule_once(lambda dt: self.select_all())
TextInput.on_touch_down(self, touch)
class Test(App):
def build(self):
return abc()
if __name__ == '__main__':
Test().run()
答案 1 :(得分:1)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)
class abc(BoxLayout):
pass
class MyTextInput(TextInput):
def on_focus(self, instance, isFocused):
if isFocused:
Clock.schedule_once(lambda dt: self.selected_text())
def selected_text(self):
ci = self.cursor_index()
cc = self.cursor_col
line = self._lines[self.cursor_row]
len_line = len(line)
start = max(0, len(line[:cc]) - line[:cc].rfind(u' ') - 1)
end = line[cc:].find(u' ')
end = end if end > - 1 else (len_line - cc)
Clock.schedule_once(lambda dt: self.select_text(ci - start, ci + end))
class Test(App):
def build(self):
return abc()
if __name__ == '__main__':
Test().run()
<abc>:
BoxLayout:
orientation: "vertical"
size_hint_y: .5
BoxLayout:
orientation: "horizontal"
spacing: 10, 10
size_hint_x: .6
Label:
text: "TEXT"
text_size: self.size
valign: 'middle'
size_hint_x: .2
MyTextInput:
size_hint_x: .4
答案 2 :(得分:1)
解决方法如下。有关详细信息,请参阅代码片段,示例和输出。
Clock.schedule_once()
当TextInput聚焦时,选择被取消。如果需要在TextInput处于焦点时显示选择,则应该延迟(使用Clock.schedule)对选择文本的函数(select_all,select_text)的调用。
def on_focus(self, instance):
if instance.focus:
Clock.schedule_once(lambda dt: instance.select_all())
TextInput:
size_hint_x: .4
on_focus: root.on_focus(self)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)
class abc(BoxLayout):
def on_focus(self, instance):
if instance.focus:
print("TextInput is focused [focus={}]".format(instance.focus))
Clock.schedule_once(lambda dt: instance.select_all())
else:
print("TextInput is defocused [focus={}]".format(instance.focus))
class Test(App):
def build(self):
return abc()
if __name__ == '__main__':
Test().run()
#:kivy 1.11.0
<abc>:
BoxLayout:
orientation: "vertical"
size_hint_y: .5
BoxLayout:
orientation: "horizontal"
spacing: 10, 10
size_hint_x: .6
Label:
text: "TEXT"
text_size: self.size
valign: 'middle'
size_hint_x: .2
TextInput:
size_hint_x: .4
on_focus: root.on_focus(self)