首页 > 解决方案 > 创建一个 kivy 应用程序以在一个屏幕上接收用户的输入并在另一个屏幕上显示这些图像

问题描述

我正在尝试创建一个应用程序,该应用程序将在我的 PC 中显示来自某个路径的图像。我想根据用户输入显示这些图像。假设用户给了一个数字 5,那么它将在该路径中随机显示 5 张图像。

标签: pythonfile-uploadpycharmkivykivymd

解决方案


这是一个基本的示例应用程序,它应该使用纯 python(无 KV 语言)完成您所要求的操作。只是说,在显示图像时,您可能需要考虑添加滚动视图以查看完整图像。目前,我通过指定size_hint(.3,.3). 此外,您想找到一种更好的方法来过滤掉不是图像的文件,我通过检查文件路径是否以“.jpg”或“.png”结尾来创建一个基本过滤器,您可以执行此正则表达式或使用全局库。

import os, random

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image
from kivy.properties import ListProperty, NumericProperty

class Screen2(Screen):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.layout = StackLayout()
        self.random_image_number = App.get_running_app().images_num
        self.images_paths = App.get_running_app().images_paths
        self.random_paths = random.choices(self.images_paths, k=self.random_image_number)

        for path in self.random_paths:
            # You may wanna change the size_hint or add a scrollview if you want full size images
            img = Image(source=path,size_hint=(.3,.3))
            self.layout.add_widget(img)

        self.add_widget(self.layout)

class Screen1(Screen):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Add your path here
        self.image_path = os.path.join("C:\\Users\\dimit\\Pictures")
        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 images to show", 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):
        App.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"):
                App.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
        App.get_running_app().screen_manager.add_widget(Screen2(name="Image Display Page"))

        # Changes screen to image display screen 
        App.get_running_app().screen_manager.current = "Image Display Page"

class ImageApp(App):
    images_paths = ListProperty([])
    images_num = NumericProperty(0)
    def build(self):
        self.title = "Image Display App"
        self.screen_manager = ScreenManager()
        self.screen_manager.add_widget(Screen1(name="Home Page"))
    
        return self.screen_manager


if __name__ == "__main__":
    ImageApp().run()
        

推荐阅读