簡體   English   中英

python kivy 綁定文本輸入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM