繁体   English   中英

在 python 中创建标签并在 kivy 设置中按下按钮后为其添加背景颜色

[英]Creating a label in python and adding it a background color after a button press in a kivy setup

我想在 python 中创建一个带有背景颜色的标签,在我按下一个按钮后,在一个 kivy 设置中。

我已经编写了下面的代码并且它运行没有错误但是由于我找不到的错误,当我按下按钮时,不会在标签后面创建画布。 我该如何纠正它?

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle

class MyWinMan(ScreenManager):
    pass

class W_MainMenu(Screen):

    def button_press(self):

        lbl_info = Label(   
                            text                = 'Please select the file...',
                            size_hint           = ( 1, 0.6),
                            font_size           = 18,
                            color               = ( 180/255, 180/255, 180/255, 1),
                            )

        with lbl_info.canvas:
                Color( 50/255, 50/255, 50/255, 1)
                Rectangle(pos=lbl_info.pos, size=lbl_info.size)

        self.manager.get_screen("win_Main").ids.scr_Main_lvl_A.add_widget(lbl_info)


kv = Builder.load_string("""

MyWinMan:

    W_MainMenu:

<W_MainMenu>:
    
    name:           "win_Main"

    BoxLayout:

        id:             scr_Main_lvl_A
        orientation:    "vertical"
        size:           root.width, root.height
        padding:        40
        spacing:        10

        Button:
        
            text:       'Test'
            id:         btn_chk
            font_size:  20
            on_release: root.button_press()
             
""")

Window.size = (700, 460)
Window.top = 50
Window.left = 100

class MyApp(App):

    def build(self):
        return kv

if __name__ == '__main__':

    MyApp().run()

您在canvas中创建的Rectangle在创建Label时使用Label的大小。 那时Label的大小和位置是(100,100)的默认大小和(0,0)的默认位置。 更新Label大小/位置时, Rectangle不会自动更新。 因此,您必须编写代码来进行更新,或者使用kv (它将为您处理更新)。 所以,我建议创建一个自定义Label类来做你想要的。 首先新建一个Label类,在py代码中使用:

class LabelInfoButton(Button):
    pass


class W_MainMenu(Screen):

    def button_press(self):
        lbl_info = LabelInfoButton(
            text='Please select the file...',
            size_hint=(1, 0.6),
            font_size=18,
            color=(180 / 255, 180 / 255, 180 / 255, 1),
        )

        # with lbl_info.canvas:
        #     Color(50 / 255, 50 / 255, 50 / 255, 1)
        #     Rectangle(pos=lbl_info.pos, size=lbl_info.size)

        self.manager.get_screen("win_Main").ids.scr_Main_lvl_A.add_widget(lbl_info)

并在您的kv中设置所需的行为:

<LabelInfoButton>:
    canvas.before:
        Color:
            rgba: 50 / 255, 50 / 255, 50 / 255, 1
        Rectangle:
            pos: self.pos
            size: self.size

我通过使用 Clock.schedule_once 解决了它,因为应该在创建标签后获得 pos 和 size 值,但我猜它之前试图获得它们。 因此我增加了一点延迟。 然而,更好的解决方案总是受欢迎的......

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle
from kivy.clock import Clock

class MyWinMan(ScreenManager):
    pass

class W_MainMenu(Screen):

    lbl_info = ""

    def create_my_label(self):

        self.lbl_info = Label(   
                            text                = 'Please select the file...',
                            size_hint           = ( 1, 0.6),
                            font_size           = 18,
                            color               = ( 180/255, 180/255, 180/255, 1),
                            )

        self.manager.get_screen("win_Main").ids.scr_Main_lvl_A.add_widget(self.lbl_info)

    def set_canvas(self, dt=0):

        with self.lbl_info.canvas:
            Color( 50/255, 50/255, 50/255, 1)
            Rectangle(pos=self.lbl_info.pos, size=self.lbl_info.size)

    def button_press(self):

        self.create_my_label()
        Clock.schedule_once(self.set_canvas)


kv = Builder.load_string("""

MyWinMan:

    W_MainMenu:

<W_MainMenu>:
    
    name:           "win_Main"

    BoxLayout:

        id:             scr_Main_lvl_A
        orientation:    "vertical"
        size:           root.width, root.height
        padding:        40
        spacing:        10

        Button:
        
            text:       'Test'
            id:         btn_chk
            font_size:  20
            on_release: root.button_press()
             
""")

Window.size = (700, 460)
Window.top = 50
Window.left = 100

class MyApp(App):

    def build(self):
        return kv

if __name__ == '__main__':

    MyApp().run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM