self.ids 不改变按钮的文字 |基维

self.ids not changing text of button | kivy

import matplotlib.pyplot as plt
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from numpy import linspace as linspace
from numpy import arange as arange
import sympy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

def formatted(f):
    return float(format(f, '.2f').rstrip('0').rstrip('.'))

class GraphWindow(Screen):
    def graph(self):
        global m
        global b
        global bs
        m = int(self.ids.mvalue.text)
        b = int(self.ids.bvalue.text)

        if i == 1:
            plot(m, b)


class ResultWindow(Screen):
    def displayresult(self):
        out = f"out: \n y = {m}x{bs}"
        print(out)
        self.ids.out.text = out


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("graph.kv")


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


graphApp().run()

Python脚本^

<ResultWindow>:
    name: "result"

    BoxLayout:
        orientation: "vertical"

        Button:
            id: out
            text: 'error'

kv文件^

我在网上看过但找不到解决方案,问题是在“resultwindow”中我尝试更改按钮的文本,但它不会更新有关该按钮的任何内容。

我不得不删除大部分 python 脚本,因为它不允许我 post。 https://pastebin.com/Ap6Vdu90

问题是您正在 ResultWindow 的另一个实例中更改 text 而不是您添加到 root 的实例中的 text

因此,为了获得预期的结果,您必须从 root 小部件访问 ResultWindow,您可以通过 [=24= 的静态方法 get_running_app 获取它] App.

因此您可能需要的更改是,

class ResultWindow(Screen):
    def displayresult(self):
        out = f"out: \n y = {m}x{bs}"
        print(out)
        app = App.get_running_app() # Accessing instance of currently running app.
        out_btn = app.root.get_screen("result").ids.out # Accessing the root, required screen and ids of that screen.
        out_btn.text = out