2016-10-01 58 views
0

我想在网上看看不同的例子来解决这个问题,但仍然无法弄清楚。我有一个应用程序调用弹出类。弹出窗口有一个输入字段,用于对输入字段中的文本进行一些操作。操纵的数据存储在弹出类内的变量中。现在,父窗口小部件如何访问从此窗口小部件获得的数据。这是一个简短的代码示例。从基维Popoup访问数据输入

KV

<ScreenManagement>: 
ScreenManager: 
    id: manager 
    size_hint_y: 93 

    Screen: 
     name: "Case_info_screen" 
     id: sc1 

     FloatLayout: 
      BoxLayout: 

       orientation: 'vertical' 


       BoxLayout: 
        size_hint_y: 10 
        canvas.before: 
         Color: 
          rgb: 0.17, 0.17, 0.17 # your color here 
         Rectangle: 
          pos: self.pos 
          size: self.size 

        Label: 
         text_size: self.size 
         font_size: 20 
         valign: 'middle' 
         height: "75dp" 
         size_hint_y: None 
         color: 1,1,1,1 

         size_hint_x: 75 
         text: " Case Info" 
        RelativeLayout: 
         size_hint_x: 25 

         Button: 
          text_size: self.size 
          halign: 'center' 
          valign: 'middle' 
          size_hint: 0.70, 0.6 
          pos_hint: {'center_x': 0.5, 'center_y': 0.5} 
          text: 'Automatic Filler' 
          on_press: root.formfiller() 

       BoxLayout: 

        size_hint_y: 5 
        spacing: 35 
        LabelCases: 
         text: ' Name: ' 
         width: '125dp' 
         size_hint_x: None 


        TextInput: 
         id: first_name_input 
         width: '300dp' 
         height: '40dp' 
         size_hint_x: None 
         font_size: 18 


         hint_text: 'First Name' 

        TextInput: 
         id: last_name_input 
         width: '300dp' 
         height: '40dp' 
         size_hint_x: None 
         font_size: 18 
         hint_text: 'Last Name' 

<Formfillerpop>: 
    title: 'Automatic Filler' 
    size_hint: None, None 
    size: 600, 600 
    BoxLayout: 
     orientation: 'vertical' 
     Label: 
      size_hint_y: 20 
      text: 'Please Paste your text' 
      markup: True 
     TextInput: 
      id: sentence 
      size_hint_y: 65 
     BoxLayout: 
      size_hint_y: 10 
      orientation: 'horizontal' 
      Button: 
       text: 'Analyze' 
       on_press: root.on_analyze(sentence.text) 
      Button: 
       text: 'Close' 
       on_press: root.closepop() 

的Python:

class Formfillerpop(Popup): 
    selection = StringProperty 
    id = ObjectProperty 
    def on_analyze(self, selection): 
     analyze = TextExtractor(selection) 

     names = analyze.name() 




    def closepop(self): 
     self.dismiss() 


class ScreenManagement(FloatLayout): 
    def formfiller(self, *args): 
     Formfillerpop().open() 




class IOApp(App): 
    def build(self): 
     return ScreenManagement() 


if __name__ == '__main__': 
    IOApp().run() 

最后,我想从在弹出的名字取TXT,然后在主应用程序与所分析的数据自动填写姓氏和名字 对不起,如果这是基本的。我对基维来说相当陌生。

回答

0

您可以使用其content属性访问弹出内容。您可以使用它来读text属性读取基础TextInput。例如,此代码将此属性绑定到本地popup_text StringProperty,这意味着弹出文本输入中的任何更改都会反映在此处:

from kivy.uix.popup import Popup 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.properties import StringProperty 

Builder.load_string(''' 
<CustomPopup>: 
    size_hint: .5, .5 
    auto_dismiss: False 
    title: 'Hello world' 
    BoxLayout: 
     text_input: text_input 
     orientation: 'vertical' 
     TextInput: 
      id: text_input 
     Button: 
      text: 'dismiss'  
      on_press: root.dismiss() 
''') 

class CustomPopup(Popup): 
    pass 

class TestApp(App): 
    popup_text = StringProperty() 

    def build(self): 
     l = BoxLayout() 
     l.add_widget(Button(
      text='show_popup', on_press=self.show_popup 
     )) 
     l.add_widget(Button(
      text='print popup text', on_press=self.print_popup_text 
     )) 
     return l  

    def show_popup(self, *args): 
     p = CustomPopup() 
     p.content.text_input.bind(text=self.setter('popup_text')) 
     p.open() 

    def print_popup_text(self, *args): 
     print(self.popup_text) 

if __name__ == '__main__': 
    TestApp().run()