Kivy多屏管理

时间:2019-05-14 16:30:29

标签: python kivy

我对Kivy还是很陌生(两天前就开始学习它),当时我正在使用基本的计算器,但是遇到了我无法跳过的障碍。
我想创建多个屏幕,因为我打算在进一步学习Kivy的同时向计算器添加更多屏幕,而且我不知道如何在代码中调整ScreenManager。

这是我的.py文件

import kivy

kivy.require('1.11.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.pagelayout import PageLayout
from kivy.core.window import Window

Window.clearcolor = .3,.3,.3,1

class RootWidget(GridLayout):   
    def calculate(self, calculation):
        if calculation:
            try:
                self.display.text = str(eval(calculation))
            except Exception:
            self.display.text = "Error"
class kutuApp(App):
    def build(self):
        return RootWidget()
if __name__== '__main__':
    kutuApp().run()

这是我的.kv文件

<CustButton@Button>:
    font_size: 35
    background_color: 0,0,0,0
    canvas.before:
        Color:
            rgba: (.4, .4, .4, 1) if self.state=='normal' else (0,.7,.7,1)
        RoundedRectangle:
            pos: self.pos
            size: self.size
            radius: [20, ]

<RawLayout@BoxLayout>:
     spacing: 8
     padding: 8
      size_hint: [1, .2]

<RootWidget>:
       id: calculator
      rows: 10
       display: entry
       spacing: 1

BoxLayout:
    size_hint: [1, .1]
    Label:
        text: 'Basic Calculator'
    Label:
        text: 'Made by Xrew'

BoxLayout:
    padding: 10
    TextInput:
        id: entry
        spacing: 1
        padding: 5
        font_size: 32
        multiline: True
        focus: False
#       background_color: 0, 0, 0, 1
#       foreground_color: [1, 0, 1, 1]

RawLayout:
    CustButton:
        text: '<'
        on_press: entry.text += self.text
    CustButton:
        text: '>'
        on_press: entry.text += self.text
    CustButton:
        text: '≈'
        on_press: entry.text += '=='                

RawLayout:
    orientation: 'horizontal'
    CustButton:
        text: '('
        on_press: entry.text += '('
    CustButton: 
        text: ')'
        on_press: entry.text += ')'
    CustButton:
        text: '√'
        on_press: entry.text += '**(.5)'
    CustButton:
        text: '¹/x' 
        on_press: entry.text += '1/'
RawLayout:
    orientation: 'horizontal'
    CustButton:
        text: 'Del'
        on_press: entry.text = entry.text[:-1]
    CustButton:
        text: 'x²'
        on_press: entry.text += '**2'
    CustButton:
        text: 'xⁿ'
        on_press: entry.text += '**'
    CustButton:
        text: 'π'
        on_press: entry.text += '3.14'

RawLayout:
    orientation: 'horizontal'       
    cols: 4    
    CustButton:
        text: 'Clr'
        font_color: [255,0,0,1]
 #       background_normal: ' '
    #    background_color: 1, .3, .4, .85
        on_press: entry.text = ""
    CustButton:
        text: '+'
        on_press: entry.text += self.text
        font_size: 32
    CustButton:
        text: '÷'
        on_press: entry.text += '/'
    CustButton:
        text: '×'
        on_press: entry.text += '*'

RawLayout:
    rows: 1   
    orientation: 'horizontal'
    CustButton:
        text: '7'
        on_press: entry.text += self.text
    CustButton:
        text: '8'
        on_press: entry.text += self.text
    CustButton:
        text: '9'
        on_press: entry.text += self.text
    CustButton:
        text: '-'
        on_press: entry.text += self.text

RawLayout:
    orientation: 'horizontal'
    rows: 1
    CustButton:
        text: '4'
        on_press: entry.text += self.text
    CustButton:
        text: '5'
        on_press: entry.text += self.text
    CustButton:
        text: '6'
        on_press: entry.text += self.text
    CustButton:
        text: '+'
        on_press: entry.text += self.text   

RawLayout:
    orientation: 'horizontal'
    cols: 3
    CustButton:
        text: '1'
        size_hint: [.5, 1]
        on_press: entry.text += self.text
    CustButton:
        text: '2'
        size_hint: [.5, 1]
        on_press: entry.text += self.text
    CustButton:
        text: '3'
        size_hint: [.5, 1]
        on_press: entry.text += self.text
    CustButton:
        text: ' '
        size_hint: [.5, 1]
        background_normal: ' '
        background_color: 0, 0, 0, 0

RawLayout:
    orientation: 'horizontal'
    size_hint: [1, .2]
    CustButton:
        text: '0'
        on_press: entry.text += self.text
        size_hint: [.34, 1]
    CustButton:
        text: '.'
        on_press: entry.text += self.text
        size_hint: [.17, 1]
        font_size: 32
    CustButton:
        text: '='
        on_press: calculator.calculate(entry.text)
        size_hint: [.17, 2.4]
#       background_normal: ' '
    #   background_color:  0, .5, 95, 1

1 个答案:

答案 0 :(得分:0)

Kivy ScreenManager

以下步骤说明了如何使用ScreenManagerScreenButton小部件以及Button的事件之一({{1 },on_release

Py文件

  1. 添加导入语句on_press
  2. 声明一个继承ScreenManager的类,例如from kivy.uix.screenmanager import ScreenManager, Screen
  3. 声明两个继承Screen的类,例如class ScreenManagement(ScreenManager):class MenuScreen(Screen):
  4. class CalculatorScreen(Screen):添加为三个新类的主体,因为我们将使用kv语言设计其视图/表示。
  5. pass替换return RootWidget(),因为现在应用的根是Kivy ScreenManager
  6. return ScreenManagement()重命名为class RootWidget

摘要-py

class Calculator

kv文件

  1. 声明class rulesfrom kivy.uix.screenmanager import ScreenManager, Screen ... class Calculator(GridLayout): ... class MenuScreen(Screen): pass class CalculatorScreen(Screen): pass class ScreenManagement(ScreenManager): pass class kutuApp(App): def build(self): return ScreenManagement() <MenuScreen>: <CalculatorScreen>:,它们分别对应于Python中的<ScreenManagement>:class MenuScreen(Screen):class CalculatorScreen(Screen):脚本
  2. class ScreenManagement(ScreenManager):重命名为RootWidget
  3. Calculator实例化为类规则Calculator:的孩子
  4. <CalculatorScreen>:MenuScreen:实例化为类规则CalculatorScreen:的子代
  5. name分别授予<ScreenManagement>:MenuScreen:CalculatorScreen:name: 'menu'。这将使我们在切换屏幕时可以引用它们。

摘要-kv

name: 'calculator'

示例

main.py

<ScreenManagement>:
    MenuScreen:
        name: 'menu'
    CalculatorScreen:
        name: 'calculator'

<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Goto Calculator'
            on_press: root.manager.current = 'calculator'
        Button:
            text: 'Quit'

<CalculatorScreen>:
    Calculator:

...
<Calculator>:
    id: calculator

main.kv

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


Window.clearcolor = .3, .3, .3, 1


class Calculator(GridLayout):
    def calculate(self, calculation):
        if calculation:
            try:
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Error"


class MenuScreen(Screen):
    pass


class CalculatorScreen(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


Builder.load_file("main.kv")


class kutuApp(App):
    def build(self):
        return ScreenManagement()


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

输出

MenuScreen CalculatorScreen