Kivy小部件无法改变pos

问题描述 投票:0回答:1

我想在TabbedPanelItem的右下角放一个圆形按钮(我更改的buttonBehavior使其成为圆圈)。

所以我喜欢这样:Kivy文件:

#:kivy 1.0.9

<CircularButton>:
    size: (min(self.width,self.height),min(self.width,self.height)) # force circle
    canvas:
        Color:
            rgba: ((1,1,1,1) if self.state == "normal" else (.5,.5,.5,1))
        Ellipse:
            pos: self.pos
            size: self.size

<MainWindow>:
    tab_pos: 'left_mid'
    size_hint: .5, .5
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        addIngre: buttonAdd

        text: 'All'

        CircularButton:
            id: buttonAdd
            size_hint: .1, .1
            pos : 90, 90 #Here it doesn't work

    TabbedPanelItem:
        text: 'first tab'
        Label:
            text: 'First tab content area'


    TabbedPanelItem:
        text: 'tab2'
        BoxLayout:
            Label:
                text: 'Second tab content area'
            Button:
                text: 'Button that does nothing'

底层伙伴:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.vector import Vector 
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty

class CircularButton(ButtonBehavior, Widget):
    def __init__(self, **kwargs):
        super(CircularButton, self).__init__(**kwargs)

    def collide_point(self, x, y):
        return Vector(x, y).distance(self.center) <= self.width / 2

    def on_release(self):
        print("Ok")

class MainWindow(TabbedPanel):
    addIngre = ObjectProperty()
    pass

class ListApp(App):
    def build(self):
        return MainWindow()


if __name__ == '__main__':
    ListApp().run()

size_hint有效。我相信它给我的小部件10%的标签大小,但pos不会改变我的小部件的位置。我尝试使用pos,pos_hint,......但似乎没有任何效果,位置不适用于我的小部件

我是否必须在我的小部件中放置一个位置,并且要求这个位置而不是我在这里做什么?

python kivy kivy-language
1个回答
1
投票

Kivy » TabbedPanel

TabbedPanel小部件管理选项卡中的不同小部件,具有实际选项卡按钮的标题区域和用于显示当前选项卡内容的内容区域。

Solution

  1. 在TabbedPanelItem中添加FloatLayout
  2. 在FloatLayout中添加您的CircularButton
  3. 使用pos_hint:

Snippets

TabbedPanelItem:
    addIngre: buttonAdd
    text: 'All'

    FloatLayout:

        CircularButton:
            id: buttonAdd
            size_hint: .1, .1
            # pos: root.center
            # pos_hint: {'x': 0.9, 'y': 0.9}
            pos_hint: {'x': 0.3, 'y': 0.5}

Output

CircularButton inside TabbedPanelItem

© www.soinside.com 2019 - 2024. All rights reserved.