如何在其他屏幕上显示输入?

时间:2019-07-07 11:36:50

标签: python kivy

我正在尝试编写多屏幕代码来计算油漆房间所需的油漆量。

有一个屏幕,我要显示上一个屏幕的用户输入。

class MainWindow(Screen):
    panjang= float
    lebar= float
    tingi= float
    luas1 = (panjang*tinggi)/10
    luas2= (lebar*tinggi)/10
    cat= luas1*2+luas2*2

    def logOut(self):
        sm.current = "create"

    def on_enter(self, *args):
        self.panjang.text = "Panjang: " + panjang
        self.lebar.text = "Lebar: " + lebar
        self.tinggi.text = "Tinggi: " + tinggi

KV文件:

<MainWindow>:
    panjang:panjang
    lebar: lebar
    tinggi:tinggi

    FloatLayout:
        Label:
            id: panjang
            pos_hint:{"x": 0.1, "top":0.9}
            size_hint:0.8, 0.2
            text: "Panjang: "

        Label:
            id: lebar
            pos_hint:{"x": 0.1, "top":0.7}
            size_hint:0.8, 0.2
            text: "Lebar: "

        Label:
            id: tinggi
            pos_hint:{"x": 0.1, "top":0.5}
            size_hint:0.8, 0.2
            text: "Tinggi:"

        Button:
            pos_hint:{"x":0.2, "y": 0.1}
            size_hint:0.6,0.2
            text: "Semula"
            on_release:
                app.root.current = "create"
                root.manager.transition.direction = "down"

将不会显示输入内容,并且会显示一条错误消息,指出“未定义panjang”。我怎样才能解决这个问题?从我的编码来看,可以计算出luas1、2和cat吗?

编辑: 我只是意识到这些行出现在错误消息中:

 78:
      79:<MainWindow>:
 >>   80:    panjang:panjang
      81:    lebar: lebar
      82:    tinggi:tinggi

然后我尝试添加空间并添加其他类似这样的变量:

panjang: panjang
    lebar: lebar
    tinggi: tinggi
    luas1: luas1
    luas2: luas2
    cat: cat

然后,这些行出现了:

 2:    name: "create"
       3:
 >>    4:    panjang: panjang
       5:    lebar: lebar
       6:    tinggi: tinggi

为什么仅在该变量中有错误?

Edit2:

完整的python编码:

# main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase
from kivy.properties import NumericProperty


class CreateAccountWindow(Screen):
    panjang = ObjectProperty(None)
    lebar = ObjectProperty(None)
    tinggi = ObjectProperty(None)

    def submit(self):
        if self.tinggi.text !="":
            try:
                if float(self.tinggi.text) and float(self.lebar.text) and float(self.panjang.text):

                    sm.current = "main"

            except:
                invalidForm()

        else:
            invalidForm()


    def login(self):
        self.reset()
        sm.current = "main"

    def reset(self):
        self.panjang.text = ""
        self.lebar.text = ""
        self.tinggi.text = ""

class MainWindow(Screen):
    # Each screen has by default a property manager that gives 
    # you the instance of the ScreenManager used.

    # declare class attributes
    panjang = ObjectProperty(None)
    lebar = ObjectProperty(None)
    tingi = ObjectProperty(None)

    # declare and initialize to 0.0 (float)
    luas1 = NumericProperty(0.0)
    luas2 = NumericProperty(0.0)
    cat = NumericProperty(0.0)

    def logOut(self):
        self.manager.current = "create"

    def volume(self):
        self.luas1 = float(self.manager.ids.create.panjang.text) * float(self.tinggi) / 10
        self.luas2 = (self.lebar * self.tinggi) / 10
        self.cat = self.luas1*2 + self.luas2*2

    def on_enter(self, *args):
        self.volume()    # calculate volume
        self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
        self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
        self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text
        self.luas1.text = "Luas:" + self.manager.ids.main.luas1.text
        self.luas2.text = "Luas:" + self.manager.ids.main.luas2.text
        self.cat.text = "Luas:" + self.manager.ids.main.cat.text


class WindowManager(ScreenManager):
    pass


def invalidLogin():
    pop = Popup(title='Invalid Login',
                  content=Label(text='Invalid username or password.'),
                  size_hint=(None, None), size=(400, 400))
    pop.open()


def invalidForm():
    pop = Popup(title='Invalid Form',
                  content=Label(text='Please fill in all inputs with valid information.'),
                  size_hint=(None, None), size=(400, 400))

    pop.open()


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

sm = WindowManager()

screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "create"


class MyMainApp(App):
    def build(self):
        return sm


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

完整的KV编码:

<ScreenManagement>:
    CreateWindow:
        id:create
        name: "create"

    MainWindow:
        id: main
        name: "main"

