如何在 Kivy 中为循环添加堆栈布局?
How to add a stack layout for loop in Kivy?
我是一个完全业余的程序员。所以我刚开始学习 Kivy 并刷新我的 Python 记忆,因为我需要制作一个移动应用程序。我有点了解 OOP 的基本思想,但其他的了解不多。我没有上过任何课程或任何东西,我只是边走边学。
因此,我将基本菜单设置为另一个屏幕,所有按钮都会将其带到另一个屏幕。我想在另一个屏幕中添加带有一堆图像(名为 1.jpg、2.jpg)的堆栈布局,并在它们下方添加字幕。我明白,如何在 Python 中做到这一点,我只是不知道如何在 Kivy 中做到这一点。我可以将它们一一添加到 .kv 文件中,但这需要很长时间。我尝试了很多不同的组合,但我开始明白我在 类 方面的教育还远远不够,并且反对这样做。我永远无法得到正确的 add_widget,因为我永远无法 1) 正确定位代码,因此它甚至 运行 和 2) 将它指向正确的属性,所以即使它 运行,结果不会出现。
我经常用来测试的for循环:
g = Stack()
for i in range(9)
btn = Button(text=("Test" + str(i), size_hint=(0.2, 0.1))
g.add_widget(btn)
我在网上查了一下,结果发现我在某处遗漏了 "self" 或 "root"。我不知道如何将 .kv 文件与 add_widget 组合。对于基本菜单,我遵循了一个简单的教程。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import *
from kivy.uix.screenmanager import *
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.image import Image
from kivy.properties import *
from kivy.config import Config
from kivy.uix.button import Button
Config.set('graphics', 'width', '411')
Config.set('graphics', 'height', '731')
class Background(Image):
pass
class Relative(RelativeLayout):
pass
class Stack(StackLayout):
pass
class MainMenu(Screen):
pass
class Other(Screen):
pass
class ScreenManagement(ScreenManager):
pass
start = Builder.load_file("KVFile.kv")
class MainApp(App):
def build(self):
return start
MainApp().run()
.kv 文件:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainMenu:
Other:
<MainMenu>:
# ///// BACKGROUND IMAGE /////
name: 'main'
Background:
source: 'BG.jpg'
background_color: .34, .2, .48, .7
size_hint: None, None
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size: 800, 800
# ///// MAIN MENU BUTTON LAYOUT /////
Relative:
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "LOREM"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.4}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "IPSUM"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.4}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "DOLOR"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.3}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "SIT"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.3}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "AMET"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.2}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "OTHER"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.2}
<Other>:
name: 'other'
Stack:
我知道这是一个不太具体的问题,我应该研究一下文档,但是那里的信息量太大了。
我不知道定义 __init__ 函数做了什么,但它确实起作用了。
class Other(Screen):
pass
class Stack(StackLayout):
def __init__(self, **kwargs):
super(Stack, self).__init__(**kwargs)
for i in range(9):
btn = Button(size_hint=(0.2, 0.1), text=("Test" + str(i)))
self.add_widget(btn)
和
<Other>:
name: 'other'
Stack:
我是一个完全业余的程序员。所以我刚开始学习 Kivy 并刷新我的 Python 记忆,因为我需要制作一个移动应用程序。我有点了解 OOP 的基本思想,但其他的了解不多。我没有上过任何课程或任何东西,我只是边走边学。
因此,我将基本菜单设置为另一个屏幕,所有按钮都会将其带到另一个屏幕。我想在另一个屏幕中添加带有一堆图像(名为 1.jpg、2.jpg)的堆栈布局,并在它们下方添加字幕。我明白,如何在 Python 中做到这一点,我只是不知道如何在 Kivy 中做到这一点。我可以将它们一一添加到 .kv 文件中,但这需要很长时间。我尝试了很多不同的组合,但我开始明白我在 类 方面的教育还远远不够,并且反对这样做。我永远无法得到正确的 add_widget,因为我永远无法 1) 正确定位代码,因此它甚至 运行 和 2) 将它指向正确的属性,所以即使它 运行,结果不会出现。
我经常用来测试的for循环:
g = Stack()
for i in range(9)
btn = Button(text=("Test" + str(i), size_hint=(0.2, 0.1))
g.add_widget(btn)
我在网上查了一下,结果发现我在某处遗漏了 "self" 或 "root"。我不知道如何将 .kv 文件与 add_widget 组合。对于基本菜单,我遵循了一个简单的教程。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import *
from kivy.uix.screenmanager import *
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.image import Image
from kivy.properties import *
from kivy.config import Config
from kivy.uix.button import Button
Config.set('graphics', 'width', '411')
Config.set('graphics', 'height', '731')
class Background(Image):
pass
class Relative(RelativeLayout):
pass
class Stack(StackLayout):
pass
class MainMenu(Screen):
pass
class Other(Screen):
pass
class ScreenManagement(ScreenManager):
pass
start = Builder.load_file("KVFile.kv")
class MainApp(App):
def build(self):
return start
MainApp().run()
.kv 文件:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainMenu:
Other:
<MainMenu>:
# ///// BACKGROUND IMAGE /////
name: 'main'
Background:
source: 'BG.jpg'
background_color: .34, .2, .48, .7
size_hint: None, None
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size: 800, 800
# ///// MAIN MENU BUTTON LAYOUT /////
Relative:
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "LOREM"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.4}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "IPSUM"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.4}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "DOLOR"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.3}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "SIT"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.3}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "AMET"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.3, 'center_y': 0.2}
Button:
font_name: 'Effra_Std_Bd'
font_size: 35
text: "OTHER"
on_release: app.root.current = "other"
background_color: .34, .2, .48, .7
size_hint: 0.39, 0.09
pos_hint: {'center_x': 0.7, 'center_y': 0.2}
<Other>:
name: 'other'
Stack:
我知道这是一个不太具体的问题,我应该研究一下文档,但是那里的信息量太大了。
我不知道定义 __init__ 函数做了什么,但它确实起作用了。
class Other(Screen):
pass
class Stack(StackLayout):
def __init__(self, **kwargs):
super(Stack, self).__init__(**kwargs)
for i in range(9):
btn = Button(size_hint=(0.2, 0.1), text=("Test" + str(i)))
self.add_widget(btn)
和
<Other>:
name: 'other'
Stack: