如何使用BeeWare创建一个应用程序

584 阅读5分钟

使用BeeWare创建一个应用程序

BeeWare是一个用于创建跨平台应用程序的Python库。它可以作为Kivy等其他应用程序构建库的替代品。

本文将使用BeeWare构建一个简单的应用程序,向读者介绍该库并欣赏它的工作原理。

前提条件

  1. Python 在您的机器上安装BeeWare。
  2. 安装beewaretoga 。运行pip3 install beewarepip3 install toga 来分别安装它们。
  3. 基本的Python知识。
  4. 一个良好的网络连接。

开始学习

本教程将创建一个简单的计算器应用程序。

接下来,用终端导航到一个你选择的文件夹,然后执行briefcase new 命令。

该命令将在该文件夹中创建一个新的应用程序。按照指示,输入所需的细节,或反复按回车键,使用默认配置。

新的应用程序应该有以下结构。

.
├── LICENSE
├── pyproject.toml
├── README.rst
└── src
    ├── simplecalculator
    │   ├── app.py
    │   ├── __init__.py
    │   ├── __main__.py
    │   └── resources
    │       ├── __init__.py
    │       ├── simplecalculator.icns
    │       ├── simplecalculator.ico
    │       └── simplecalculator.png
    └── simplecalculator.dist-info
        ├── INSTALLER
        └── METADATA

src 文件夹中有应用程序运行所需的文件。所有运行应用程序的逻辑都在app.pyapp.py ,看起来应该如下。

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class SimpleCalculator(toga.App):
    def startup(self):
        main_box = toga.Box()
        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

def main():
    return SimpleCalculator()

该文件首先导入toga 工具包。Toga 是一个Python本地跨平台图形用户界面(GUI)工具包。

接下来,定义一个包含startup 方法的类SimpleCalculator 。这个方法定义了一个toga box 组件。它充当主框。

当你创建应用程序时,main_box 这个名字是默认声明和初始化的,但以后可以根据需要改变。

声明主窗口,其标题是用briefcase new 命令创建应用程序时定义的应用程序的名称。

接下来,我们让应用程序显示我们的窗口,其中包含我们的空主框作为其内容。最后,定义一个main 函数,返回我们的SimpleCalculator 类实例。这个main 方法会被__main__.py 文件调用和调用。

现在,你已经有了一个简单的工作程序。你可以把目录改成Simple Calculator ,然后输入briefcase dev ,在开发者模式下运行该应用程序。你应该有下面这个简单的应用程序。

Starter app image

设置盒子

修改app.py 文件,一步一步地创建我们最终的计算器应用程序。首先定义所有需要的必要的盒子组件。对app.py 文件进行如下修改。

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
from functools import partial

class SimpleCalculator(toga.App):

    def startup(self):
        box1 = toga.Box()
        box2 = toga.Box()
        box3 = toga.Box()
        box4 = toga.Box()
        box5 = toga.Box()
        box6 = toga.Box()

        main = toga.Box()

        # adding in main box
        main.add(box1)
        main.add(box2)
        main.add(box3)
        main.add(box4)
        main.add(box5)
        main.add(box6)
        main.style.update(direction=COLUMN)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main
        self.main_window.show()

def main():
    return SimpleCalculator()

我们已经创建了六个次要的盒子和一个主盒子,里面有所有六个盒子。我们将主框作为一个列框,这意味着除非定义,否则它将默认拥有所有的宽度,而高度将根据框内的内容扩展。

在下一节中,我们将让每个盒子包含数字和运算符,除了两个分别有一个输入字段和一个计算按钮外。当你运行应用程序时,你将不会看到任何变化。

设置按钮

现在设置数字、运算符和计算按钮。我们还将有一个输入文本的盒子。编辑app.py ,显示如下。

"""
Simple calculator
"""
from functools import partial

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


