[英]python kivy Binding TextInput
我正在 python kivy 中制作应用程序,并且我的“TacScreen”中有一个可拖动的图像,所以每当我双击可拖动的图像时,它会打开一个弹出窗口,在弹出窗口内我有一个文本输入字段和一个按钮。 截至目前,当我按下“释放时”按钮时,它会关闭弹出窗口。 我想要做的是,无论我在 TextInput 字段中输入什么文本,我都希望在按下按钮时将该文本放置在我的可拖动图像下方。 我怎样才能做到这一点? 下面是我的代码。 谢谢你!
主文件
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:
print('this is a double tap')
layout = GridLayout(cols=1, row_force_default=True, row_default_height=30,
spacing=80)
popuptextinput = TextInput(hint_text='Name',
multiline=False,
background_color='blue',
halign='left',
cursor_blink=True,
cursor_color='white',
foreground_color='white',
font_size='14')
closebutton = Button(text="SAVE", background_color="black", font_size="14",
font_name='Sackersz.otf')
layout.add_widget(popuptextinput)
layout.add_widget(closebutton)
popup = Popup(title='ADD NAME',
title_color="white",
separator_color="black",
title_align="center",
separator_height="0dp",
background_color="red",
title_font="Fontyh.otf",
title_size="10",
content=layout,
size_hint_y=.6,
size_hint_x=.7)
popup.open()
closebutton.bind(on_release=popup.dismiss)
return super(DragImage, self).on_touch_down(touch)
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: name
pos: 0, 102
size_hint: 1, .1
source: "person.png"
'''
我强烈建议您创建自己的类,继承自您要使用的 class,这样您就可以修改 class 的默认行为(例如,如果您希望按钮在按下时执行某些操作) . 另外我建议你有一个.kv
文件。
由于您实例化小部件的方式,访问属性很困难,所以让我们修复它(我不会完全编写您的程序,而是一个非常相似且更快的示例)。 首先创建您的自定义类,我将在下面给您一个示例:
import stuff #Here you import all widgets and modules you need
# Here we declare our root widget. This widget will contain your "DraggableImage"
# and the Label where you want to show the TextInput.
# At the moment, we're not gonna modify the behaviors so we just wite "pass"
# Our root widget is a Layout
class MyLayout(AnchorLayout):
pass
# We also create a class for our custom Popup that inherits from Popup Widget
class MyPopup(Popup):
pass
# App class
class MainApp(App):
def build(self):
self.roota = MyLayout() # Instanciate the root widget and return it when built
return self.roota
MainApp().run()
到目前为止,如果您运行代码,您将看到一个黑屏,所以让我们将所有小部件添加到.kv
文件的根目录中
#:kivy 2.0.0
#:import Factory kivy.factory.Factory
<MyLayout>:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout: #Create a layout centered
orientation:'vertical'
size_hint: 0.5, 0.5
Button: #Add a Button
text:'Supose this Button is your draggable image'
on_press: Factory.MyPopup().open() # Open the popup
Label: #Add a Label
id: lbl1 #It's important to add an ID for further reference
size_hint: None, None
width: 200
height: 40
text: 'before'
<MyPopup>:
size_hint: (0.4, 0.4)
auto_dismiss: False #autodissmis set to False, so you have to close the popup manually
BoxLayout: #Ceate a layout so you can add more than 1 widget to the popup
orientation: 'vertical'
TextInput: #Add TextInput
id: text1 #It's important to add an ID for further reference
text: 'Hey From textInput'
multiline: False
Button: #Add a close and save button
text: 'Close and save'
on_release: root.onPopUpClosed() #The method close the popup, We'll add it to the .py file
如果此时运行代码,您将看到一个按钮和一个 label,按钮下方带有“before”文本。 假设按钮是您的可拖动图像。 如果你按下按钮会显示弹出窗口,但如果你试图关闭它,你会得到一个错误,那是因为我们没有添加关闭方法。 如您所见,Popup 中的按钮在按下时调用 function root.onPopUpClosed()
。 根引用弹出窗口小部件,所以让我们在 class 定义中添加方法onPopUpClosed()
。
class MyPopup(Popup):
def onPopUpClosed(self):
self.dismiss() # Close the popup
此时您可以打开和关闭弹出窗口。 现在需要根据文本输入更改文本 Label,为此我们只需将弹出窗口 class 中的 textInput 数据发送到包含 ZD304BA20E96D87414588EEABAC850E 的根小部件
让我们在弹出的关闭按钮被调用时发送数据,这样我们就可以使用之前创建的方法。
这是完整的代码:
import kivy
kivy.require('2.0.0') #Min version required
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
class MyLayout(AnchorLayout):
def editLabel(self, text):
self.ids.lbl1.__self__.text = text #Modified the label
class MyPopup(Popup):
def onPopUpClosed(self):
App.get_running_app().getRoot(self.ids.text1.text) #Call app method we created
self.dismiss() #Close the popup
class MainApp(App):
def build(self):
self.roota = MyLayout()
return self.roota
#This method refrences our root instnace, so we just pass the data trhough it
def getRoot(self, text):
self.roota.editLabel(text)
MainApp().run()
如您所见,当我们关闭弹出窗口时,我们将输入的文本( self.ids.text1.text
)从弹出窗口 class 发送到 App 实例,这是因为在应用程序实例中是我们的根小部件实例(roota) . 所以我们只是在 App class 中创建了一个方法来从roota
editLabel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.