Python Kivy标签文本不更新

Python Kivy标签文本不更新,python,kivy,Python,Kivy,目前,我正在与Kivy一起编写日历程序。我第二次写这个问题,因为第一次我无法解决这个问题,而且代码太长了,实际问题中有不必要的行 我的问题是打印到标签功能。我在函数末尾编写了appointment\u label.text=appointment\u name\u file\u内容,但标签文本仅在我重新启动程序时才会更新。如果您想知道label\u id\u file\u content代表标签的id“1jal” 谢谢你的帮助,朱利叶斯 Python: #diable # diable mul

目前,我正在与Kivy一起编写日历程序。我第二次写这个问题,因为第一次我无法解决这个问题,而且代码太长了,实际问题中有不必要的行

我的问题是打印到标签功能。我在函数末尾编写了appointment\u label.text=appointment\u name\u file\u内容,但标签文本仅在我重新启动程序时才会更新。如果您想知道label\u id\u file\u content代表标签的id“1jal”

谢谢你的帮助,朱利叶斯

Python:


#diable # 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
from kivy.clock import Clock


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)

        Ja = JanuaryWindow()
        Ja.__init__()


class JanuaryWindow(Screen):
    def __init__(self, **kwargs):
        super(JanuaryWindow, self).__init__(**kwargs)
        Clock.schedule_once(self.print_appointment_to_label, .5)

    def print_appointment_to_label(self,dt):
        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 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)

        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_popup(self):
        contentp = PopupContent()
        popup = Popup(title="Make new appointment",content=contentp,size_hint=(None,None),size=(1500,1500))
        popup.open()


    def build(self):
        return kv


Calendar().run()

千伏:

屏幕管理器:
一月窗:
:
标签:
文本:“输入您的约会名称”
尺寸提示:0.4,0.1
pos_提示:{“x”:0.1,“y”:0.6}
文本输入:
id:约会名称
尺寸提示:0.4,0.06
pos_提示:{“x”:0.478,“y”:0.615}
多行:False
按钮:
文本:“创建”
尺寸提示:0.8,0.2
pos_提示:{“x”:0.1,“y”:0.09}
发布时:
root.store\u约会\u name()
:
姓名:“下巴”
浮动布局:
按钮:
pos_提示:{“x”:0.38,“y”:0.843}
尺寸提示:0.02,0.03
正文:“+”
发布时:
应用程序转换标签中的按钮id(“1jab”)
app.build_popup()
标签:
id:1日航
pos_提示:{“x”:0.3,“y”:0.4}
大小提示:1,1
案文:“”

问题在于您的
存储约会名称()
方法执行以下操作:

    Ja = JanuaryWindow()
    Ja.__init__()
这些行正在创建一个新的
JanuaryWindow
实例,并调用它的
\uuuu init\uuuu()
方法。首先,新创建的
JanuaryWindow
实例不是应用程序GUI中的实例,因此对该实例所做的任何更改都不会对GUI产生影响。顺便说一句,当您执行
JanuaryWindow()
时,会调用
\uuuuu init\uuuu()
方法,因此您不需要显式调用
\uuuuuu init\uuuuuo()
方法

因此,您要做的是访问GUI中的
JanuaryWindow
。要执行此操作,您可以修改
store\u appointment\u name()
方法,如下所示:

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)

        Ja = App.get_running_app().root.get_screen('JaW')
        Ja.print_appointment_to_label(0)
上面的代码使用
App.get\u running\u App()
方法获取对
App
的引用。然后
.root
访问
ScreenManager
,而
get_屏幕('JaW')
获取应用程序GUI中的
JanuaryWindow
。下一行只调用
print\u appointment\u to\u label()
方法

您所说的“标签文本仅在我重新启动程序时才更新”是什么意思?
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)

        Ja = App.get_running_app().root.get_screen('JaW')
        Ja.print_appointment_to_label(0)