Как изменить изображение с помощью идентификаторов в kivy - PullRequest
0 голосов
/ 07 августа 2020

Как изменить значок профиля? Я хочу добавить в приложение функциональность, при которой при нажатии значка профиля появляется всплывающее окно, а затем вы можете выбрать другие значки по вашему выбору, чтобы они стали значком нового профиля. Всякий раз, когда я выбираю другие значки, получаю: [ERROR] [Image] Ошибка чтения файла <<strong> main .ImageButton объект в 0x000001610E195E48>

Вот код:

main .py

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.properties import StringProperty, ObjectProperty,NumericProperty
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout


class MainScreen(Screen, FloatLayout):
    mantra_text = ObjectProperty(None)

    def printMantra(self):
        print(self.ids.mantra_text.text)

    def icon_popup(self):
        popup = Popup(title="Profile Icon", content=Popup_Content(), size_hint=(None, None), size=(300, 200))
        popup.open()


class Popup_Content(FloatLayout):
    pass


class ImageButton(ButtonBehavior, Image):
    pass


class MainApp(App):
    def build(self):
        return MainScreen()

    def set_profile_icon(self, image):
        self.root.ids.profile_icon.source = str(image)
        print(image)
        #print(self.root.ids.profile_icon)


MainApp().run()

main.kv

#:import utils kivy.utils
<MainScreen>
    Popup_Content:
        id: popup_content

    FloatLayout:
        canvas:
            Color:
                rgb: utils.get_color_from_hex("#ffbb99")
            Rectangle:
                pos: self.pos
                size: self.size

        GridLayout:
            cols: 2
            pos_hint: {"x":0.6, "top":1}
            size_hint: 0.4,0.2
            spacing_horizontal: [0.9*root.width]
            Label:
                text: "Name"
            ImageButton:
                id: profile_icon
                source: "profile_icon"
                on_release: root.icon_popup()

        Label:
            text: mantra_text.text
            pos_hint: {"x":0, "top":0.8}
            size_hint: 1, 0.2
            text_size: self.size
            halign: "center"
            font_size: 25
        TextInput:
            id: mantra_text
            pos_hint: {"x": 0.15, "top":0.6}
            size_hint: 0.7, 0.1
            #text_size: self.size

        Label:
            text: "Time"
            pos_hint: {"x":0.3, "top":0.6}
            size_hint: 0.4, 0.2
            text_size: self.size
            halign: "left"
            font_size: 30

        Button:
            text: "Time"
            pos_hint: {"x":0.3, "top":0.5}
            size_hint: 0.4, 0.2
            on_release: root.printMantra()

<Popup_Content>

    #profile_icon: profile_icon
    FloatLayout:
        GridLayout:
            cols: 5
            pos_hint: {"x":0.95, "y":1.6}

            ImageButton:
                id: man_01
                source: "icons/male_icon_01.png"
                on_release: app.set_profile_icon(man_01)
            ImageButton:
                id: man_02
                source: "icons/male_icon_02.png"
                on_release: app.set_profile_icon(man_02)
            ImageButton:
                source: "icons/male_icon_01.png"
                on_release: app.set_profile_icon()
            ImageButton:
                source: "icons/male_icon_01.png"
                on_release: app.set_profile_icon() #these are empty because they are stand in for the other icons
            ImageButton:
                source: "icons/male_icon_01.png"
                on_release: app.set_profile_icon()
            ImageButton:
                id: female_01
                source: "icons/female_icon_01.png"
                on_release: app.set_profile_icon(female_01)


1 Ответ

1 голос
/ 07 августа 2020

Проблема

В вашем приложении возникла ошибка, потому что оно пытается назначить экземпляр изображения источнику.

Решение

Замените в main.py str(image) с image.source.

Выход

введите описание изображения здесь

...