簡體   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