【问题标题】:can't position label with round button to top corner kivy无法将带有圆形按钮的标签定位到顶角 kivy
【发布时间】:2019-08-13 12:52:14
【问题描述】:

我正在尝试获取一个带有圆形按钮的标签,该按钮放置在左上角,但似乎无法完成。

-->backlabel 中都尝试了 pos_hint。

摘自我的代码:

tryout.py:

class RoundedButton(Button):
    pass

class BackLabel(Label):
    def __init__(self, **kwargs):
        super(BackLabel, self).__init__(**kwargs)
        self.RoundedButton=RoundedButton()
        self.add_widget(self.RoundedButton)  

class SubScreen(Screen):
    def __init__(self, **kwargs):
        super(SubScreen, self).__init__(**kwargs)
        self.BackLabel = BackLabel(id='bl')
        self.add_widget(self.BackLabel)
        self.OpenLink = Button(id= 'forward')   
        self.add_widget(self.OpenLink)
        self.Footer = Label(id='footer')
        self.add_widget(self.Footer)    

.kv 文件:

<BackLabel@Label>:
    background_color: 0,0,0,0
    canvas.before:
        Color:
            rgb: 0, 1, 0
        RoundedRectangle:
            size: [100,100]
            radius: [50,]


<RoundedButton@Button>:
    background_color: 0,0,0,0  # the last zero is the critical on, make invisible
    canvas.before:
        Color:
            rgba: (.4,.4,.4,1) if self.state=='normal' else (0,.7,.7,1)  # visual feedback of press
        RoundedRectangle:
            size: [100,100]
            radius: [50,]
    text:'<--Back'
    on_release: app.root.current = 'mainmenu'


<SubScreen>
    name: 'submenu'
    BackLabel:
        id: 'bl'

【问题讨论】:

    标签: python position label kivy


    【解决方案1】:

    很难准确地解释您正在尝试做什么。例如,为什么要在BackLabel__init__() 方法中将RoundedButton 添加到BackLabel?无论如何,这是您的代码的一个更简单的版本,可以满足您的需求。需要注意的几件事:为了有意义地定位Widget,它必须有一个合理的size。任何Widget 的默认size 与其父级相同,因此定位它并没有真正意义。所以我下面的代码设置了RoundButton 的大小(这要求size_hint(None, None)。最后,可以设置位置(如下面kv 中的SubScreen 规则):

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import Screen, ScreenManager
    
    
    class SubScreen(Screen):
        pass
    
    
    Builder.load_string('''
    <RoundedButton@Button>:
        size_hint: (None, None)
        size: (100, 100)
        background_color: 0,0,0,0  # the last zero is the critical on, make invisible
        canvas.before:
            Color:
                rgba: (.4,.4,.4,1) if self.state=='normal' else (0,.7,.7,1)  # visual feedback of press
            RoundedRectangle:
                pos: (self.center_x - 50, self.center_y - 50)
                size: self.size
                radius: [50,]
        text:'<--Back'
        on_release: app.root.current = 'mainmenu'
    
    
    <SubScreen>
        name: 'submenu'
        RoundedButton:
            pos: (0, root.height-self.height)
    ''')
    
    
    class TheApp(App):
        def build(self):
            sm = ScreenManager()
            sm.add_widget(SubScreen())
            return sm
    
    TheApp().run()
    

    【讨论】:

    • 感谢约翰·安德森。有效。我知道将按钮放在标签中有点奇怪,但我让按钮可见的唯一方法。您对我的代码段的编辑工作完美。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2013-10-07
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多