Python kivy文本输入

时间:2013-11-03 06:26:29

标签: python dictionary kivy

我是python的新手,现在用kivy做一本字典。问题是当我输入文字时,它不起作用。在那里我只想检查它是否正常工作,所以我放了一些弹出窗口,如果输入文本是'a',则打印true。它只是检查它是否有效,希望你们帮助我,谢谢你。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.bubble import Bubble

class CustomPopup(Popup):
    pass

class Diction(GridLayout):

    def __init__(self, **kwargs):
        super(Diction, self).__init__(**kwargs)
        self.cols=2
        self.add_widget(Label(text="Search"))
        self.search=TextInput(multiline=False)
        self.add_widget(self.search)
        if self.search=='A':
            print 'True'
        else:
            print 'False'
        self.add_widget(Button(text="click",on_press=self.show_popup))
    def show_popup(self, b):
        p = CustomPopup()
        p.open()

class MyApp(App):
    def build(self):
        return LoginScreen()

if __name__=="__main__":
    MyApp().run()

1 个答案:

答案 0 :(得分:0)

有两个原因导致无法正常工作:

  1. if应位于处理事件的方法中,即show_popup
  2. 您应该比较text中的Label,而不是Label本身。您应该使用self.search=='A'
  3. 而不是self.search.text=='A'

    以下是更正的__init__show_popup代码:

    class Diction(GridLayout):
    
        def __init__(self, **kwargs):
            super(Diction, self).__init__(**kwargs)
            self.cols=2
            self.add_widget(Label(text="Search"))
            self.search=TextInput(multiline=False)
            self.add_widget(self.search)
            self.add_widget(Button(text="click",on_press=self.show_popup))
    
        def show_popup(self, b):
            if self.search.text=='A':
                print 'True'
            else:
                print 'False'
            p = CustomPopup()
            p.open()
    

    使用Kivy语言的另一种方法

    Kivy语言可以帮助您获得更清晰的代码。您的代码可能如下所示:

    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    # DON'T forget to import Label!!!
    from kivy.uix.label import Label
    from kivy.uix.popup import Popup
    from kivy.lang import Builder
    
    Builder.load_string("""
    <CustomPopup@Popup>:
        title: "My Custom Poput"    
    
    <Diction@GridLayout>:
        cols: 2
        search: id_search
        Label:
            text: "Search"
        TextInput:
            id: id_search
        Button:
            text: "click"
            on_press: root.show_popup(self)
    """)
    
    class CustomPopup(Popup):
        pass
    
    class Diction(GridLayout):    
        def show_popup(self, b):
            if self.search.text=='A':
                print 'True'
            else:
                print 'False'
        # You can send any content to the popup with the content attribute
        CustomPopup(content=Label(text=self.search.text)).open()
    
    class MyApp(App):
        def build(self):
            return Diction()
    

    有助于将逻辑与接口分开。如果您使用load_file函数而不是load_string,则甚至可以保留单独的文件。