[英]Python kivy TextInput text not showing
我正在 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 Name 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='Edit Name here',
content=self.layout)
self.popup.open()
self.closebutton.bind(on_release=self.updttext)
return self.layout2
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"
'''
on_touch_down()
方法不應返回小部件,它應返回True
或False
(請參閱文檔)。 另外,您需要使用content=
將TextInput
添加到您的Popup
中:
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 Name 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='Edit Name here', content=self.layout)
self.popup.open()
self.closebutton.bind(on_release=self.updttext)
return super(DragImage, self).on_touch_down(touch)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.