python - Kivy - 动态文本标记?

标签 python dynamic markup kivy

过去一个月左右我一直在学习 Kivy;玩得很开心,但这件事让我真的很难过。我有很多文本,我需要 Kivy 突出显示我编写的术语表中存在的那些单词,当单击这些单词时,打开一个包含适当定义的弹出窗口。我有一些代码几乎可以做到这一点,但无论我单击哪个单词,我都只会弹出一个带有“crab”定义的弹出窗口:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ListProperty, DictProperty, ObjectProperty

sentence = 'For breakfast today we will be having cheese and crab cakes and milk'

my_gloss = {'crab':'a beach-dwelling critter',
        'milk':'leads to cheese when processed',
        'cheese':'the processed milk of a given ungulate',
        'breakfast':'the first meal of the day',
        }

class GlossaryGrabber(Widget):

    sentence = StringProperty(sentence)
    glossary_full = DictProperty(my_gloss)
    gloss_curr_key_list = ListProperty([])
    new_sentence = StringProperty()
    definition_popup = ObjectProperty(None)
    glossary_def = StringProperty()

    def highlight_terms(self):
            self.gloss_curr_key_list = self.glossary_full.keys()
        sent_copy = self.sentence.split(' ')
        for i in self.gloss_curr_key_list:
            if i in sent_copy:
                sent_copy.insert(sent_copy.index(i), '[ref=][b][color=ffcc99]')
                sent_copy.insert(sent_copy.index(i) + 1, '[/color][/b][/ref]')
                self.glossary_def = self.glossary_full[i]
        self.new_sentence = ' '.join(sent_copy)

class GlossaryApp(App):

    def build(self):
        g = GlossaryGrabber()
        g.highlight_terms()
        return g

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

及其免费的 .kv 文件:

<GlossaryGrabber>:

    definition_popup: definition_popup.__self__

    Label:
        text: root.new_sentence
        center_y: root.height/2
        center_x: root.width/2
        markup: True
        on_ref_press: root.definition_popup.open()

    Popup:
        id: definition_popup
        on_parent: root.remove_widget(self)
        title: 'definition'
        content: def_stuff
        BoxLayout:
            id: def_stuff
            orientation: 'vertical'
            Label:
                 text: root.glossary_def
            Button:
                text: 'go back'
                on_release: root.definition_popup.dismiss()

显然,在句子副本的每次迭代中,连接到弹出窗口内容的glossary_def字符串都会被覆盖,但我无法传递任何类型的“ref=X”,因为标签本身包含在字符串中。有没有办法给每个单词一个单独的标识符?或者有更好的方法来解决这个问题吗?我需要能够向程序传递“句子”的任何字符串以及任何词汇表词典。

最佳答案

当您单击某个引用时,kivy 会告诉您单击了哪个引用。您需要使用它来决定在弹出窗口中显示正确的文本。单击后您似乎根本没有设置文本。第二个问题是你没有说出裁判的名字。

以下是一些可以使其正常工作的更改:

markup: True
on_ref_press:
    root.glossary_def = root.glossary_full[args[1]]
    root.definition_popup.open()

还有:

for i in self.gloss_curr_key_list:
    if i in sent_copy:
        sent_copy.insert(sent_copy.index(i), '[ref={}][b][color=ffcc99]'.format(i))
        sent_copy.insert(sent_copy.index(i) + 1, '[/color][/b][/ref]')

关于python - Kivy - 动态文本标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297108/

相关文章:

java - IntelliJ IDEA - 在 Java 文档注释中渲染 HTML 标记

data-binding - 如何在 JavaFX 2 中的 FXML 中进行绑定(bind)?

python - 如何用不同颜色绘制同一 Pandas Series 列的不同部分?

python - pandas to_datetime 将 71 转换为 2071 而不是 1971

javascript - 如何使用 javascript 动态添加 html 元素并在 php 中保存它们的记录

iphone - UITableViewCell 动态高度与标签

wiki - RTF 到 Wiki 转换器?

Python psycopg2 检查行是否存在

python - 做self.something = something的目的是什么

javascript - 如何在 Meteor 中创建动态 JavaScript 语句?