Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy:self.ids不更新标签文本_Python_Kivy_Label - Fatal编程技术网

Python Kivy:self.ids不更新标签文本

Python Kivy:self.ids不更新标签文本,python,kivy,label,Python,Kivy,Label,我想使用Kivy时钟每秒更新标签中的文本 当按下“TimeDisplay”中的按钮时,我希望时钟调用类“Display”,并在每次调用时更新“Display”中标签中的文本 以下是我的Python代码: from kivy.app import App from kivy.lang import Builder from kivy.core.window import Window from kivy.uix.screenmanager import ScreenManager, Screen

我想使用Kivy时钟每秒更新标签中的文本

当按下“TimeDisplay”中的按钮时,我希望时钟调用类“Display”,并在每次调用时更新“Display”中标签中的文本

以下是我的Python代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
import time
from kivy.properties import StringProperty
import threading
from kivy.clock import Clock
from kivy.uix.label import Label

Window.size = (600, 400)

class WindowManager(ScreenManager):  # class used for transitions between windows
   pass

class TimeDisplay(Screen):
   def on_press(self):
       display = Display()
       Clock.schedule_interval(display.display_time, 1)

class Display(Screen):
   def display_time(self, *args):
       print(self.ids)
       self.current_time = StringProperty()
       self.current_time = time.strftime("%H:%M:%S") #provides local time as a structure format of minutes followed by seconds
       self.ids.time.text = str(self.current_time)


class MyApp(App):
   def build(self):
       layout = Builder.load_file("layout.kv")  # loads the kv file
       return layout


if __name__ == "__main__":
   MyApp().run()
这是kivy的代码:


WindowManager:
#sets up the different screens for the app and the default order that they run in
    transition: NoTransition() #sets all transitions as 'NoTransition'
    TimeDisplay:
    Display:
    #AlarmDisplay:


<Button>:
    background_normal: "" #sets all buttons default background colour to white


<TimeDisplay>
    Button:
        opacity: 0
        on_press:
            root.on_press()
            root.manager.current = "display"

<Display>
    name: "display"

    FloatLayout:

        Label:
            id: time
            color: (1,0.5,0.5,1)
            size_hint: (0.5, 0.5)
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            opacity: 1
            text: ""
            font_size: 35
            color: ((83/255),(83/255),(83/255),1) #sets the colour of the text (rgba)

WindowManager:
#设置应用程序的不同屏幕及其运行的默认顺序
转换:NotTransition()#将所有转换设置为“NotTransition”
时间显示:
显示:
#报警显示:
:
背景颜色:正常:“将所有按钮的默认背景颜色设置为白色
按钮:
不透明度:0
新闻界:
root.on_press()
root.manager.current=“显示”
名称:“显示”
浮动布局:
标签:
id:时间
颜色:(1,0.5,0.5,1)
尺寸提示:(0.5,0.5)
pos_提示:{“center_x”:0.5,“center_y”:0.5}
不透明度:1
案文:“”
字体大小:35
颜色:((83/255),(83/255),(83/255),1)#设置文本的颜色(rgba)

为什么
self.ids.time.text
不使用id
time
更新标签的文本?

您的
on\u press()方法中的代码行:

   display = Display()
创建
Display
类的新实例。此新实例不是GUI中显示的实例,因此对其所做的任何更改都不会显示在GUI中。必须引用GUI中实际存在的
Display
实例。您可以将上述行替换为:

display = App.get_running_app().root.get_screen('display')

非常感谢。现在可以了