在KIVY上,更新标签文本不会更新

问题描述 投票:0回答:1

虽然该行代码成功执行,但文本字段并未更新。 “data_PV”变量被初始化为“toto”,并且“toto”确实被写入该字段中。但“data_PV”的修改并不会导致KIVY屏幕上文字的修改。 Print 函数确实显示变量在 Python 中已更新

我尝试通过删除 KV 文件中的“text:root.data_PV”行来直接写入 Python 中的文本字段(2 行注释)。结果是一样的。

import ctypes
import sys
import time

from kivy.config import Config
Config.set('graphics','resizable', False)
#S9 2960 x 1440
n = 3
Config.set('graphics','width', str(int(1440/n)))
Config.set('graphics','height', str(int(2960/n)))
import kivy
from kivy.uix.popup import Popup
from kivy.app import App, StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.progressbar import ProgressBar
from kivy.properties import ObjectProperty
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen 
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import threading
from kivy.clock import mainthread

class MainWindow(Screen):
    pass

class PompeSurf1Window(Screen):
    freq_SP = ObjectProperty(TextInput)
    data_PV = StringProperty("toto")
        
    def update(self, loc_data,*a):
        self.data_PV = str(loc_data)
        print("valeur data:", self.data_PV)
        #self.freq_SP.text= str(self.data_PV)
        #self.ids.label_freq_PV.text=self.data_PV
        
    def pub_freq_SP(self):
        for i in range(1):
            text = "at#rtuw=1,6,8193,"+str(int(self.freq_SP.text)*100)
            publish.single("set", text, qos=0, retain= False, hostname="test.mosquitto.org")

class WindowManager(ScreenManager):
    pass    

kv = Builder.load_file('main.kv')

class LoivesApp(App):
    client = mqtt.Client()
    
    @mainthread
    def on_message(self,client, userdata, message):
        data=message.payload.decode("utf-8")
        print(data)
        instance1 = PompeSurf1Window()
        instance1.update(data)
           
    def build(self):
        client = mqtt.Client()
        self.client.on_message=self.on_message 
        self.client.connect('test.mosquitto.org',1883)
        self.client.loop_start()
        self.client.subscribe([("statusword2",0)])
        return kv
    
    def on_pause(self):
        pass
        
    def on_resume(self):
        self.client.on_message=self.on_message 
        self.client.connect('test.mosquitto.org',1883)
        self.client.loop_start()
        self.client.subscribe([("statusword2",0)])

        
if __name__ == "__main__":
    LoivesApp().run()

WindowManager:
    MainWindow:
    PompeSurf1Window:
    
<MainWindow>:
    name: "main"
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        spacing: 20
        padding: 20
        Label:
            text: "Ferme des Loives"
            font_size: 100
        Button:
            text: "Pompe Surface 1"
            font_size: 50
            size_hint: 0.8, 0.8
            pos_hint: {"x":0.1, "top":0.9}
            on_release:
                app.root.current = "Pompe1"
                root.manager.transition.direction = "left" # peut être right up down   
               
<PompeSurf1Window>:
    name: "Pompe1"
    spacing: 20
    padding: 20
    freq_PV: label_freq_PV
    freq_SP: input_freq_SP
    GridLayout:
        resizable: False
        cols: 1
        size: root.width, root.height
        spacing: 20
        padding: 20
        Label:
            size_hint_y: None
            height: 20 
        Label:
            text: "Pompe de Surface 1"
            font_size: 100
            size_hint_y: None
            height: 50    
        GridLayout:
            cols: 2
            Label:
                text: "Fréquence actuelle"
                font_size_hint: None
                font_size: 32
            Label:
                text: "Consigne de fréquence"
                font_size_hint: None
                font_size: 32
            
            Label:
                id: label_freq_PV
                text: root.data_PV
                font_size_hint: None
                font_size: 32

            TextInput:
                id: input_freq_SP
                font_size: 32
                multiline: False
                on_text_validate: root.pub_freq_SP()
            
        Button:
            text: "retour"
            size_hint: 0.8, 0.2
            pos_hint: {"x":0.1, "top":0.9}
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "right"
kivy label updates
1个回答
0
投票

问题出在你的

on_message()
方法上。线路:

instance1 = PompeSurf1Window()

正在创建

PompeSurf1Window
的新实例,该实例与应用程序显示中的
PompeSurf1Window
实例无关。因此,调用该新实例的任何方法都不会影响您的应用程序显示。修复方法是实际使用应用程序显示中的
PompeSurf1Window
实例。这是该方法的修改版本,可以满足您的要求:

def on_message(self, client, userdata, message):
    print('in on_message')
    data = message.payload.decode("utf-8")
    # instance1 = PompeSurf1Window()
    instance1 = self.root.get_screen('Pompe1')
    instance1.update(data)
© www.soinside.com 2019 - 2024. All rights reserved.