【问题标题】:Kivy widget can't change posKivy 小部件无法更改位置
【发布时间】:2023-03-03 07:54:21
【问题描述】:

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

所以我这样做了: 基维文件:

#: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'

python 文件:

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】:

    Kivy » TabbedPanel

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

    解决方案

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

    片段

    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}
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多