<CreateAccountWindow>:

    panjang: panjang
    lebar: lebar
    tinggi: tinggi

    FloatLayout:
        cols:1

        FloatLayout:
            size: root.width, root.height/2

            Label:
                text: "serfbgokmfor"
                size_hint: 0.8, 0.2
                pos_hint: {"x":0.1, "top":1}
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.5,0.12
                pos_hint: {"x":0, "top":0.8}
                text: "Panjang: "
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.5, "top":0.8}
                size_hint: 0.4, 0.12
                id: panjang
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.5,0.12
                pos_hint: {"x":0, "top":0.8-0.13}
                text: "Lebar: "
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.5, "top":0.8-0.13}
                size_hint: 0.4, 0.12
                id: lebar
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.5,0.12
                pos_hint: {"x":0, "top":0.8-0.13*2}
                text: "Tinggi:"
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.5, "top":0.8-0.13*2}
                size_hint: 0.4, 0.12
                id: tinggi
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

        Button:
            pos_hint:{"x":0.3,"y":0.25}
            size_hint: 0.4, 0.1
            font_size: (root.width**2 + root.height**2) / 17**4
            text: "Fefrkglt;mlfavsmdmcfr"
            on_release:
                root.manager.transition.direction = "left"
                root.login()

        Button:
            pos_hint:{"x":0.2,"y":0.05}
            size_hint: 0.6, 0.15
            text: "Kira"
            font_size: (root.width**2 + root.height**2) / 14**4
            on_release:
                root.manager.transition.direction = "left"
                root.submit()



<MainWindow>:
    panjang: panjang
    lebar: lebar
    tinggi: tinggi
    luas1: luas1
    luas2: luas2
    cat: cat

    FloatLayout:
        Label:
            id: panjang
            pos_hint:{"x": 0.1, "top":0.9}
            size_hint:0.8, 0.2
            text: "Panjang: "

        Label:
            id: lebar
            pos_hint:{"x": 0.1, "top":0.8}
            size_hint:0.8, 0.2
            text: "Lebar: "

        Label:
            id: tinggi
            pos_hint:{"x": 0.1, "top":0.7}
            size_hint:0.8, 0.2
            text: "Tinggi:"

        Label:
            id: luas1
            pos_hint:{"x": 0.1, "top":0.6}
            size_hint:0.8, 0.2
            text: "Luas:"

        Label:
            id: luas2
            pos_hint:{"x": 0.1, "top":0.5}
            size_hint:0.8, 0.2
            text: "Luas:"

        Label:
            id: cat
            pos_hint:{"x": 0.1, "top":0.4}
            size_hint:0.8, 0.2
            text: "Cat:"

        Button:
            pos_hint:{"x":0.2, "y": 0.1}
            size_hint:0.6,0.2
            text: "Semula"
            on_release:
                app.root.current = "create"
                root.manager.transition.direction = "down"

1 个答案:

答案 0 :(得分:0)

问题1

  

panjang未定义

根本原因

出现此错误是因为当您的应用尝试访问/引用方法on_enter()中未本地定义的属性/变量时。

解决方案

要从另一个屏幕访问输入值,需要通过执行以下操作将屏幕链接到ScreenManager:

  1. id:分配给kv文件中的每个屏幕
  2. Kv文件: :从<CreateWindow>:访问<MainWindow>:中的输入值,使用root.manager.ids.create.ids.panjang.text(使用全部ids )或root.manager.ids.create.panjang.text(混合使用idsObjectProperty
  3. Py文件: :从<CreateWindow>:访问<MainWindow>:中的输入值,使用self.manager.ids.create.ids.panjang.text(使用全部ids )或self.manager.ids.create.panjang.text(混合使用idsObjectProperty

摘要-kv文件

<WindowManager>:
    CreateAccountWindow:
        id: create
        name: 'create'

    MainWindow:
        id: main
        name: 'main'

<CreateAccountWindow>:
    ...
<MainWindow>:
    ...

摘要

def on_enter(self, *args):
    self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
    self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
    self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text

问题2

  

可以计算出luas1、2和cat吗?

解决方案

是的,可以计算出来。

  • 实施方法volume()并将公式放入其中
  • 在需要计算涂料量时调用新方法

摘要

from kivy.properties import NumericProperty, ObjectProperty
...

class MainWindow(Screen):
    # Each screen has by default a property manager that gives 
    # you the instance of the ScreenManager used.

    # declare class attributes
    panjang = ObjectProperty(None)
    lebar = ObjectProperty(None)
    tingi = ObjectProperty(None)
    luas1 = ObjectProperty(None)
    luas2 = ObjectProperty(None)
    cat = ObjectProperty(None)

    def logOut(self):
        self.manager.current = "create"

    def volume(self):
        luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
        luas2 = float(self.manager.ids.create.lebar.text) * float(self.manager.ids.create.tinggi.text) / 10

        self.luas1.text = str(luas1)
        self.luas2.text = str(luas2)
        self.cat.text = str(luas1 * 2 + luas2 * 2)

    def on_enter(self, *args):
        self.volume()    # calculate volume
        self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
        self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
        self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text

输出

MainWindow displayed