問題描述
我目前正在熟悉 kivy.我認為它有很大的潛力,但我確實發現普通 python"和 kv 語言之間的關系有點令人困惑,這使得很難理解在哪里做項目.目前在我看來,使用 python 與 kv-l 時的行為(幕后發生的事情)并不是一對一的,總的來說,我認為這使得可用性/生產力的標準相當高.
I'm at the moment getting familiar with kivy. I think it has great potential, but I do find the relationship between "normal python" and the kv-language a bit confusing which makes it hard to understand where to do items. At the moment it appears to me that the behaviour (the things that happens behind the scenes) is not one to one when using python vs kv-l, in general I think that makes the bar quite high for usability/productivity.
我和其他人一起使用了速成課程",這是獲得對 kivy 第一印象的良好開端.無論如何,在學習的過程中,我只是想看看我是否可以制作一個可滾動的 box-view - 結果我做不到.
I've amongst others used the "crash course" by inclement which is a great start to get a first impression of kivy. Anyway, in the process of learning I just wanted to see if I could make a box-view scrollable - it turned out that I could not.
需要什么才能使這段代碼正常工作,即將標簽擴展到它們的紋理大小",同時擁有一個可以調整的 ScrollView?
What's needed to make this code work, i.e. expand the labels to their "texture-size", and at the same time having a ScrollView that adjusts to that?
如果 BoxLayout 有 size_hint_y: None,則標簽不會擴展到文本,但是當窗口非常小時,可以看到滾動視圖.
If the BoxLayout has a size_hint_y: None, the labels are not expanded to the text, but the scrollview can be seen in action when making the window really small.
如果 BoxLayout 有 size_hint_y: 1,則標簽會展開,但顯然 boxlayout 的高度根本沒有改變,即滾動視圖窗口似乎與 size_hint_y: None 相同
If the BoxLayout has a size_hint_y: 1, the labels are expanded, but apparantly the height of the boxlayout does not change at all, i.e. the scrollview window seems to be the same as with size_hint_y: None
如果我只是輸入一個很大的高度,滾動視圖會覆蓋它,但我希望可以獲得與其內容耦合的 boxlayout 的動態高度.
If I just put in a height which is large, the scrollview covers this, but I would expect that it's possible to get a dynamic height of the boxlayout coupled to it's content.
我玩過高度、size_hints 等,但我沒有找到有效的組合,有時會收到警告說由于內部重繪循環需??要重新編寫代碼?
I've played around with heights, size_hints, etc. and I have not found a combination that works and sometimes get warnings that the code needs to be remade due to internal redrawing loops?
我錯過/不理解什么?
代碼如下.
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
Builder.load_string("""
<ScrollableLabel>:
BoxLayout:
orientation: 'vertical'
# size_hint_y: 1
size_hint_y: None
height: 400 #self.size[1]
canvas:
Color:
rgba: (1, 0, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
Label:
id: bust
text: 'a string that is long ' * 10
font_size: 50
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
canvas:
Color:
rgba: (0, 1, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
Label:
text: '2 strings that are long ' * 10
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
Button:
text: 'just testing'
""")
class ScrollableLabel(ScrollView):
pass
runTouchApp(ScrollableLabel())
推薦答案
BoxLayout 的設計目的是讓它的子元素填充自己.您想要的動態調整大小的更好布局是 GridLayout,它有一個可以綁定到自動調整大小的 minimum_height.
The BoxLayout is designed to make its children fill itself. A better layout for the dynamic resizing you want is the GridLayout, which has a minimum_height you can bind to for automatic resizing.
<ScrollableLabel>:
GridLayout:
cols: 1
size_hint_y: None
height: self.minimum_height
canvas:
Color:
rgba: (1, 0, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
Label:
id: bust
text: 'a string that is long ' * 10
font_size: 50
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
canvas:
Color:
rgba: (0, 1, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
Label:
text: '2 strings that are long ' * 10
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
Button:
text: 'just testing'
""")
這篇關于帶有boxlayout的kivy滾動視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!