我使用手风琴表现得很奇怪,我在屏幕上的文字输入也不起作用

时间:2018-03-01 20:27:09

标签: python kivy

在我的代码的显示屏幕上,我使用了手风琴,我按照指示做了所有事情,并且该页面中TextInput的代码是正确的但是手风琴没有按预期工作,而且textInput没有接受任何输入。我是kivy的新手,据我所知,每件事情对我来说都是正确的。

继承我的代码:

    import kivy
    kivy.require('1.10.0')

    from kivy.uix.stacklayout import StackLayout
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label 
    from kivy.app import App
    from kivy.uix.popup import Popup  
    from kivy.uix.screenmanager import ScreenManager, Screen 
    from kivy.lang import Builder 
    from kivy.properties import ObjectProperty
    from kivy.uix.textinput import TextInput
    from kivy.properties import StringProperty


    import json

    Builder.load_file('VocabularyJournal.kv')

    class MenuPage(Screen):
        pass

    class DisplayPage(Screen):                   # here is the display page[![enter image description here][1]][1]
        search_box= ObjectProperty()
        label_maening=StringProperty()
        label_synonym=StringProperty()
        label_ant=StringProperty()
        label_sentence=StringProperty()


        def search_function(self):
            with open('vocab_words.json') as rfile:
                data=json.load(rfile)

            word=self.search_box.text 

            for value in data:
                if value['word']==word:
                    self.label_maening=value['meaning']
                    self.label_synonym=value['synonym']
                    self.label_ant=value['antonyms']
                    self.label_sentence=value['sentence']


    class WordInsertPage(Screen):
        pass


    class NewWordPage(Screen):
        word_box = ObjectProperty()
        meaning_box = ObjectProperty()
        synonym_box = ObjectProperty()
        ant_box = ObjectProperty()
        sentence_box = ObjectProperty()


        def saving_data(self):

            with open('vocab_words.json') as rfile:
                data=json.load(rfile)


            entry={'word': self.word_box.text, 'meaning': self.meaning_box.text, 'synonym': self.synonym_box.text, 'antonyms': self.ant_box.text, 'sentence': self.sentence_box.text}
            data.append(entry)


            with open('vocab_words.json','w') as wfile:
                json.dump(data,wfile,indent=4)


    class FlashCard(Screen):
        pass

    class WordGroups(Screen):
        pass

    class Manager(ScreenManager):
        pass

    class VocabularyJournalApp(App):
        def build(self):
            return Manager()

    object = VocabularyJournalApp()
    object.run()

