![](/img/trans.png)
[英]Size_hint and pos_hint of kivy not working when adding buttons programmatically in python
[英]Kivy: Camera does not respect pos_hint (but does respect size_hint)
我正在 Kivy 中編寫程序,但遇到了障礙。 相機對象(來自kivy.uix.camera
)響應size_hint
的變化,但不響應pos_hint
的變化。 對於 pos_hint,它總是將相機饋送集中在由 size_hint 繪制的假想框中(即,如果size_hint: (0.5, 0.5)
,則饋送將在 window 的左下半部分居中,無論 pos_hint 設置為什么)。
這是一個最小的可重現示例。 如果您想要不同size_hint
或pos_hint
值,請隨意更改給定的值:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.camera import Camera
from kivy.core.window import Window
Window.fullscreen = 'auto' # uses display's current resolution
class CameraStream(FloatLayout):
pass
class RootWidget(FloatLayout):
camera_stream = ObjectProperty(None)
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
class LifterApp(App):
def build(self):
self.root = RootWidget()
return self.root
if __name__ == '__main__':
LifterApp().run()
#:kivy 1.11.1
<CameraStream>:
camera_stream: camera_stream_id
Camera:
id: camera_stream_id
resolution: (320, 240)
play: True
<RootWidget>:
camera_stream: camera_stream_id
CameraStream:
id: camera_stream_id
size_hint: (0.5, 0.5) # this does work
#size_hint: (0.1, 0.1) # ...so does this
#size_hint: (1, 1) # ...and this
pos_hint: {'center_x': 0.1, 'center_y': 0.1} # NOTE this doesn't work
#pos_hint: {'center_x': 0.9, 'center_y': 0.9} # ...neither does this
#pos_hint: {'x': 0, 'y': 0} # ...or this
我想通了,所以我會分享我的解決方案。 我的代碼有兩個問題。 首先,我意識到kivy.uix.camera.Camera
是從kivy.uix.image.Image
派生的,所以我檢查了這些文檔,果然,他們說:
默認情況下,圖像居中並適合小部件邊界框。 如果您不想這樣,可以將 allow_stretch 設置為 True 並將 keep_ratio 設置為 False。
這解釋了小部件調整大小(即“想象框”),而不是實際的視頻源。 我遇到的另一個問題是我試圖在<RootWidget>
的CameraStream:
下設置所有屬性,而實際上我需要在<CameraStream>
的Camera:
下設置它們,因為它是我正在修改的 Camera object,不是包含它的 CameraStream FloatLayout。
解決這兩個問題使它按我的意願工作。 為了完整起見,這是我更新的 lifter.kv 文件(main.py 文件未更改):
#:kivy 1.11.1
<CameraStream>:
camera_stream: camera_stream_id
Camera:
id: camera_stream_id
resolution: (320, 240)
play: True
allow_stretch: True
keep_ratio: False
size_hint: (0.5, 0.5)
#size_hint: (0.1, 0.1)
#size_hint: (1, 1)
pos_hint: {'center_x': 0.1, 'center_y': 0.1}
#pos_hint: {'center_x': 0.9, 'center_y': 0.9}
#pos_hint: {'x': 0, 'y': 0}
<RootWidget>:
camera_stream: camera_stream_id
CameraStream:
id: camera_stream_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.