【发布时间】:2021-07-14 14:03:53
【问题描述】:
这是定义一个可以垂直和水平滚动的 Kivy ScrollView 标签的代码:
from kivy.app import App
from kivy.config import Config
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
class ScrollablePopup(Popup):
contentBox = ObjectProperty()
scrollView = ObjectProperty()
def scrollToTop(self):
self.scrollView.scroll_y = 1 # force scrolling to top
def scrollToBottom(self):
self.scrollView.scroll_y = 0 # force scrolling to bottom
def initText(self, text):
self.contentBox.content.text = text
class ScrollPopup(BoxLayout):
popup = None
def openPopup(self):
self.popup = ScrollablePopup(title="Scrollable popup")
text = u"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus.\nFusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia.\nIn sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla.\nDonec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo.\nQuisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui.\nNulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor.\nCras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat.\nPellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis.\n"
for i in range(0, 4):
text += "\n\n" + text
self.popup.initText(text)
self.popup.open()
class ScrollPopupVertHorzApp(App):
def build(self): # implicitly looks for a kv file of name kivylistview1111.kv which is
# class name without App, in lowercases
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '500')
Config.write()
return ScrollPopup()
def on_pause(self):
# Here you can save data if needed
return True
def on_resume(self):
# Here you can check if any data needs replacing (usually nothing)
pass
if __name__ == '__main__':
ScrollPopupVertHorzApp().run()
scrollpopupverthorz,kv
<ScrollPopup>:
orientation: "vertical"
popupButton: popup_button
BoxLayout:
size_hint_y: None
Button:
id: popup_button
text: "Open scrollable popup"
size_hint_y: None
height: "40dp"
on_press: root.openPopup()
<ScrollablePopup>:
id: scr_popup
auto_dismiss: False
contentBox: content_box
scrollView: scroll_view
padding:10, 10
BoxLayout:
id: content_box
orientation: "vertical"
content: content_text
ScrollView:
id: scroll_view
effect_cls: "ScrollEffect" # prevents overscrolling
do_scroll_x: True # enabling horizontal scrolling
Label:
id: content_text
size_hint_x: None # required for horizontal scrolling to work. This
# comment is wrong. See the answer below !
size_hint_y: None # required for horizontal scrolling to work
height: self.texture_size[1] # required for vertical scrolling to work
width: self.texture_size[0] # Set the Label width to the text width.
# Requnred for horizontal scrolling to work
line_height: 1.5
valign: "top"
Button:
text: "Scroll to top"
size_hint_y: None
height: "40dp"
on_press: scr_popup.scrollToTop()
Button:
text: "Scroll to bottom"
size_hint_y: None
height: "40dp"
on_press: scr_popup.scrollToBottom()
Button:
text: "Close"
size_hint_y: None
height: "40dp"
on_press: root.dismiss()
我的问题是:如何在不分别剪切部分文本行或在每个文本行左侧添加空格的情况下将标签宽度设置为更小或更大的值?
【问题讨论】:
标签: python-3.x kivy scrollview