Kivy中文本的输入输出管理

时间:2018-12-16 14:40:42

标签: python python-3.x kivy kivy-language

我正在尝试编写一个带有gui的程序,该程序将以三角形的3个边作为输入并输出该区域。我希望实时输出(以便无需用户按下任何执行按钮即可更新区域)。我是新手,但是我不知道如何管理输入和输出。任何帮助将不胜感激。

import kivy
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.graphics import *
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition, NoTransition
from kivy.uix.textinput import TextInput

Builder.load_string('''
#:kivy 1.10.1

<MainScreen>:
    Widget:
        pos_hint:{'x': 0, 'right': 1}
        size_hint: 2,2.1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.35}
        size_hint: 0.6,1
        canvas:
            Color:
                rgb: [0.8,0.8,0.8,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.04}
        size_hint: 0.04,1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Button:
        text: "Home"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0.2
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.9, 'right': 0.35}
        on_press:
    Button:
        text: "Area of a Triangle"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.75, 'right': 0.35}
        on_press: root.manager.current = 'triscreen'
    Button:
        text: "LCM and HCF"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.6, 'right': 0.35}
        on_press: root.manager.current = 'lcmscreen'
    Button:
        text: "Last Digit"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.45, 'right': 0.35}
        on_press:
            root.manager.current = 'ldscreen'


<LDScreen>:
    Widget:
        pos_hint:{'x': 0, 'right': 1}
        size_hint: 2,2.1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.35}
        size_hint: 0.6,1
        canvas:
            Color:
                rgb: [0.8,0.8,0.8,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.04}
        size_hint: 0.04,1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Button:
        text: "Home"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.9, 'right': 0.35}
        on_press:
    Button:
        text: "Area of a Triangle"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.75, 'right': 0.35}
        on_press:
    Button:
        text: "LCM and HCF"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.6, 'right': 0.35}
        on_press:
    Button:
        text: "Last Digit"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0.2
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.45, 'right': 0.35}
        on_press:

<LSMScreen>:

<TriScreen>:
    id: triscreens
    Widget:
        pos_hint:{'x': 0, 'right': 1}
        size_hint: 2,2.1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.35}
        size_hint: 0.6,1
        canvas:
            Color:
                rgb: [0.8,0.8,0.8,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Widget:
        pos_hint:{'x': 1, 'right': 0.04}
        size_hint: 0.04,1
        canvas:
            Color:
                rgb: [0.09,0.1372,0.2,1]
            Rectangle:
                pos: self.pos
                size: self.size
    Button:
        text: "Home"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.9, 'right': 0.35}
        on_press:
    Button:
        text: "Area of a Triangle"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0.2
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.75, 'right': 0.35}
        on_press:
    Button:
        text: "LCM and HCF"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.6, 'right': 0.35}
        on_press:
    Button:
        text: "Last Digit"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.45, 'right': 0.35}
        on_press:

    Label:
        text: "Area of a Triangle Calculator"
        size_hint:
        pos_hint: {"top": 1.35,"right":1.16}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0
        font_size: 35
    Label:
        text: "Start by entering the length of the sides"
        size_hint:
        pos_hint: {"top": 1.28,"right":1.1}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0
        font_size: 20

    Image:
        source: 'tridi.png'
        keep_ratio: True
        allow_stretch: False
        opacity: 0.8
        size_hint: 0.3, 0.4
        pos_hint: {'top': 0.7, 'right': 0.99 }

    TextInput:
        id: sidea
        text: "0"
        size_hint: 0.2,0.08
        pos_hint: {'top': 0.67, 'right': 0.68}
        font_size: 26

    TextInput:
        id: sideb
        text: "0"
        size_hint: 0.2,0.08
        pos_hint: {'top': 0.536, 'right': 0.68}
        font_size: 26

    TextInput:
        id: sidec
        text: "0"
        size_hint: 0.2,0.08
        pos_hint: {'top': 0.41, 'right': 0.68}
        font_size: 26

    Label:
        id: lsidea
        text: "a ="
        size_hint: 0.2,0.08
        font_size: 34
        pos_hint: {'top': 0.675, 'right': 0.52}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0

    Label:
        id: lsideb
        text: "b ="
        size_hint: 0.2,0.08
        font_size: 34
        pos_hint: {'top': 0.54, 'right': 0.52}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0

    Label:
        id: lsidec
        text: "c ="
        size_hint: 0.2,0.08
        font_size: 34
        pos_hint: {'top': 0.42, 'right': 0.52}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0

    Label:
        text: "The Area of the triangle is:"
        size_hint: 0.18,0.18
        font_size: 32
        pos_hint: {'top': 0.32, 'right': 0.7}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0

    Label:
        id: area
        text: triscreens.calculate(sidea.text, sideb.text, sidec.text)
        size_hint: 0.18,0.18
        font_size: 32
        pos_hint: {'top': 0.32, 'right': 1.01}
        color: 0.8,0.8,0.8,1
        background_color: 0.8,0.8,0.8,0

    Button:
        text: "ss"
        color: 0.09,0.1372,0.2,1
        background_color: 0.8,0.8,0.8,0
        size_hint: 0.31,0.15
        font_size: 28
        pos_hint: {'top': 0.25, 'right': 0.6}
        on_press: root.calc_area()


''')


class MainScreen(Screen):
    pass


class LDScreen(Screen):
    pass


class LCMScreen(Screen):
    pass


class TriScreen(Screen):

    def calculate(self, sidea, sideb, sidec):
        self.sidea = sidea.text
        self.sideb = sideb.text
        self.sidec = sidec.text

        if self.sidea == True and self.sideb == True and self.sidec == True:
            try:
                area.text = int(eval('sidea + sideb'))
            except Exception:
                area.text = "err"


sm = ScreenManager(transition=NoTransition())
sm.add_widget(MainScreen(name='main'))
sm.add_widget(LDScreen(name='ldscreen'))
sm.add_widget(LCMScreen(name='lcmscreen'))
sm.add_widget(TriScreen(name='triscreen'))


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


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

如果正常工作,程序似乎只返回两边的和,但是我稍后会更改公式。我只需要帮助纠正错误即可。

0 个答案:

没有答案