如何在Kivy/Python中从另一个屏幕的一个屏幕引用TextInput? [英] How to ref a TextInput from one screen in another screen in Kivy/Python?

查看:116
本文介绍了如何在Kivy/Python中从另一个屏幕的一个屏幕引用TextInput?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以计算圆锥体体积的应用程序(到目前为止). 我有一个名为ConeVolumeScreen的屏幕,其中有两个TextInput小部件.

I'm trying to make an app that can calculate the volume of a cone(so far). I have a screen named ConeVolumeScreen that has two TextInput widgets.

<ConeVolumeScreen>:
    BoxLayout:
        orientation: ...
        padding: ...
        spacing: ...
        Label:
            text: 'Radius:'
        TextInput:
            id: cone_vol_radius
            multiline: False
            input_type: 'number'
        Label:
            text: 'Height:'
        TextInput:
            id: cone_vol_height
            multiline: False
            input_type: 'number'
        Button:
            text: 'Solve'
            on_release: app.root.changeScreen('solve cone volume')               

应该将一个人的圆锥体的半径和高度输入到这两个小部件中.然后,此人可以单击按钮以转到名为SolveConeVolumeScreen的下一个屏幕.在此屏幕中,有一个标签应打印该人指定的锥体的体积.

A person is supposed to enter the radius and height of the cone into these two widgets. Then the person can click on a button to go to the next screen named SolveConeVolumeScreen. In this screen there is a Label that should print the volume of the cone that the person specified.

<SolveConeVolumeScreen>:
    BoxLayout:
        orientation: ...
        padding: ...
        spacing: ...
        Label:
            text: app.getConeVolume(cone_vol_radius, cone_vol_height)

getConeVolume()是这里的一种方法

getConeVolume() is a method over here

class CalculatorRoot(BoxLayout):
    def __init__(self, **kwargs):
        super(CalculatorRoot, self).__init__(**kwargs)
        self.screen_list = []

    def changeScreen(self, next_screen):
        if self.ids.calc_screen_manager.current not in self.screen_list:
            self.screen_list.append(self.ids.calc_screen_manager.current)

        if next_screen == 'volume':
            self.ids.calc_screen_manager.current = 'volume_screen'
        elif next_screen == 'area_screen':
            self.ids.calc_screen_manager.current = 'area_screen'
        elif next_screen == 'surface area':
            self.ids.calc_screen_manager.current = 'surfarea_screen'
        elif next_screen == 'cone volume':
            self.ids.calc_screen_manager.current = 'coneVolume_screen'
        elif next_screen == 'solve cone volume':
            self.ids.calc_screen_manager.current = 'solveConeVolume_screen'
        elif next_screen == 'rectangular based pyramid volume':
            self.ids.calc_screen_manager.current = 'rectPyramidVolume_screen'

    def onBackButton(self):
        if self.screen_list:
            self.ids.calc_screen_manager.current = self.screen_list.pop()
            return True
        return False



class CalculatorApp(App):
    def __init__(self, **kwargs):
        super(CalculatorApp, self).__init__(**kwargs)
        Window.bind(on_keyboard=self.onBackButton)

    def onBackButton(self, window, key, *args):
        if key == 27:
            return self.root.onBackButton()

    def build(self):
        return CalculatorRoot()

    def getConeVolume(self, r, h):
        first_step = 'π * ' + str(r) + '^2 * ' + str(h) + ' / 3\n'
        rr = round(r * r, 2)
        second_step = 'π * ' + str(rr) + ' * ' + str(h) + ' / 3\n'
        rh = round(rr * h, 2)
        third_step = 'π * ' + str(rh) + ' / 3\n'
        pirh = round(pi * rh, 2)
        fourth_step = str(pirh) + ' / 3\n'
        result = round(pi * rh, 2)
        final_step = 'The answer is ' + str(result) + '.'
        thing = first_step + second_step + third_step + fourth_step + final_step
        return thing

但该错误表明未定义锥度_半径_半径.

