python - Kivy/Python 代码在 rpi 触摸屏上显示白屏

标签 python kivy raspberry-pi3

我写的一些代码似乎在桌面上运行时遇到了一些问题,但是当复制到我的 raspberry pi/触摸屏时,我得到一个纯空白的屏幕。 Rpi 已经安装了 kivy。 Kivy 演示在两个系统上都运行良好。有没有可能在 kivy 上有 import Window 的东西?任何建议将不胜感激!

编辑 1:我在网上看到其他人有类似的问题,需要添加(from kivy.lang.builder import Builder)和(Builder.load_file.py)。我在 python 代码中添加了它,这让我可以看到原始程序的大部分。一些 Kivy 正在显示,尽管出于某种原因——图形 menu.png 丢失了,页面底部的文本框/网格布局也丢失了(下面的 kivy 代码的底部)。这可能是由于我的定位,所以我会研究一下。

编辑 2:这应该得到解决 - 它正在将 Builder 实现到 python 代码(正确版本如下所示)。我还发现 - 树莓派可能对名为“menu.png”的图像有问题...只需更改图像的名称,一切都可见。不知道那个。感谢您的协助。

Python 代码:

from time import sleep

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.lang.builder import Builder

Builder.load_file('my.kv')


class MyFloatLayout(Widget):

    def formula(self, num):
        print (num)
        self.ids.formula_input.text = ''
        self.ids.formula_input.text = f'{num}'       

    def press(self):
        loaded = self.ids.formula_input.text
        loadnum = int(loaded)

        if loaded == "":
            print(f'Please enter a formula to continue!')
            self.ids.disp_stat.text = f'Please enter a formula'
            
        else:
            print(f'Dispensing Formula {loaded}!')
            self.ids.disp_stat.text = f'Dispensing Formula #{loaded}!'
            self.ids.formula_input.text = ''         
        
class MyApp(App):

    def build(self):
        Window.clearcolor = (1,1,1,1)
        return MyFloatLayout()
 
if __name__ == '__main__':
    MyApp().run()

基维代码:

(sideways carrot)MyFloatLayout(sideways carrot)

disp_stat:disp_stat
formula_input:formula_input

FloatLayout:
    size: root.width, root.height

    Label:
        text: "Welcome To Sharp!"
        color: (0,0,0,1)
        font_size: 40
        size_hint: (0.5,0.2)
        pos_hint: {"x":0.2, "top":1}

    Image:
        padding:200
        source: 'mylogo.png'
        size_hint: (0.5,0.25)
        pos_hint: {"x":.6, "top":1}
        allow_stretch: True
        keep_ratio: True
        
    Image:
        padding:200
        source: 'funimg.png'
        size_hint: (.5, .5)
        pos_hint: {"x":0, "top":.75}
        allow_stretch: True
        keep_ratio: True
        
    GridLayout:
        cols:1
        size_hint: (.4,.5)
        pos_hint: {"x":0.55, "top":.8}

        Label:
            text: "Select Your Formula:"
            font_size: 24
            color: (0,0,0,1)
            
        GridLayout:
            cols:2
            size_hint: (1,1)

            Button:
                text: "Relax"
                on_press: root.formula(1)

            Button:
                text: "Shemax"
                on_press: root.formula(2)

            Button:
                text: "Memax"
                on_press: root.formula(3)

            Button:
                text: "Bebax"
                on_press: root.formula(4)
                          
        Button:
            text: "Initialize Dispense!"
            font_size: 24       
            size_hint: (.2,.3)
            on_press: root.press()

    GridLayout:
        cols:2
        size_hint: (0.4,0.07)
        pos_hint: {"x":.05, "y":.06}

        Label:
            id:disp_stat
            text: "Formula to dispense:"
            color: (0,0,0,1)
            font_size: 12

        TextInput:
            id:formula_input
            text: ""
            font_size:12
            size_hint: (0.5,0.07)

最佳答案

遇到了同样的问题。通过在 Raspberry Pi 上增加专用于 GPU 的内存解决了这个问题。

编辑您的/boot/config.txt 文件(需要sudo)

向下滚动到 [all] 部分,然后按如下方式编辑 gpu_mem 行:

gpu_mem=256

然后重启你的 pi。

关于python - Kivy/Python 代码在 rpi 触摸屏上显示白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70801067/

相关文章:

python - 如何在kivy启动时使用触摸屏?

raspberry-pi3 - RPI 上的 SMBUS 给出 IOError : [Errno 121] Remote I/O error

python - 将多维张量中的一个值映射到另一个值

python - Kivy 未检测到目录中的文件

python - Google App Engine mail.Send 在 python2.7/smtplib.py 中返回 "TypeError: unhashable instance"

python - 如何创建 Kivy 文件 (.kv)

python - GLade Python 中带有 CSS 的复选框

linux - 无法将 rnx2rtkp 从 rtklib2.4.3b29 交叉编译到树莓派 pi3

python - 尝试将数组与用户输入匹配时出错

python - 调用文本但完全排除表格