Kivy 小部件无法更改 pos
Kivy widget can't change pos
我想在 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,...但似乎没有任何效果,位置不适用于我的小部件
我是否必须在我的小部件中放置一个位置,并调用这个位置而不是我在这里做的事情?
The TabbedPanel widget manages different widgets in tabs, with a
header area for the actual tab buttons and a content area for
showing the current tab content.
解决方案
- 在 TabbedPanelItem
中添加一个 FloatLayout
- 在 FloatLayout 中添加 CircularButton
- 使用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}
输出
我想在 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,...但似乎没有任何效果,位置不适用于我的小部件
我是否必须在我的小部件中放置一个位置,并调用这个位置而不是我在这里做的事情?
The TabbedPanel widget manages different widgets in tabs, with a header area for the actual tab buttons and a content area for showing the current tab content.
解决方案
- 在 TabbedPanelItem 中添加一个 FloatLayout
- 在 FloatLayout 中添加 CircularButton
- 使用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}