可滚动动态生成的弹出窗口



我试图在kivy中动态创建一个可滚动的弹出窗口(没有kv文件)。我的目标有两个。

  1. 让弹出窗口滚动,如果popup_label有太多的文本

  2. 使popup_label文本自动换行并使用自己的空间,如果文本太大,则不会溢出到其他小部件。

问题:

我一直在尝试下面的代码的许多变化,但我不能得到弹出滚动。正如你所看到的屏幕截图所示,我已经让文本开始超出弹出窗口的顶部和小部件之间的大空间,当在手机上查看时,白色的popup_label文本都聚集在一起,溢出了蓝色的标题标签。

有什么建议吗?PS:我是自学python和kivy的,这是我的第一个应用程序,所以请用可以理解的术语回答:)IE: x - y表示x在x/cos(*%s, %d)中会超过我的头:)

目前的相关代码片段:

class LocationPopupMenu(Popup):
"""
Add a popup dialog box when a user clicks an employers location pin
This is called from "EmployerMarker.py
"""
def __init__(self, user_data):
self.user = user_data
super().__init__()
root = App.get_running_app()
regotype = root.registration_type
measurement = root.firebase_user['measurement']
# Set all the required fields of current_user data
# Update this list with matching data you want to add to the popup
if regotype == "Employer":
headers = "Preferred Catagories : ,Preferred Positions : ,Distance Away : "
else:
headers = "Hiring Catagories : ,Positions : ,Distance Away : "
headers = headers.split(',')
# Generate a Gridlayout for the popup
layout = GridLayout(cols=1, padding=5, spacing=5, size_hint_y=None)
# attempt to bind like i would in a kv file IE : height: self.minimum_height
layout.bind(minimum_height=layout.setter("height"))
scrollLayout = ScrollView(orientation='vertical', scroll_type=['content'])
layout.add_widget(scrollLayout)
closeButton = None
attribute_value = ""
for i in range(len(headers)):
# iterate over the attributes of the current_user
attribute_name = headers[i]
if i == 0:
attribute_value = user_data['position_categories']
if i == 1:
attribute_value = user_data['position_sub_categories']
if i == 2:
attribute_value = f"{user_data['distance']} {measurement}"
# Add the attributes to a label then add the label widget to the layout
# add the blue header labels
popup_header = MDLabel(text=f"{attribute_name}", font_size="22sp", text_size=(self.width, None), halign="left", valign="center", size_hint_y=None, theme_text_color="Custom", text_color=root.theme_cls.accent_color)
popup_header.bind(height=popup_header.setter("texture_size"))
# Add the white labels
popup_label = MDLabel(text=f"{attribute_value}", font_size="22sp", text_size=(self.width, None), halign="left", valign="top", size_hint_y=None)
# attempt to bind texture_size as i would for kv file IE: height: self.texture_size[1] ?
# so that if popup_label is a large amount of text it doesnt just display over the top of
# other widgets, it displays inside its own allocated grid
popup_label.bind(height=popup_label.setter("texture_size"))
layout.add_widget(popup_header)
layout.add_widget(popup_label)
# Add a button to the bottom of the popup
closeButton = MDRoundFlatButton(text="Close me", opposite_colors=True, font_size="17sp", halign="center", valign="center", size_hint_y=None, height="40dp", opacity=1, line_color=root.theme_cls.primary_dark, text_color="yellow", on_release=self.dismiss)
profileButton = MDRoundFlatButton(text="View Profile", opposite_colors=True, font_size="17sp", halign="center", valign="center", size_hint_y=None, height="40dp", opacity=1, line_color=root.theme_cls.primary_dark, text_color=root.theme_cls.accent_color, on_release=self.viewProfile)
if regotype == "Employer":
layout.add_widget(profileButton)
layout.add_widget(closeButton)
# set the attributes to the popup class
setattr(self, "title", f"{user_data['registration_type']} Details")
setattr(self, "content", layout)
setattr(self, "auto_dismiss", False)

和在Linux上运行时的屏幕截图

问题是您正在将ScrollView添加到GridLayout。应该是相反的。试着改变:

layout.add_widget(scrollLayout)

:

scrollLayout.add_widget(layout)

和变化:

setattr(self, "content", layout)

:

setattr(self, "content", scrollLayout)

或:

self.content = scrollLayout

最新更新