繁体   English   中英

Python kivy Text输入文本

[英]Python kivy TextInput text

我正在 Python kivy 中制作应用程序,并且我的“TacScreen”中有一个可拖动的图像,每当我双击可拖动的图像时,它都会打开一个包含文本输入字段和按钮的弹出窗口。 无论我在文本输入字段中放置什么文本,我都希望该文本显示(在我的“TacScreen”中),并且在我按下按钮时关闭弹出窗口(现在当我按下按钮时,文本显示在弹出窗口中,如果我关闭弹出文本消失)下面是我的代码,我非常感谢任何帮助,因为我一直在努力寻找解决方案。 但似乎找不到任何东西。 谢谢你!

主文件

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
from functools import partial
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.image import Image
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label


class DragImage(DragBehavior, Image):
def on_touch_up(self, touch):
    uid = self._get_uid()
    if uid in touch.ud:
        print(self.source, 'dropped at', touch.x, touch.y)
    return super(DragImage, self).on_touch_up(touch)

def on_touch_down(self, touch):
    if self.collide_point(*touch.pos) and touch.is_double_tap:
            self.layout = GridLayout(cols=1)
            self.popuptextinput = TextInput(hint_text='Enter text Here')
            self.closebutton = Button(text="click to save")
            self.output = Label(text='')
            self.layout.add_widget(self.popuptextinput)
            self.layout.add_widget(self.closebutton)
            self.layout.add_widget(self.output)
            self.popup = Popup(title='Enter text here', 
                          content=self.layout)
            self.popup.open()
            self.closebutton.bind(on_release=self.updttext)

    return super(DragImage, self).on_touch_down(touch)

def updttext(self, other):
    self.output.text = self.popuptextinput.text


class StartScreen(Screen):
    pass


class TacScreen(Screen):
    pass


class MainApp(App):
    def build(self):
        return Builder.load_string(kv)


MainApp().run()

.kv 文件

kv = '''

<DragImage>:
drag_rectangle: self.center[0] - self.norm_image_size[0]/6, self.center[1] - 
    self.norm_image_size[1]/6, \
    self.norm_image_size[0]/3, self.norm_image_size[1]/3
drag_timeout: 10000000
drag_distance: 0


<TacScreen>:
#:import utils kivy.utils


DragImage
    id: lady
    pos: 0, 102
    size_hint: 1, .1
    source: "lady.png"

'''

可悲的是,我没有太多时间,但尝试用这个替换你的DragImage class:

    class DragImage(DragBehavior, Image):
    def __init__(self, **kwargs):
        super(DragImage, self).__init__(**kwargs)
        self._layout_defined = False
        self._layout = None
        self._popup_text_input = None
        self._close_button = None
        self._output = None
        self._popup = None

    def on_touch_up(self, touch):
        uid = self._get_uid()
        if uid in touch.ud:
            print(self.source, 'dropped at', touch.x, touch.y)
        return super(DragImage, self).on_touch_up(touch)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos) and touch.is_double_tap:
            self._check_layout_existence()
            self._popup.open()
        return super(DragImage, self).on_touch_down(touch)

    def _check_layout_existence(self) -> None:
        if not self._layout_defined:
            self._layout = GridLayout(cols=1)
            self._popup_text_input = TextInput(hint_text='Enter text Here')
            self._close_button = Button(text="click to save")
            self._output = Label(text='')
            self._layout.add_widget(self._popup_text_input)
            self._layout.add_widget(self._close_button)
            self._layout.add_widget(self._output)
            self._popup = Popup(title='Enter text here', content=self._layout)
            self._close_button.bind(on_release=self.update_text)

    def update_text(self, close_button: Button) -> None:
        self._output.text = self._popup_text_input.text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM