2013-11-03 53 views
0

我是python的新手,現在在做一個字典kivy。問題是當我鍵入文本時,它不起作用。在那裏,我只是想檢查它是否正常工作,所以我放了一些彈出窗口,如果輸入的文本是'a',然後打印true。這只是檢查它的工作與否,希望你們幫助我,謝謝。Python kivy文本輸入

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() 
+0

如果您想讓** kivy **在屏幕上打印某些內容,您必須將這些工具使用到_widgets_庫中。我認爲只使用_print_將不起作用。所以** if **中的部分就不會做任何事情。嘗試觸發事件,如果爲True或False,您將測試條件。 – tuned

回答

0

有兩個原因,爲什麼不工作:

  1. if應該在處理該事件的方法,即show_popup
  2. 你應該比較textLabel,而不是Label本身。取而代之的self.search=='A',你應該使用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(""" 
<[email protected]>: 
    title: "My Custom Poput"  

<[email protected]>: 
    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,您甚至可以保存在單獨的文件中。

+0

非常感謝你,這對我很有意義,順便說一句如果我想在彈出窗口上顯示我的翻譯!我必須這樣寫嗎? _CustomPopup(text =「self.search.text」)或kv語言: text:self.search.text ??? _ – Zorig

+0

我不知道我是否理解您的問題。 (1)我可以告訴你'Popup'沒有屬性'text'。你是說標題?或者你的意思是一個'Widget'(可能是'Label')文本? (2)第二個選項是錯誤的,因爲'search'不屬於'CustomPopup'。我會試着給你另一個例子,但你最好問一個不同的問題,讓更多的人可以閱讀並回復。 –

+0

我修改了示例,以便您可以將'content'發送到'CustomPopup'。你可能不想要一個'CustomPopup',有一個''CustomContent''可能會更有用。 Custom的'content'屬性將接受任何Widget。 –