下面是kivy代码 -

    <Manager>:
        MenuPage:
            name: 'menu'
        WordInsertPage:
            name: 'insertword'
        NewWordPage:
            name: 'newword'
        FlashCard:
            name: 'flashcard'
        WordGroups:
            name: 'wordgroup'
        DisplayPage:
            name: 'display'

    <MenuPage>:
        Label: 
            text: "Vocabulary Journal"
            size_hint: .90,.10

        StackLayout:
            orientation: 'tb-rl'
            spacing: 10
            padding: 10

            Button:
                text: 'Search'
                size_hint: None,.20
                width: 130
                background_down:'darkgrey.png'
                on_press: root.manager.current='insertword'
            Button:
                text: 'New Word'
                size_hint: None,.20
                width: 130
                background_down:'darkgrey.png'
                on_press: root.manager.current='insertword'
            Button:
                text: 'Flash Cards'
                size_hint: None,.20
                width: 130
                background_down:'darkgrey.png'
                on_press: root.manager.current='flashcard'

            Button:
                text: 'Word Groups'
                size_hint: None,.20
                width: 130
                background_down:'darkgrey.png'
                on_press: root.manager.current='wordgroup'

    <WordInsertPage>:

        FloatLayout:

            Button: 
                text: "New Word"
                on_press: root.manager.current='newword'
                font_size: 30
                color: 0,0,0,1
                size_hint: .2, .1
                pos_hint: {"center_x": .5, "center_y": 0.3}
                background_down: 'darkgrey.png'
            Button:
                text: "search word"
                on_press: root.manager.current='display'
                font_size: 30
                color: 0,0,0,1
                size_hint: .2, .1
                pos_hint: {"center_x": .5, "center_y": 0.5}
                background_down: 'darkgrey.png'
            Button:
                text: 'Flash Cards'
                on_press: root.manager.current="flashcard"
                font_size: 30
                color: 0,0,0,1
                size_hint: .2, .1
                pos_hint: {"center_x": .5, "center_y": 0.7}
                background_down: 'darkgrey.png'



    <NewWordPage>:
        id: refer_to_it
        word_box: word_input
        meaning_box: meaning_input
        synonym_box: Synonym_input
        ant_box: ant_input
        sentence_box: sentence_input
        StackLayout:
            orientation: 'tb-rl'
            spacing: 10
            padding: 90
            TextInput:
                text: "write your word here"
                color: 1,1,1,1
                id: word_input
                width: 300
                size_hint: None, .10

            TextInput:
                text: "write meaning of your word here"
                color: 1,1,1,1
                id: meaning_input
                width: 600
                size_hint: None, .20

            TextInput:
                text: "write Synonyms of your word here"
                color: 1,1,1,1
                id: Synonym_input
                width: 600
                size_hint: None, .20

            TextInput:
                text: "write antonyms of your text here"
                color: 1,1,1,1
                id: ant_input
                width: 600
                size_hint: None, .20

            TextInput:
                text: "write a sentence based on your word here"
                color: 1,1,1,1
                id: sentence_input
                width: 600
                size_hint: None, .20

            Button:
                text: 'Save'
                size_hint: None,.10
                width: 130
                background_down:'darkgrey.png'
                on_press: refer_to_it.saving_data()     

    <DisplayPage>:                          # here is the display page
        search_box: search_text
        BoxLayout:
            size_hint_y: None
            height: '48dp'

            TextInput:
                text:'enter the word you wanna search here'
                id: search_text
                on_text_validate: root.search_function()

        Accordion:
            orientation: 'vertical'

            AccordionItem:
                title:'meaning'

                Label:
                    text: root.label_maening
                    text_size: self.width, None

            AccordionItem:
                title:'Synonym'

                Label:
                    text: root.label_synonym
                    text_size: self.width, None

            AccordionItem:
                title:'Antonym'

                Label:
                    text: root.label_ant
                    text_size: self.width, None

            AccordionItem:
                title:'Sentence'

                Label:
                    text: root.label_sentence
                    text_size: self.width, None

enter image description here

1 个答案:

答案 0 :(得分:1)

原则上我看到三个问题:

  • on_text_validate 未在多行输入中触发。您应该将multiline属性设置为False

  • 使用hint_text属性设置建议文字。

  • 您的手风琴的奇怪行为可能是因为您正在加载您的kv文件两次(您的"Multiple screens named ..."警告表明了这一点。)。我建议您将kv重命名为vocabularyjournal.kv,而不是使用Builder.load_file ()。您可以查看this related answer

<强> main.py:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty


import json


class MenuPage(Screen):
    pass

class DisplayPage(Screen):                  
    search_box= ObjectProperty()
    label_maening=StringProperty()
    label_synonym=StringProperty()
    label_ant=StringProperty()
    label_sentence=StringProperty()


    def search_function(self):
        with open('vocab_words.json') as rfile:
            data=json.load(rfile)

        word=self.search_box.text

        for value in data:
            if value['word']==word:
                self.label_maening=value['meaning']
                self.label_synonym=value['synonym']
                self.label_ant=value['antonyms']
                self.label_sentence=value['sentence']


class WordInsertPage(Screen):
    pass


