python - 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:""
解决方案
你有没有试过把 self.ids 改成
self.root.ids['']
?
推荐阅读
- types - 我如何获取feetype的ID列表,后跟学生ID
- css - 从菜单和链接中删除下划线
- ibm-integration-bus - 如何从属性文件中读取数据并将其存储在 ibm mb 的缓存节点中
- python - 在 OpenCV 和/或 PIL 中叠加两个图像时出错
- android - 如何将附加音频文件上传到 Firebase?
- git - 也许从 GIT 项目运行命令“git lfs install”
- java - 使用 selenium 在 java 中使用 tesng 进行并行测试
- java - 无法在 Java 中运行用于 websocket 实现的客户端端点代码(找不到类文件错误)
- java - javax.imageio.IIOException 读取输入文件时
- emacs - 在 Emacs 中以特定宽度设置连续的新帧