Python 动态文本标记?
在过去的一个多月里,我一直在学习奇维;有一个爆炸,但这一个让我真的难住了。我有很多文本,我需要Kivy突出显示我编写的词汇表中存在的单词,当单击这些单词时,打开一个带有适当定义的弹出窗口。我有一些代码几乎可以做到这一点,但无论我点击哪个单词,我只会得到一个带有“crab”定义的弹出窗口:Python 动态文本标记?,python,dynamic,markup,kivy,Python,Dynamic,Markup,Kivy,在过去的一个多月里,我一直在学习奇维;有一个爆炸,但这一个让我真的难住了。我有很多文本,我需要Kivy突出显示我编写的词汇表中存在的单词,当单击这些单词时,打开一个带有适当定义的弹出窗口。我有一些代码几乎可以做到这一点,但无论我点击哪个单词,我只会得到一个带有“crab”定义的弹出窗口: from kivy.app import App from kivy.uix.label import Label from kivy.uix.widget import Widget from kivy.pr
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()
:
定义\弹出:定义\弹出__
标签:
文本:root.new_语句
中心y:root.height/2
中心x:root.width/2
标记:正确
按:root.definition\u popup.open()
弹出窗口:
id:定义\u弹出窗口
在\u父项上:根。删除\u小部件(自身)
标题:“定义”
内容:def_资料
盒子布局:
id:def_东西
方向:“垂直”
标签:
文本:root.glossary\u def
按钮:
文本:“返回”
发布时:root.definition\u popup.disclose()
显然,连接到弹出窗口内容的glossary_def字符串在句子副本的每次迭代中都会被覆盖,但我无法传递任何类型的“ref=X”,因为标记本身包含在字符串中。有没有办法给每个单词一个单独的标识符?还是有更好的办法?我需要能够向程序传递“句子”的任何字符串和任何词汇词典。当你点击一个参考时,kivy会告诉你点击了哪个参考。您需要使用此选项来决定在弹出窗口中显示正确的文本。单击后,您似乎根本没有设置文本。第二个问题是,您没有命名引用 以下是一些可以使其正常工作的更改:
markup: True
on_ref_press:
root.glossary_def = root.glossary_full[args[1]]
root.definition_popup.open()
以及:
你的解决方案非常有效!我看到我试图引用在on_ref_press事件之外选择的文本,在那里处理所有内容更有意义。非常感谢。
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]')