所以我是基维的初学者。我编写了一个程序,它从列表中读取句子,并以按钮的形式显示在from布局中。are布局在Floatlayout中,因此我可以控制句子的位置。如果我点击其中一个句子,它就会为每个单词分成几个按钮。到目前一切尚好。现在我想创建一个标签,显示在Now布局下面的某个地方。只要单击word按钮,它就会显示出来,并显示与按钮相同的文本。所以让我把这句话当作按钮:
你好世界,这是一个例句。|
如果我点击这个句子:
单词应该分裂成独立的按钮。例如,当我点击‘句子’按钮时,标签应该出现在文本‘句子’下面的某个地方。
.py:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Widget, Label
from kivy.config import Config
sentences = ['This is example sentence 1.', 'This is example sentence 2.', 'This is example sentence 3.', 'This is example sentence 4.', 'This is example sentence 5.', 'This is example sentence 6.']
words = [ ['This', 'is' , 'example' , 'sentence' , '1' ], ['This', 'is' , 'example' , 'sentence' , '2' ], ['This', 'is' , 'example' , 'sentence' , '3' ], ['This', 'is' , 'example' , 'sentence' , '4' ], ['This', 'is' , 'example' , 'sentence' , '5' ], ['This', 'is' , 'example' , 'sentence' , '6' ] ]
class AdaptiveButton(Button):
"""This button takes as much space as it needs to contain its text."""
def __init__(self, **kwargs):
super(AdaptiveButton, self).__init__(**kwargs)
self.size_hint = (None, None)
self.bind(texture_size = self.setter("size"))
class Boxlayout(BoxLayout):
"""This will arrange all the necessary widgets."""
def __init__(self, **kwargs):
super(Boxlayout, self).__init__(**kwargs)
self.orientation = "vertical"
self.spacing = "10dp"
for i, line in enumerate(sentences):
abtn = AdaptiveButton(text = line, font_size=30)
abtn.bind(on_press = lambda btn, j = i : self.destroy_then_create(btn, j))
self.add_widget(abtn)
def destroy_then_create(self, btn, index):
self.remove_widget(btn)
box = BoxLayout(size_hint = (None, None), spacing = "10dp")
box.bind(minimum_size = box.setter("size"))
sentence = words[index]
i = len(sentences)-index-1
for word in sentence:
abtn = AdaptiveButton(text = word)
abtn.bind(on_press=lambda btn, j=i: show(word)) # Here is the function for #the label being binded
box.add_widget(abtn)
self.add_widget(box, i)
class Meaningclass(Label): # Label class
pass
def show(k): #function for the Label
MeC = Meaningclass()
Meaning = Label(text=k, font_size=30)
MeC.add_widget(Meaning)
class MyApp(App):
def build(self):
self.icon = 'budspencer.png'
# creating Floatlayout
Fl = FloatLayout()
Box = Boxlayout(pos_hint={'x': .23, 'y': .58})
meaning = Meaningclass(pos_hint={'x': .9, 'y': .1})
Fl.add_widget(Box)
Fl.add_widget(meaning) #Here we add the Label to the FloatLayout
# return the layout
return Fl
# run the App
if __name__ == "__main__":
MyApp().run()发布于 2022-07-04 07:27:47
问题是,您实际上正在向函数show中的另一个标签中添加一个标签,但是您从未访问过已经添加到应用程序子类中的标签。
解决办法如下,
创建一个引用
def build(self):
...
Box = Boxlayout(pos_hint={'x': .23, 'y': .58})
# Create a reference of the label with 'self'.
self.meaning = Meaningclass(pos_hint={'x': .2, 'y': .1}, font_size=30) # Reduce it so that it doesn't go out of the screen.
Fl.add_widget(Box)
Fl.add_widget(self.meaning) #Here we add the Label to the FloatLayout之前一样,在方法destroy_then_create中传递“word”。
...
for word in sentence:
abtn = AdaptiveButton(text = word)
abtn.bind(on_press=lambda btn, w=word: show(w)) # Pass the word only as before using kwarg.
box.add_widget(abtn)
...show,进行以下更改
def show(k): #function for the Label
# Grab the running app instance.
app = App.get_running_app()
# Access the label you already added to the app's subclass and set the text.
app.meaning.text = k您可以在代码中的任何地方添加此函数show,但请记住以任何合适的方式访问应用程序实例。
https://stackoverflow.com/questions/72851870
复制相似问题