class SimpleCalculator(toga.App):

    def startup(self):
        box1 = toga.Box()
        box2 = toga.Box()
        box3 = toga.Box()
        box4 = toga.Box()
        box5 = toga.Box()
        box6 = toga.Box()

        main = toga.Box()

        self.input_text = toga.TextInput()
        self.input_text.style.width = 200
        self.input_text.style.padding_left = 10

        button7 = toga.Button('7', on_press=partial(self.enterdata, number='7'))
        button7.style.padding_top = 20
        button7.style.padding_left = 10

        button8 = toga.Button('8', on_press=partial(self.enterdata, number='8'))
        button8.style.padding_top = 20

        button9 = toga.Button('9', on_press=partial(self.enterdata, number='9'))
        button9.style.padding_top = 20

        buttonplus = toga.Button('+', on_press=partial(self.enterdata, number='+'))
        buttonplus.style.padding_top = 20

        button4 = toga.Button('4', on_press=partial(self.enterdata, number='4'))
        button4.style.padding_left = 10

        button5 = toga.Button('5', on_press=partial(self.enterdata, number='5'))

        button6 = toga.Button('6', on_press=partial(self.enterdata, number='6'))

        buttonminus = toga.Button('-', on_press=partial(self.enterdata, number='-'))

        button1 = toga.Button('1', on_press=partial(self.enterdata, number='1'))
        button1.style.padding_left = 10

        button2 = toga.Button('2', on_press=partial(self.enterdata, number='2'))

        button3 = toga.Button('3', on_press=partial(self.enterdata, number='3'))

        buttonmultiply = toga.Button('×', on_press=partial(self.enterdata, number='*'))

        buttondot = toga.Button('.', on_press=partial(self.enterdata, number='.'))
        buttondot.style.padding_left = 10

        button0 = toga.Button('0', on_press=partial(self.enterdata, number='0'))

        buttonclear = toga.Button('C', on_press=partial(self.enterdata, number='C'))

        buttondivide = toga.Button('÷', on_press=partial(self.enterdata, number='/'))

        calculate = toga.Button('CALCULATE', on_press=self.calculate)
        calculate.style.width = 150
        calculate.style.padding_top = 30
        calculate.style.padding_left = 30

        # adding
        box1.add(self.input_text)

        box2.add(calculate)

        box3.add(button7)
        box3.add(button8)
        box3.add(button9)
        box3.add(buttonplus)

        box4.add(button4)
        box4.add(button5)
        box4.add(button6)
        box4.add(buttonminus)

        box5.add(button1)
        box5.add(button2)
        box5.add(button3)
        box5.add(buttonmultiply)

        box6.add(buttondot)
        box6.add(button0)
        box6.add(buttonclear)
        box6.add(buttondivide)

        # adding in main box
        main.add(box1)
        main.add(box2)
        main.add(box3)
        main.add(box4)
        main.add(box5)
        main.add(box6)

        main.style.update(direction=COLUMN)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main
        self.main_window.show()

    def enterdata(self, widget, number):
        if (number == "C"):
            self.input_text.value = ""
        else:
            self.input_text.value = self.input_text.value + number

    def calculate(self, widget):
        output = eval(self.input_text.value)
        self.input_text.value = output


def main():
    return SimpleCalculator()

我们已经定义了所有必要的按钮,以使我们的应用程序所需。每一个按钮都是一个托加按钮,有些按钮在顶部或左侧有一点填充物,或两者都有。

在相同的按钮中,有一个回调方法on_press 。我们利用一个类定义的函数enterdata ,它接收一个数字或操作数,如+,-,*,÷,.C 。并将其附加到输入值上。

然后,我们使用Python中的内置函数partial ,它接收一个函数和一些预填充的输入,返回一个完全填充的函数。我们的部分函数接收我们的enterdata 函数和一个数字值,作为函数的最后参数。当按下时,CALCULATE 按钮也调用calculate 函数。

calculate 函数接收我们由enterdata 函数评估的表达式,并使用 Python 的内置函数eval 产生一个结果。eval 函数接收一个表达式作为输入,并将表达式的结果作为输出返回。

我们把我们的按钮添加到前面定义的不同的盒子里。我们首先将输入文本添加到box1 。接下来,我们将CALCULATE 按钮添加到box2 ,然后将789plus 按钮添加到box3 。最后,我们将按钮4、5、6和减号添加到box4 。然后,所有必要的按钮都以类似方式添加。

当我们最后运行我们的应用程序时,我们有以下输出。Final App

结论

在这一点上,你已经拥有了一个使用BeeWare构建的、在开发模式下运行的简单的工作计算器。