Kivy输入值,用于简单计算 [英] Kivy input values for simple calculations

查看:93
本文介绍了Kivy输入值,用于简单计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Kivy(刚从昨天开始)很陌生,并且正在尝试创建一个足够简单的应用程序,该应用程序具有用于输入多个高度和面积值以计算体积的输入框.我找不到执行此操作的任何工作方法.到目前为止,我所得到的就是这个:

I'm quite new to Kivy (started yesterday) and am trying to create a simple enough app that has input boxes for several values of height and area to calculate volumes. I cant find any working methods of doing this. So far all I have got is this:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


Builder.load_string("""
<MenuScreen>:
    FloatLayout:

        Label:
            text: 'Please Select an Area to Work With:'
            pos: 230, 490
            size_hint: .15, .05
            font_size: 23



        Button:
            text: "A"
            pos: 230, 100
            size_hint: .4,.1
            font_size: 23
            on_press: root.manager.current = 'settings'


        Button:
            text: "B"
            pos: 230, 210
            size_hint: .4,.1
            font_size: 23
            on_press: root.manager.current = 'settings'


        Button:
            text: "C"
            pos: 230, 320
            size_hint: .4,.1
            font_size: 23
            on_press: root.manager.current = 'settings'



        Button:
            text: "D"
            pos: 230, 420
            size_hint: .4,.1
            font_size: 23
            on_press: root.manager.current = 'settings'






<SettingsScreen>:
    GridLayout:
        Label:
            text: 'Room 1'
            pos: 6, 460
            size_hint: .15, .05
            font_size: 23

        Label:
            text: 'Room 2'
            pos: 6, 420
            size_hint: .15, .05
            font_size: 23

        Label:
            text: 'Room 3'
            pos: 6, 380
            size_hint: .15, .05
            font_size: 23

        Label:
            text: 'Room 4'
            pos: 6, 340
            size_hint: .15, .05
            font_size: 23

        Label:
            text: 'Room 5'
            pos: 6, 300
            size_hint: .15, .05
            font_size: 23

        Label:
            text: 'Room 6'
            pos: 6, 260
            size_hint: .15, .05
            font_size: 23

        TextInput:
            text1: "0"
            multiline: False
            pos: 200,420
            font_size: 23
            on_text: viewer.text = self.text1
            size_hint: .001, .001

        TextInput:
            text2: "0"
            multiline: False
            pos: 200, 420
            font_size: 23
            on_text: viewer.text = self.text2
            size_hint: .001, .001

        TextInput:
            text3: "0"
            multiline: False
            pos: 200,380
            font_size: 23
            on_text: viewer.text = self.text3
            size_hint: .001, .001

        TextInput:
            text4: "0"
            multiline: False
            pos: 200,340
            font_size: 23
            on_text: viewer.text = self.text4
            size_hint: .001, .001


        TextInput:
            text5: "0"
            multiline: False
            pos: 200,300
            font_size: 23
            on_text: viewer.text = self.text5
            size_hint: .001, .001

        TextInput:
            text6: "0"
            multiline: False
            pos: 200,240
            font_size: 23
            on_text: viewer.text = self.text6
            size_hint: .001, .001
""")

# Declare both screen
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):

    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

我打算让每个按钮的第二页都具有唯一性,但是希望获得任何帮助.

Im planning to have the second page unique for each button pressed, but want Any help would be appreciated.

推荐答案

我希望我能理解您的问题.您正在要求执行此操作的方法.一个简单的答案是,您拥有整个Python来执行所需的所有操作. Kivy是一个提供许多组件来设计GUI的库.它还为您提供了一种由Builder.load_string()解析的语言.这是一个或多或少都在寻找的示例.它在第一个屏幕上有点像计算器.第二个屏幕为空,您可以使用底部的按钮在它们之间移动.

I hope I got to understand your question. You are asking for methods to do this. The plain answer is that you have the whole Python to do all the operations you want. Kivy is a library that provides a lot of components to design GUI. It also provides you with a language that is parsed by the Builder.load_string(). Here is an example that might be more or less what you are looking for. It is sort of a calculator on the first screen. The second screen is empty and you can move between them with the bottom buttons.

第一个屏幕上的计算器具有两个InputTexts和两个按钮(SumProduct).求和按钮直接在奇异果语言上实现了求和.产品按钮在根(Calc的一个实例)中调用一个方法.该方法本身并不存在.我在kivy部分下面的python代码中创建了代码.对于我所说的代码,有一些注释.

The calculator on the first screen has two InputTexts and two buttons (Sum and Product). The Sum Button has the implementation of a sum directly on the kivy language. The Product Button calls a method in the root (an instance of Calc). The method doesn't exist by itself. I created in the python code below the kivy section. There is some comments on the code for what I am saying.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Calc>:
    # This are attributes of the class Calc now
    a: _a
    b: _b
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    TextInput:
                        id: _a
                        text: '3'
                    TextInput:
                        id: _b
                        text: '5'
                    Label:
                        id: _result
                    Button:
                        text: 'sum'
                        # You can do the opertion directly
                        on_press: _result.text = str(int(_a.text) + int(_b.text))
                    Button:
                        text: 'product'
                        # Or you can call a method from the root class (instance of calc)
                        on_press: root.product(*args)
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Calc(FloatLayout):
    # define the multiplication of a function
    def product(self, instance):
        # self.result, self.a and self.b where defined explicitely in the kv
        self.result.text = str(int(self.a.text) * int(self.b.text))

class TestApp(App):
    def build(self):
        return Calc()

if __name__ == '__main__':
    TestApp().run()

这篇关于Kivy输入值,用于简单计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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