python - 滚动查看多个输入到一个变量
问题描述
我正在使用 Kivy 在 python 中构建照片查看器应用程序。简要介绍一下,它接受用户的输入并随机显示来自我电脑中特定路径的图像。例如,用户输入 10,然后它会走 PC 中的特定路径并随机选择该路径中的图像并将它们显示在另一个屏幕上。
代码:
class MainScreen(Screen):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.image_path = os.path.join("C:\\Users\\harsh\\PycharmProjects\\pythonProject1") # Add your path here
self.layout = RelativeLayout()
self.input_bar = TextInput(multiline=False, size_hint=(0.4, 0.1), pos_hint={"center_x": .5, "center_y": .5},
hint_text="How many sections you have", input_type="number")
self.input_bar.bind(on_text_validate=self.get_images)
self.layout.add_widget(self.input_bar)
self.add_widget(self.layout)
# inst refers to the input bar object
def get_images(self, inst):
MDApp.get_running_app().images_num = int(inst.text)
for fname in os.scandir(self.image_path):
# Skip any directories
if fname.is_dir():
continue
# Filter for images
if fname.path.endswith(".jpg") or fname.path.endswith(".png"):
MDApp.get_running_app().images_paths.append(fname.path)
# This line needs to be here, because if you add the screen in the build method, it wont add the images
MDApp.get_running_app().screen_manager.add_widget(ImageScreen(name="Image Display Page"))
# Changes screen to image display screen
MDApp.get_running_app().screen_manager.current = "Image Display Page"
class ImageScreen(Screen):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.layout = GridLayout(cols=1, spacing=10)
self.random_image_number = MDApp.get_running_app().images_num
self.images_paths = MDApp.get_running_app().images_paths
self.random_paths = random.choices(self.images_paths, k=self.random_image_number)
for path in self.random_paths:
sv = ScrollView()
img = Image(source=path)
self.layout.add_widget(img)
self.add_widget(self.layout)
class DemoApp(MDApp):
images_paths = ListProperty([])
images_num = NumericProperty(0)
def build(self):
self.theme_cls.primary_palette = 'Green'
self.theme_cls.primary_hue = 'A700'
self.theme_cls.theme_style = 'Dark'
self.title = "Wise Time"
self.screen_manager = ScreenManager()
self.screen_manager.add_widget(MainScreen(name="main"))
self.screen_manager.add_widget(ImageScreen(name="image"))
return self.screen_manager
DemoApp().run()
帮我实现可滚动功能。我想要实现的是能够滚动适合窗口大小的所有图像。我已经尝试列出图像显然它不起作用。
解决方案
推荐阅读
- docker - 将带有端口的 URL 映射到没有 Docker 端口的 URL?
- mysql - 编写 MySQL 查询以获得测试表的预期结果
- android - 如何将 Kotlin Multiplatform Mobile 与 Amazon 放大服务集成?
- python - 键属性的 AttributeValue 不能包含空字符串值
- mysql - MYSQL如何去除重复项
- matlab - 有没有办法在 matlab 中输出类似于 LaTex 中的(符号)矩阵方程?
- prolog - 在 Prolog 中创建元变量
- c# - 以对象不为空为条件设置对象的属性
- javascript - 在jquery(javascript)中动态设置最大日期
- javascript - 我无法运行条件 IF