Kivy Tutorial2

Python

main.py

#-*- coding: utf-8 -*-

import kivy

kivy.require(‘1.9.1’)

from kivy.config import Config

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout

from kivy.uix.widget import Widget

from kivy.properties import StringProperty

from kivy.properties import ListProperty

from kivy.properties import BooleanProperty

from kivy.resources import resource_add_path

from kivy.utils import get_color_from_hex

from kivy.factory import Factory

from kivy.core.window import Window

from kivy.core.text import LabelBase, DEFAULT_FONT

#ウィンドウサイズの変更

Config.set(‘graphics’, ‘width’, ‘640’)

Config.set(‘graphics’, ‘height’, ‘480’)

#フォントの変更

resource_add_path(‘./fonts’)

LabelBase.register(DEFAULT_FONT, ‘mplus-2c-regular.ttf’)

class Calc(BoxLayout):

    clear_bool = BooleanProperty(False)

    def __init__(self, **kwargs):

        super(Calc, self).__init__(**kwargs)

    def print_number(self, number):

        if self.clear_bool:

            self.clear_display()

        text = ‘{}{}’.format(self.display.text, number)

        self.display.text = text

    def print_operator(self, operator):

        if self.clear_bool:

            self.clear_bool = False

        text = ‘{}{}’.format(self.display.text, operator)

        self.display.text = text

    def print_point(self, operator):

        print(“未実装演算子「{0}」が押されました”.format(operator))

    def clear_display(self):

        self.display.text = “”

        self.clear_bool = False

    def del_char(self):

        self.display.text = self.display.text[-1]

    def calculate(self):

        try:

            self.display.text = str(eval(self.display.text))

            self.clear_bool = True

        except:

            print(“数字を入力せずに「=」を押しています”)

    def change_calc2(self):

        self.clear_widgets()

        calc2 = Factory.Calculator2()

        self.add_widget(Factory.Calculator2())

class TestApp(App):

    def __init__(self, **kwargs):

        super(TestApp, self).__init__(**kwargs)

        self.title = ‘ウィンドウ名’

if __name__ == ‘__main__’:

    Window.clearcolor = get_color_from_hex(‘#FFFFFF’)

    TestApp().run()

test.kv

#-*- coding: utf-8 -*-

#: import get_color_from_hex kivy.utils.get_color_from_hex

Calc

<Calc>

    id: calc

    orientation: ‘vertical’

    display: display_input

    ActionBar:

        ActionView:

            ActionPrevious:

                title: ‘電卓’

                with_previous: False

            ActionButton:

                text: ‘切替’

                on_press: app.root.change_calc2()

            ActionButton:

                text: ‘音’

                icon: ‘atlas://data/images/defaulttheme/audio-volume-high’

    TextInput:

        id: display_input

        size_hint_y: 1

        font_size: 30

        hint_text: ‘0’

    Keyboard:

        size_hint_y: 3.5

<Keyboard@GridLayout>:

    cols: 4

    rows: 5

    spacing: 2

    padding: 4

    ClearButton:

        text: “C”

    CalcButton:

        text: “%”

    DelButton:

        text: “x”

    OperatorButton:

        text: “/”

    NumberButton:

        text: “7”

    NumberButton:

        text: “*”

    NumberButton:

        text: “-“

    EqualButton:

        text: “=”

<ButtonFormat@Button>:

    font_size: “30dp”

    background_normal: “”

    background_down: “”

    background_color: get_color_from_hex(“#83481F”)

    on_press: self.background_color = get_color_from_hex(“#825534”)

    on_release: self.background_color = get_color_from_hex(“#823600”)

<NumberButton@ButtonFormat>:

    on_press: app.root.print_number(self.text)

<OperatorButton@ButtonFormat>:

    on_press: app.root.print_operator(self.text)

<ClearButton@ButtonFormat>:

    on_press: app.root.clear_display()

<DelButton@ButtonFormat>:

    on_press: app.root.del_char()

<EqualButton@ButtonFormat>:

    on_press: app.root.calculate()

<CalcButton@ButtonFormat>:

    on_press: app.root.print_point(self.text)

<Calculator2@BoxLayout>

    id: calculator2

    orientation: “vertical”

    Label:

        text: “切替画面”

BACK