首页 > 解决方案 > 更新主页上的值 - Kivy

问题描述

怎么样,伙计们。我已经找了两天了,我似乎无法连接这些点。请原谅我对python有些经验,但OOP概念仍然有点不清楚。我看过几个类似的帖子,但蟒蛇有点过头了。

我想要做的是在一个单独的屏幕(称为设置温度的按钮)中设置温度(最终是湿度)。然后让该屏幕上的设置温度值显示在我的主屏幕上。我试图将温度值与全局变量(tep)联系起来。然后,当一个人点击主菜单按钮(在设置的临时屏幕内)并调用主屏幕类中的一个方法时,该方法将引用该全局tep变量。但是当我这样做时,什么也没有发生。我已经厌倦了通过 ID 调用它,在主菜单中创建一个方法来显式更新该变量但无济于事,反之亦然,但似乎没有任何效果。我尝试查看如何刷新屏幕,但这些示例超出了我的想象。如果有人能对这个问题有所了解,那就太棒了。

感谢您花时间阅读我的帖子!

蟒蛇文件

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from random import randint
from kivy.lang import Builder  
from kivy.uix.screenmanager import ScreenManager, Screen

Window.size = (600,250)

tep = 75
# Window Manager Class
class WindowManager(ScreenManager):
    pass

# Main home menu Class
class MyTerrLayout(Screen):
    internal_read = ObjectProperty(None)
    external_read = ObjectProperty(None)
    target_temp = ObjectProperty(None)
    target_hum = ObjectProperty(None)

    def set_temp(self):
        global tep
        self.ids._Target_Temp.text = str(tep)
        print(str(tep))

# Temprature Screen Class / Window  
class TempWindow(Screen):
    temp_menu = ObjectProperty(None)
    def sub_temp(self):
        global tep
        tep = tep - 1
        self.temp_menu.text = str(tep)

    def add_temp(self):
        global tep
        tep = tep + 1
        self.temp_menu.text = str(tep)
    def set_temp(self):
        MyTerrLayout().set_temp()

# Humidity Class / Window
class HumWindow(Screen):   
    pass


# Builder Section
kv = Builder.load_file('terrlayoutV1.kv')

class TerrLayout(App):
    def build(self):
        return kv

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

基维文件

#:kivy 1.0.9
WindowManager:
    MyTerrLayout:
    TempWindow:
    HumWindow:

<MyTerrLayout>:
    name: "main"
    internal_read: internal_reading
    external_read: external_reading
    target_temp: _Target_Temp
    target_hum: _Target_Hum

    GridLayout:
        cols:2
        RelativeLayout:
            Label:
                id: _internal
                text: 'Internal Temp/Humidity'
                size_hint: 0.5 , .166
                pos_hint: {'x': .25, 'top': 1}
            Label:
                text: 'Target:'
                font_size: 15
                size_hint: 0.125 , .166
                pos_hint: {'x': 0.1, 'top': 0.834}   
            Label:
                id: _Target_Temp
                size_hint: 0.125 , .166
                pos_hint: {'x': .25, 'top': 0.834}
            Label:
                id: _Target_Hum
                size_hint: 0.125 , .166
                pos_hint: {'x': .38, 'top': 0.834}
            Label:
                text: 'Current:'
                size_hint: 0.125 , .166
                pos_hint: {'x': .55, 'top': 0.834}
            Label:
                id: internal_reading
                text: '25C/35%'
                size_hint: 0.125 , .166
                pos_hint: {'x': .75, 'top': 0.834}
            Label:
                id: _external
                text: 'External Temp/Humidity'
                size_hint: 0.5, .166
                pos_hint: {'x': .25, 'top': 0.668}
            Label:
                id: external_reading    
                size_hint: 0.5, .166
                pos_hint: {'x': .25, 'top': 0.502}
            Label:
                id: _heatbed
                text: 'Heatbed Temprature'
                size_hint: 0.5, .166
                pos_hint: {'x': 0.25, 'top': 0.336}
            Label:
                id: heatbed_reading
                text: '80C'
                size_hint: 0.5, .166
                pos_hint: {'x': 0.25, 'top': 0.17}
        StackLayout:
            orientation: 'rl-tb'
            spacing: 1.5
            Button:
                id: _Set_Temp
                text: 'Set Temp'
                size_hint: [.5, .25]
                on_release: app.root.current = "Temp"
            Button:
                id: _Set_Hum
                text: 'Set Humidity'
                size_hint: [.5, .25]
                on_release: app.root.current = "Hum"
            Button:
                id: _Set_Fan
                text: 'Set Fan Time'
                size_hint: [.5, .25]
            Button:
                id: _Set_Light
                text: 'Set Light Time'
                size_hint: [.5, .25]
            Button:
                id: _Tog_Light
                text: 'Toggle Light'
                size_hint: [.5, .25]
            Button:
                id: _Tog_Fan
                text: 'Toggle Fan'
                size_hint: [.5, .25]
            Button:
                id: _Dis_Mode
                text: 'Display Mode'
                size_hint: [1, .25]



<TempWindow>:
    name: "Temp" 
    temp_menu: set_tep_menu
    GridLayout:
        cols: 2
        Label:
            id: set_tep_menu
            size_hint: 1, .5
            font_size: 32
        StackLayout:
            orientation: 'rl-tb'
            Button:
                text: "Increase Temp"
                on_press: root.add_temp()
                size_hint: [1, .3]
            Button:
                text: "Decrease Temp"
                on_press: root.sub_temp()
                size_hint: [1, .3]
            Button:
                text: "Main Menu"
                on_release: app.root.current = "main"
                on_release: root.set_temp() #calling method to set temp
                size_hint: [1, .3]

<HumWindow>:
    name: "Hum"
    Button:
        text: "Humidity"
        on_release: app.root.current = "main"


标签: pythonkivykivy-language

解决方案


你的代码:

def set_temp(self):
    MyTerrLayout().set_temp()

正在创建一个新实例MyTerrLayout并调用set_temp()该实例。但是,该新实例MyTerrLayout不是您的 GUI 的一部分,因此看不到任何更改。要实际更改您的 GUI,您需要调用 GUI中实例的set_temp()方法。MyTerrLayout为此,请将set_temp()方法更改TempWindow为:

def set_temp(self):
    terrLayout = self.manager.get_screen('main')
    terrLayout.set_temp()

推荐阅读