class NewWordPage(Screen):
    word_box = ObjectProperty()
    meaning_box = ObjectProperty()
    synonym_box = ObjectProperty()
    ant_box = ObjectProperty()
    sentence_box = ObjectProperty()


    def saving_data(self):

        with open('vocab_words.json') as rfile:
            data=json.load(rfile)


        entry={'word': self.word_box.text, 'meaning': self.meaning_box.text, 'synonym': self.synonym_box.text, 'antonyms': self.ant_box.text, 'sentence': self.sentence_box.text}
        data.append(entry)


        with open('vocab_words.json','w') as wfile:
            json.dump(data,wfile,indent=4)


class FlashCard(Screen):
    pass

class WordGroups(Screen):
    pass

class Manager(ScreenManager):
    pass

class VocabularyJournalApp(App):
    def build(self):
        return Manager()

obj = VocabularyJournalApp()
obj.run()

<强> vocabularyjournal.kv:

<Manager>:
    MenuPage:
        name: 'menu'
    WordInsertPage:
        name: 'insertword'
    NewWordPage:
        name: 'newword'
    FlashCard:
        name: 'flashcard'
    WordGroups:
        name: 'wordgroup'
    DisplayPage:
        name: 'display'

<MenuPage>:
    Label:
        text: "Vocabulary Journal"
        size_hint: .90,.10

    StackLayout:
        orientation: 'tb-rl'
        spacing: 10
        padding: 10

        Button:
            text: 'Search'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='insertword'
        Button:
            text: 'New Word'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='insertword'
        Button:
            text: 'Flash Cards'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='flashcard'

        Button:
            text: 'Word Groups'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='wordgroup'

<WordInsertPage>:

    FloatLayout:

        Button:
            text: "New Word"
            on_press: root.manager.current='newword'
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.3}
            background_down: 'darkgrey.png'
        Button:
            text: "search word"
            on_press: root.manager.current='display'
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.5}
            background_down: 'darkgrey.png'
        Button:
            text: 'Flash Cards'
            on_press: root.manager.current="flashcard"
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.7}
            background_down: 'darkgrey.png'



    <NewWordPage>:
        id: refer_to_it
        word_box: word_input
        meaning_box: meaning_input
        synonym_box: Synonym_input
        ant_box: ant_input
        sentence_box: sentence_input
        StackLayout:
            orientation: 'tb-rl'
            spacing: 10
            padding: 90
            TextInput:
                hint_text: "write your word here"
                color: 1,1,1,1
                id: word_input
                width: 300
                size_hint: None, .10

            TextInput:
                hint_text: "write meaning of your word here"
                color: 1,1,1,1
                id: meaning_input
                width: 600
                size_hint: None, .20

            TextInput:
                hint_text: "write Synonyms of your word here"
                color: 1,1,1,1
                id: Synonym_input
                width: 600
                size_hint: None, .20

            TextInput:
                hint_text: "write antonyms of your text here"
                color: 1,1,1,1
                id: ant_input
                width: 600
                size_hint: None, .20

            TextInput:
                hint_text: "write a sentence based on your word here"
                color: 1,1,1,1
                id: sentence_input
                width: 600
                size_hint: None, .20

            Button:
                hint_text: 'Save'
                size_hint: None,.10
                width: 130
                background_down:'darkgrey.png'
                on_press: refer_to_it.saving_data()

    <DisplayPage>:                          # here is the display page
        search_box: search_text
        BoxLayout:
            size_hint_y: None
            height: '48dp'

            TextInput:
                hint_text:'enter the word you wanna search here'
                id: search_text
                multiline: False
                on_text_validate: root.search_function()

        Accordion:
            orientation: 'vertical'

            AccordionItem:
                title:'meaning'

                Label:
                    text: root.label_maening
                    text_size: self.width, None

            AccordionItem:
                title:'Synonym'

                Label:
                    text: root.label_synonym
                    text_size: self.width, None

            AccordionItem:
                title:'Antonym'

                Label:
                    text: root.label_ant
                    text_size: self.width, None

            AccordionItem:
                title:'Sentence'

                Label:
                    text: root.label_sentence
                    text_size: self.width, None
  

注意:您不应将object用作变量名称。它是一个内置函数。