【问题标题】:Kivy on Android Buttons have weird symbols instead of textAndroid 按钮上的 Kivy 有奇怪的符号而不是文本
【发布时间】:2021-07-25 09:53:03
【问题描述】:

堆栈溢出 我是 Kivy、buildozer 和一般应用程序开发的新手,并试图找出某个问题:

  1. 我将我的计算器应用部署到我的 Android 设备。
  2. 当我打开它时,我看到 Button 小部件的文本有奇怪的白色方形符号而不是文本。
  3. 不得不提一下,文本应该是黑色的,当我按下它时,符号消失,Button 变为空。
  4. 我尝试在不使用自定义字体的情况下进行构建,但没有任何改变。该程序在电脑上运行良好。

这是我应用的截图:calculator app screenshot

还有我的main.py:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.config import Config
import math

Config.set('kivy', 'window_icon', "calculator.ico")

class CalculatorLayout(Widget):
    operators = {'+', '-', '*', '/'}
    forbidden = {'+', '/', '*', '.'}
    all = {'+', '-', '*', '/', '.'}
    md = {'*', '/'}

    def __init__(self):
        super().__init__()
        self.exp = '0'

    def add(self, arg):
        split = self.split()
        if self.exp == '0' and not(arg in self.forbidden):
            self.exp = arg
        else:
            if (arg == '.') and (split[-1].count('.') == 1 or split[-1] in self.operators):
                pass
            elif (arg in self.operators) and (self.exp[-1] in self.all):
                pass
            else:
                self.exp += arg
        self.update()

    def submit(self):
        if self.exp[-1] in self.all:
            self.exp = self.exp[:-1]
        self.exp = str(eval(self.exp))
        self.update()

    def pow(self):
        if self.exp[-1] in self.all:
            self.exp = self.exp[:-1]
        self.exp = str(eval(self.exp)**2)
        self.update()

    def sqrt(self):
        if self.exp[-1] in self.all:
            self.exp = self.exp[:-1]
        self.exp = str(math.sqrt(eval(self.exp)))
        self.update()

    def delete(self):
        self.exp = self.exp[:-1]
        if not self.exp:
            self.exp = '0'
        self.update()

    def clear(self):
        self.exp = '0'
        self.update()

    def split(self):
        self.expSplit = []
        self.elem = ''
        for i, char in enumerate(self.exp):
            if char in self.operators:
                if self.elem:
                    self.expSplit.append(self.elem)
                self.elem = ''
                self.expSplit.append(char)
            else:
                self.elem += char
        if self.elem:
            self.expSplit.append(self.elem)
            self.elem = ''
        print(self.expSplit)
        return self.expSplit

    def update(self):
        self.box_design.text = self.exp


class CalculatorApp(App):
    Builder.load_file('main.kv')
    def build(self):
        self.icon = "calculator.png"
        Window.clearcolor = (232/255, 232/255, 232/255, 1)
        return CalculatorLayout()


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

这是我的 main.kv:

#:import rgb functions.rgb
#:import Window kivy.core.window.Window
<Button>
    size_hint: 0.33333333, 0.2
    background_normal: ''
    background_color: rgb(201, 201, 201, 1)
    color: "black"
    font_name: "digital-7.ttf"
    font_size: 24
    background_down: ""
    on_press:
        self.background_color = rgb(158, 158, 158, 1)
    on_release:
        self.background_color = rgb(201, 201, 201, 1)

<CalculatorLayout>
    box_design: box
    GridLayout:
        rows: 2
        size: root.width, root.height
        BoxLayout:
            size_hint_y: 0.2
            orientation: "horizontal"
            Label:
                id: box
                padding: (5, 0)
                text_size: self.size
                valign: "middle"
                halign: "right"
                font_size: 48
                font_name: 'digital-7.ttf'
                text: "0"
                color: rgb(0, 0, 0, 1)
        GridLayout:
            cols: 2
            spacing: 1
            StackLayout:
                size_hint_x: 0.8
                spacing: 1
                Button:
                    text: 'Clear'
                    font_size: 24 if Window.size[0] >= 400 else 20
                    font_size: 16 if Window.size[0] <= 300 else 24
                    on_press: root.clear()
                Button:
                    text: 'x^2'
                    font_size: 24 if Window.size[0] >= 400 else 20
                    font_size: 16 if Window.size[0] <= 300 else 24
                    on_press: root.pow()
                Button:
                    text: "sqrt(x)"
                    font_size: 24 if Window.size[0] >= 400 else 20
                    font_size: 16 if Window.size[0] <= 300 else 24
                    on_press: root.sqrt()
                Button:
                    text: '7'
                    on_press: root.add(self.text)
                Button:
                    text: '8'
                    on_press: root.add(self.text)
                Button:
                    text: '9'
                    on_press: root.add(self.text)
                Button:
                    text: '4'
                    on_press: root.add(self.text)
                Button:
                    text: '5'
                    on_press: root.add(self.text)
                Button:
                    text: '6'
                    on_press: root.add(self.text)
                Button:
                    text: '1'
                    on_press: root.add(self.text)
                Button:
                    text: '2'
                    on_press: root.add(self.text)
                Button:
                    text: '3'
                    on_press: root.add(self.text)
                Button:
                    text: '0'
                    on_press: root.add(self.text)
                Button:
                    text: '.'
                    on_press: root.add(self.text)
                Button:
                    text: '='
                    on_press: root.submit()
            StackLayout:
                spacing: 1
                size_hint_x: 0.2
                Button:
                    size_hint_x: 1
                    text: "Delete" if Window.size[0] >= 350 else "Del"
                    font_size: 24 if Window.size[0] >= 400 else 20
                    font_size: 16 if Window.size[0] <= 300 else 24
                    on_press: root.delete()
                Button:
                    size_hint_x: 1
                    text: '/'
                    on_press: root.add(self.text)
                Button:
                    size_hint_x: 1
                    text: '*'
                    on_press: root.add(self.text)
                Button:
                    size_hint_x: 1
                    text: '-'
                    on_press: root.add(self.text)
                Button:
                    size_hint_x: 1
                    text: '+'
                    on_press: root.add(self.text)

【问题讨论】:

  • 看起来很像字体问题。如果你在一些文本中包含一个标准的 kivy 标签会发生什么,该文本是否也不好?
  • 同样的问题,我确实指定了没有自定义字体的构建不能解决问题。也只是安卓的问题,因为我的程序在PC上运行流畅。

标签: python android kivy buildozer


【解决方案1】:

找到了解决方案。在对我的代码进行了一些详细操作后,我发现问题出在Button 颜色上。看起来 android 无法加载 "black" 字符串颜色值。我将其更改为 rgb 值(0, 0, 0, 1),它正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2012-10-08
    • 2014-09-04
    • 1970-01-01
    相关资源
    最近更新 更多