首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy标签没有出现

Kivy标签没有出现
EN

Stack Overflow用户
提问于 2022-07-04 04:46:28
回答 1查看 79关注 0票数 0

所以我是基维的初学者。我编写了一个程序,它从列表中读取句子,并以按钮的形式显示在from布局中。are布局在Floatlayout中,因此我可以控制句子的位置。如果我点击其中一个句子,它就会为每个单词分成几个按钮。到目前一切尚好。现在我想创建一个标签,显示在Now布局下面的某个地方。只要单击word按钮,它就会显示出来,并显示与按钮相同的文本。所以让我把这句话当作按钮:

你好世界,这是一个例句。|

如果我点击这个句子:

单词应该分裂成独立的按钮。例如,当我点击‘句子’按钮时,标签应该出现在文本‘句子’下面的某个地方。

.py:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-04 07:27:47

问题是,您实际上正在向函数show中的另一个标签中添加一个标签,但是您从未访问过已经添加到应用程序子类中的标签。

解决办法如下,

  1. 首先为该标签

创建一个引用

代码语言:javascript
复制
    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

  1. 与使用kwarg.,

之前一样,在方法destroy_then_create中传递“word”。

代码语言:javascript
复制
        ...
        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)
        ...

  1. 现在对函数show

进行以下更改

代码语言:javascript
复制
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,但请记住以任何合适的方式访问应用程序实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72851870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档