在单个应用程序和 kivy 模拟器中使用 kivy 上的 2 个页面布局

Usage of 2 page layouts on kivy in a single app and emulator for kivy

我使用 Kivy 制作了一个简单的计算器应用程序。我面临的问题是我只能在应用程序中使用一种布局。我可以只使用网格布局,而我需要页面布局和网格布局。我想要一个滑块,通过它我可以滚动并查看应用程序中的其余按钮,而不是一个 Page.More 上的所有按钮准确地代码是:

main.py

from __future__ import division
import kivy
from math import sqrt
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.uix.pagelayout import PageLayout
class CalcGridLayout(GridLayout):
    def something(x):
        x+="hi"
        return x
    # Function called when equals is pressed
    def calculate(self, calculation):
        if calculation:
            try:
                # Solve formula and display it in entry
                # which is pointed at by display
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Galat Hai Bhai !"
class CalculatorApp(App):
    def build(self):
        return CalcGridLayout()
calcApp = CalculatorApp()
calcApp.run()
#buildozer init
#buildozer android debug deploy ---------------code to deploy into apk and then transfer the file into ur mobile

calculator.kv:

# Custom button
<CustButton@Button>:
    font_size: 65
    #color:.25,.80,.92,1
    size:100,100
    background_color:.50,.50,.50,1

# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
    id: calculator
    display: entry
    rows: 8
    padding: 0
    spacing: 0

    # Where input is displayed
    BoxLayout:
        TextInput:
            id: entry
            font_size: 80
            multiline: False

    # When buttons are pressed update the entry
    BoxLayout:
        spacing: 0
        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

    BoxLayout:
        spacing: 0
        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

    BoxLayout:
        spacing: 0
        CustButton:
            text: "1"
            on_press: entry.text += self.text
        CustButton:
            text: "2"
            on_press: entry.text += self.text
        CustButton:
            text: "3"
            on_press: entry.text += self.text
        CustButton:
            text: "*"
            on_press: entry.text += self.text

    # When equals is pressed pass text in the entry
    # to the calculate function
    BoxLayout:
        spacing: 0
        CustButton:
            text: "AC"
            on_press: entry.text = ""
        CustButton:
            text: "0"
            on_press: entry.text += self.text
        CustButton:
            text: "="
            on_press: calculator.calculate(entry.text)
        CustButton:
            text: "/"
            on_press: entry.text += self.text
    #my new layout
    BoxLayout:
        spacing: 0
        CustButton:
            text: "Del"
            on_press: entry.text =entry.text[:-1] 
        CustButton:
            text: "Pow"
            on_press: entry.text += '**'
        CustButton:
            text: "//"
            on_press: entry.text +=self.text
        CustButton:
            text: "mod"
            on_press: entry.text +='%'
    BoxLayout:
        CustButton:
            text: "Made for learning face"

现在这个应用程序的输出非常简单:

这里所有的数字和操作都只在一页上。我想要另一个我可以滚动的页面,它必须包含像'(',')'这样的操作。我正在考虑为此实现页面布局,但无法在应用程序中同时实现布局(网格+页面)。有人可以帮忙吗? 我的第二个疑问是我可以同时在移动设备(有点模拟器)上检查应用程序,我尝试使用 Kivy Remote Shell 但它没有用。每次我必须使用 buildozer init 然后部署它本身需要很多时间。然后用U盘把apk文件传到手机里测试。这很费时间。

你想使用 PageLayout 是对的,而且可以做到,只需按照以下步骤操作:

步骤 1: 在你的 main.py 添加这一行:

from kivy.uix.pagelayout import PageLayout

然后在你的 CalcGridLayout class 继承自 PageLayout 而不是 GridLayout 像这样:

class CalcGridLayout(PageLayout):

步骤 02: 在 <CalcGridLayout>: 之后的 calculator.kv 添加以下内容:

GridLayout:

然后缩进您的代码,如下所示 needed.A 工作示例..取自您的代码:main.py

from __future__ import division
import kivy
from math import sqrt
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.pagelayout import PageLayout
from kivy.config import Config
from kivy.uix.pagelayout import PageLayout

class CalcGridLayout(PageLayout):
    def something(x):
        x+="hi"
        return x
    # Function called when equals is pressed
    def calculate(self, calculation):
        if calculation:
            try:
                # Solve formula and display it in entry
                # which is pointed at by display
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Galat Hai Bhai !"
class CalculatorApp(App):
    def build(self):
        return CalcGridLayout()
if __name__=='__main__':
    calcApp = CalculatorApp()
    calcApp.run()

和calculator.kv

# Custom button
<CustButton@Button>:
    font_size: 65
    #color:.25,.80,.92,1
    size:100,100
    background_color:.50,.50,.50,1

# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
    GridLayout:
        id: calculator
        display: entry
        rows: 8
        padding: 0
        spacing: 0

        # Where input is displayed
        BoxLayout:
            TextInput:
                id: entry
                font_size: 80
                multiline: False

        # When buttons are pressed update the entry
        BoxLayout:
            spacing: 0
            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

        BoxLayout:
            spacing: 0
            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

        BoxLayout:
            spacing: 0
            CustButton:
                text: "1"
                on_press: entry.text += self.text
            CustButton:
                text: "2"
                on_press: entry.text += self.text
            CustButton:
                text: "3"
                on_press: entry.text += self.text
            CustButton:
                text: "*"
                on_press: entry.text += self.text

        # When equals is pressed pass text in the entry
        # to the calculate function
        BoxLayout:
            spacing: 0
            CustButton:
                text: "AC"
                on_press: entry.text = ""
            CustButton:
                text: "0"
                on_press: entry.text += self.text
            CustButton:
                text: "="
                on_press: calculator.calculate(entry.text)
            CustButton:
                text: "/"
                on_press: entry.text += self.text
        #my new layout
        BoxLayout:
            spacing: 0
            CustButton:
                text: "Del"
                on_press: entry.text =entry.text[:-1] 
            CustButton:
                text: "Pow"
                on_press: entry.text += '**'
            CustButton:
                text: "//"
                on_press: entry.text +=self.text
            CustButton:
                text: "mod"
                on_press: entry.text +='%'
        BoxLayout:
            CustButton:
                text: "Made for learning face"
    BoxLayout:
        id: test
        Button:
            text: 'You can then add more widgets here'

最后关于模拟器,这在 kivy 中还不是真正的模拟器,但是您可以通过将您的应用配置为 运行 在特定设备的规格上来模拟设备,例如 运行 您的应用模拟摩托罗拉机器人 2:

KIVY_DPI=240 KIVY_METRICS_DENSITY=1.5 python main.py --size 854x480

或 HTC ONE X:

KIVY_DPI=320 KIVY_METRICS_DENSITY=2 python main.py --size 1280x720

或任何其他设备:

KIVY_DPI=<your-desired-dpi> KIVY_METRICS_DENSITY=<your-device-density> python main.py --size <your-device-size>