But the error says that cone_vol_radius is not defined.

 ...
 128:        spacing: min(root.width, root.height) * .02
 129:        Label:

130:文本:app.getConeVolume(cone_vol_radius,cone_vol_height) 131: 132 :: ... BuilderException:解析器:文件"/Users/fayzulloh/Desktop/Calculator App/calculator.kv",第130行: ... 128:间距:最小值(root.width,root.height)* .02 129:标签: 130:文本:app.getConeVolume(cone_vol_radius,cone_vol_height) 131: 132 :: ... NameError:名称"cone_vol_radius"未定义

130: text: app.getConeVolume(cone_vol_radius, cone_vol_height) 131: 132:: ... BuilderException: Parser: File "/Users/fayzulloh/Desktop/Calculator App/calculator.kv", line 130: ... 128: spacing: min(root.width, root.height) * .02 129: Label: 130: text: app.getConeVolume(cone_vol_radius, cone_vol_height) 131: 132:: ... NameError: name 'cone_vol_radius' is not defined

请帮助.我真的很感谢任何建议.

please help. I would really appreciate any advice.

这是我的屏幕管理器

<CalculatorRoot>:
    orientation: "vertical"

    ScreenManager:
        id: calc_screen_manager
        StartScreen:
            name: 'start_screen'
        VolumeScreen:
            id: volume_screen
            name: 'volume_screen'
        AreaScreen:
            id: area_screen
            name: 'area_screen'
        SurfaceAreaScreen:
            id: surfarea_screen
            name: 'surfarea_screen'
        ConeVolumeScreen:
            id: coneVolume_screen
            name: 'coneVolume_screen'
        SolveConeVolumeScreen:
            id: solveConeVolume_screen
            name: 'solveConeVolume_screen'
        RectPyramidVolumeScreen:
            id: rectPyramidVolume_screen
            name: 'rectPyramidVolume_screen'

推荐答案

错误

应用程序中有几个错误.

Errors

There are a couple of errors in the application.

在参数中添加root.ids.ids.coneVolume_screen.ids..

解决NameError后,将发生AttributeError. AttributeError: 'NoneType' object has no attribute 'ids'.这是因为内部ID尚不可用.

After solving the NameError, an AttributeError will occurred. AttributeError: 'NoneType' object has no attribute 'ids'. This is because inner ids are not available yet.

基辅语言»ids

注意,最外面的小部件在应用任何其他规则之前,先将kv规则应用于其所有内部小部件. 这意味着如果 内部小部件包含ID,这些ID可能在 内部小部件的__init__函数.

Note that the outermost widget applies the kv rules to all its inner widgets before any other rules are applied. This means if an inner widget contains ids, these ids may not be available during the inner widget’s __init__ function.

AttributeError:ids-解决方案

  1. id赋予标签,例如id: result
  2. 添加on_pre_enter事件以调用getConeVolume()方法.
  3. 用TextInput的文本替换TextInput对象,即分别用cone_vol_radius.textcone_vol_height.text替换cone_vol_radiuscone_vol_height.
  4. 添加int()函数以将TextInput的文本/字符串转换为整数.
  1. Give an id to the Label e.g. id: result
  2. Add an on_pre_enter event to invoke getConeVolume() method.
  3. Replace TextInput object with TextInput's text i.e. replace cone_vol_radius and cone_vol_height with cone_vol_radius.text and cone_vol_height.text respectively.
  4. Add int() function to convert TextInput's text/string to integer.

摘要

<SolveConeVolumeScreen>:
    on_pre_enter:
        root.ids.result.text = app.getConeVolume(int(app.root.ids.coneVolume_screen.ids.cone_vol_radius.text), int(app.root.ids.coneVolume_screen.ids.cone_vol_height.text))

    BoxLayout:
        orientation: 'vertical'
        Label:
            id: result

输出

这篇关于如何在Kivy/Python中从另一个屏幕的一个屏幕引用TextInput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