我遇到了这个问题,我无法使 SetUpScreen 可滚动,因此我的文本彼此拥挤。我不知道如何修复它并管理正确的缩进。非常感谢任何帮助!
这是主要的Python代码
# Python code
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
kivy.require('1.9.0')
class MyRoot(BoxLayout):
screen_manager = None
def __init__(self, **kwargs):
super(MyRoot, self).__init__(**kwargs)
def on_next_button_press(self):
self.screen_manager.current = 'login_screen'
def on_help_button_press(self):
self.screen_manager.current = 'help_screen'
class LoginScreen(Screen):
def on_login_button_press(self):
username = self.ids.username_input.text
password = self.ids.password_input.text
print("Username:", username)
print("Password:", password)
self.manager.current = 'logfile_screen'
class HelpScreen(Screen):
def on_setting_up_button_press(self):
self.manager.current = 'setting_up_screen'
def on_configure_button_press(self):
self.manager.current = 'configure_screen'
def on_using_button_press(self):
self.manager.current = 'using_screen'
def on_previous_button_press(self):
self.manager.current = 'login_screen'
class LogFileScreen(Screen):
pass
class ScrollableScreen(ScrollView, BoxLayout):
def __init__(self, **kwargs):
super(ScrollableScreen, self).__init__(**kwargs)
class SetUpScreen(ScrollableScreen, Screen):
def on_back_button_press(self):
self.manager.current = 'help_screen'
class ConfigureScreen(ScrollableScreen, Screen):
def on_back_button_press(self):
self.manager.current = 'help_screen'
class UsingScreen(ScrollableScreen, Screen):
def on_back_button_press(self):
self.manager.current = 'help_screen'
class NeuralRandom(App):
def build(self):
root = MyRoot()
root.screen_manager = root.ids.screen_manager
root.screen_manager.add_widget(SetUpScreen(name='setting_up_screen'))
root.screen_manager.add_widget(ConfigureScreen(name='configure_screen'))
root.screen_manager.add_widget(UsingScreen(name='using_screen'))
return root
neuralRandom = NeuralRandom()
neuralRandom.run()
这是我正在尝试修复的 kivy 文件中的代码块
<SetUpScreen>:
manager: self.manager
ScrollView:
do_scroll_x: False # Disable horizontal scrolling
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgba: 0.93, 0.87, 0.04, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Setup Home Assistant on a compatible device (E.G., old computer, raspberry pi, etc.):"
font_size: 30
color: 0, 0, 0
halign: 'center'
valign: 'middle'
Label:
text: "1. Steps for Raspberry Pi 3/4:"
font_size: 25
color: 0, 0, 0
halign: 'center'
valign: 'middle'
Label:
text: 'a. Connect Pi to Local network – Two options:\n' \
' i. Create CONFIG USB Drive\n' \
' 1. Format USB Drive as FAT32 or MS-DOS FAT. Name Drive “CONFIG”\n' \
' 2. Create Directory called “network"\n' \
' 3. Create a file with no extension called “my-network”\n' \
' 4. Paste this into file (fill in <Your Wifi Name Here> and <Your Wifi Password Here>\n' \
' ii. Connect Pi to Router via Ethernet Cable\n' \
' 1. That’s it… :)\n' \
'b. Create Home Assistant Micro-SD\n' \
' i. Install Raspberry Pi Imager\n' \
' ii. Insert SD Card into Computer\n' \
' iii. In Imager select SD card from “Storage”\n' \
' iv. Navigate to “Operating System” -> “Other specific-purpose OS” -> “Home assistants and home automation” -> “Home Assistant” -> Choose version compatible with your Pi version.\n' \
' v. Press “Write”\n' \
'c. Start Home Assistant Server\n' \
' i. Insert SD Card into Raspberry Pi\n' \
' ii. Insert USB Drive into Raspberry Pi (if opting with CONFIG USB Drive)\n' \
' iii. Connect Raspberry Pi into power source (at least 5V, 3A)'
multiline: True
font_size: 20
color: 0, 0, 0
halign: 'center'
valign: 'middle'
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: '130dp'
spacing: '20dp'
pos_hint: {'center_x': 0.5, 'y': 0}
Button:
text: "Back"
font_size: 25
size_hint_x: None
width: '150dp'
pos_hint: {'center_x': 0.5, 'y': 0}
on_press: root.on_back_button_press()
我尝试重新格式化第三个标签中的文本,以便它转到下一行,但我不断收到缩进错误,而且我不知道如何修复它们。我还在主要的 python 代码中添加了一个可滚动类,但到目前为止它还不起作用。
在您的
kv
中 BoxLayout
中的 ScrollView
使用 minimum_height
:
<SetUpScreen>:
manager: self.manager
ScrollView:
do_scroll_x: False # Disable horizontal scrolling
BoxLayout:
orientation: 'vertical'
# use this to allow the BoxLayout to grow
size_hint_y: None
height: self.minimum_height
然后您需要为
height
内的每个小部件分配一个 BoxLayout
。对于 Label
执行此操作的简单方法是:
Label:
# assign a height
size_hint_y: None
height: self.texture_size[1]
self.minimum_height
将计算 BoxLayout
的高度,该高度足以容纳所有子级。但它要求每个孩子都有一个指定的height
,所以这就是为什么每个Label
都应该有上面的代码。您可以对 Button
使用相同的代码。孩子 BoxLayout
已经分配了 height
,所以这样就可以了。