首页 > 解决方案 > Kivy 无法访问 ID

问题描述

我正在使用 Kivy 模块开发日历应用程序。我的问题出在 print_label() 函数中。似乎他无法访问 id :

约会标签 = self.ids[label_id_file_content] TypeError: 'kivy.properties.DictProperty' 对象不可下标

谁能帮我解决这个问题?

谢谢你

朱利叶斯

Python代码:

#diable multitouch
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
from kivy.app import App
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.lang.builder import Builder
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout




class screenmanager(ScreenManager):
    pass

class PopupContent(FloatLayout):
    def store_appointment_name(self):
        appointment_name = self.ids.appointment_name.text
        with open("appointment_name_file","w") as appointment_name_file:
            appointment_name_file.write(appointment_name)
        with open("label_id_file","r") as label_id_file:
            label_id_file_content = label_id_file.read()

        if "ja" in label_id_file_content:
            JanuaryWindow.print_label(JanuaryWindow)



class MonthWindow(Screen):
    pass


class JanuaryWindow(Screen):
    def print_label(self):
        with open("appointment_name_file", "r") as appointment_name_file:
            appointment_name_file_content = appointment_name_file.read()
        with open("label_id_file", "r") as label_id_file:
            label_id_file_content = label_id_file.read()
        appointment_label = self.ids[label_id_file_content]
        appointment_label.text = appointment_name_file_content




kv = Builder.load_file("Calendar-KIVY.kv")

class Calendar(App):
    def create_popup_and_convert_button_id_in_label_id(self,button_id):
        with open("button_id_file","w+") as button_id_file:
            button_id_file.write(button_id)


        pcontent = PopupContent()
        new_appointment_p = Popup(title="Make new appointment",content=pcontent,size_hint=(None,None),size=(1500,1000))
        new_appointment_p.open()

        with open("button_id_file", "r") as button_id_file:
            button_id_file_content = button_id_file.read()

        label_id = button_id_file_content.replace("b", "l")

        with open("label_id_file", "w") as label_id_file:
            label_id_file.write(label_id)


    def build(self):
        return kv

Calendar().run()

Kv-代码:

#:import Factory kivy.factory.Factory

screenmanager:
    MonthWindow:
    JanuaryWindow:




<PopupContent>:
    Label:
        text: "Enter your appointment name"
        size_hint: 0.4,0.1
        pos_hint:{"x":0.1,"y":0.6}
    TextInput:
        id: appointment_name
        size_hint: 0.4,0.06
        pos_hint: {"x":0.478,"y":0.615}
        multiline: False
    Button:
        text: "Create"
        size_hint: 0.8,0.2
        pos_hint: {"x":0.1,"y":0.09}
        on_release:
            root.store_appointment_name()




<MonthWindow>:
    GridLayout:
        cols:4
        Button:
            text:"January"
            on_release:
                app.root.current = "JaW"
        Button:
            text:"February"
        Button:
            text:"March"
        Button:
            text:"April"
        Button:
            text:"May"
        Button:
            text:"June"
        Button:
            text:"July"
        Button:
            text:"August"
        Button:
            text:"September"
        Button:
            text:"October"
        Button:
            text:"November"
        Button:
            text:"December"




<JanuaryWindow>:
    name : "JaW"
    FloatLayout:
        Label:
            text:"January"
            font_size: "30sp"
            pos_hint: {"x":-0.426,"y":0.429}
    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":-0.4,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            text:"1"
        Label:
            text:"2"
        Label:
            text:"3"
        Label:
            text:"4"
        Label:
            text:"5"
        Label:
            text:"6"
        Label:
            text:"7"
        Label:
            text:"8"
        Label:
            text:"9"
        Label:
            text:"10"
        Label:
            text:"11"
        Label:
            text:"12"
        Label:
            text:"13"
        Label:
            text:"14"
        Label:
            text:"15"
        Label:
            text:""
    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":0,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            text:"16"
        Label:
            text:"17"
        Label:
            text:"18"
        Label:
            text:"19"
        Label:
            text:"20"
        Label:
            text:"21"
        Label:
            text:"22"
        Label:
            text:"23"
        Label:
            text:"24"
        Label:
            text:"25"
        Label:
            text:"26"
        Label:
            text:"27"
        Label:
            text:"28"
        Label:
            text:"29"
        Label:
            text:"30"
        Label:
            text:""
    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":0.32,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            text:"31"
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""


    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.843}
            size_hint: 0.02,0.03
            text: "+"
            on_release:
                app.create_popup_and_convert_button_id_in_label_id("1jab")
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.7885}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.734}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.6795}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.6795}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.625}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.5705}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.516}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.4615}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.4615}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.407}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.3525}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.298}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.2435}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.2435}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.188}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.1235}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.38,"y":0.07}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.843}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.7885}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.734}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.6795}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.6795}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.625}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.5705}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.516}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.4615}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.4615}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.407}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.3525}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.298}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.2435}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.2435}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.188}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.1235}
            size_hint: 0.02,0.03
            text: "+"
    FloatLayout:
        Button:
            pos_hint: {"x":0.7174,"y":0.07}
            size_hint: 0.02,0.03
            text: "+"



    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":-0.36,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            id:1jal
            text:""
        Label:
            id:2jal
            text:""
        Label:
            id:3jal
            text:""
        Label:
            id:4jal
            text:""
        Label:
            id:5jal
            text:""
        Label:
            id:6jal
            text:""
        Label:
            id:7jal
            text:""
        Label:
            id:8jal
            text:""
        Label:
            id:9jal
            text:""
        Label:
            id:10jal
            text:""
        Label:
            id:11jal
            text:""
        Label:
            id:12jal
            text:""
        Label:
            id:13jal
            text:""
        Label:
            id:14jal
            text:""
        Label:
            id:15jal
            text:""
        Label:
            text:""
    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":0,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            id:16jal
            text:""
        Label:
            id:17jal
            text:""
        Label:
            id:18jal
            text:""
        Label:
            id:19jal
            text:""
        Label:
            id:20jal
            text:""
        Label:
            id:21jal
            text:""
        Label:
            id:22jal
            text:""
        Label:
            id:23jal
            text:""
        Label:
            id:24jal
            text:""
        Label:
            id:25jal
            text:""
        Label:
            id:26jal
            text:""
        Label:
            id:27jal
            text:""
        Label:
            id:28jal
            text:""
        Label:
            id:29jal
            text:""
        Label:
            id:30jal
            text:""
        Label:
            text:""
    BoxLayout:
        orientation: "vertical"
        pos_hint: {"x":0.32,"y":-0.0015}
        size_hint: 0.9,0.89
        Label:
            id:31jal
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""
        Label:
            text:""

标签: pythonkivy

解决方案


你有没有试过把 self.ids 改成

self.root.ids['']

?


推荐阅读