gpt4 book ai didi

python - 为什么运行 py 和 kv 脚本时出现空白屏幕?

转载 作者:行者123 更新时间:2023-12-01 08:59:38 29 4
gpt4 key购买 nike

当我启动该程序时,它给我一个空白的黑屏。我使用 python 文件来实现功能,使用 kv 文件来实现对象的属性。py文件:

import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout


class FloatLayoutApp(App):

def build(self):
return FloatLayout()

FlApp = FloatLayoutApp()
FlApp.run()

kv 文件:

<CustButton@Button>:
font_size: 32
color: 0, 0, 0, 1
size: 150, 50
background_normal: ""
background_down: "bg-grey.jpg"
background_color: .88, .88, .88, 1
size_hint: .4, .3

<FloatLayout>:
CustButton:
text: "Top Left"
pos_hint: {"x": 0, "top": 0}
CustButton:
text: "Bottom Left"
pos_hint: {"right": 1, "y": 0}
CustButton:
text: "Top Right"
pos_hint: {"right": 1, "top": 1}
CustButton:
text: "Bottom Left"
pos_hint: {"left": 1, "bottom": 0}
CustButton:
text: "Center"
pos_hint: {"center_x": 0, "center_y": 0}

最佳答案

您遇到黑屏/空白屏幕,因为在构建方法中它返回一个 FloatLayout 小部件作为根,并且没有任何小部件添加到根。

Kv language » How to load KV

There are two ways to load Kv code into your application:

By name convention:

Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:

MyApp -> my.kv

If this file defines a Root Widget it will be attached to the App’s root attribute and used as the base of the application widget tree.

By Builder: You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it will be returned by the method:

Builder.load_file('path/to/file.kv')

or:

Builder.load_string(kv_string)

解决方案

这个问题有几种解决方案。

方法1

将您的 kv 文件重命名为 floatlayout.kv

方法2

kv 文件

替换类规则,<FloatLayout>:具有根规则,FloatLayout:

Python 脚本

  1. 添加导入语句,from kivy.lang import Builder
  2. 替换return FloatLayout()return Builder.load_file('kv-filename.kv')

输出

Img01 - Method 1 Img02 - Method 2

关于python - 为什么运行 py 和 kv 脚本时出现空白屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52554144/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com