[英]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)
我在网上看了一下,发现所有地方都缺少“自我”或“根”。 我不知道如何将.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:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.