I'm a completely amateur programmer. So I just started learning Kivy and refreshing my Python memory because I needed to make a mobile app. I kind of understand the basic idea of OOP, but not much else. I haven't taken any courses or anything, I'm just winging it as I go along.
So I have the basic menu set up with one other screen that gets taken to by all the buttons. I'd like to add a stack layout with a bunch of images (named 1.jpg, 2.jpg) with captions underneath them in that other screen. I understand, how to make that happen in Python, I just have no idea how to do it in Kivy. I could just add them all 1 by 1 in the .kv file but that would take ages. I tried a lot of different combinations, but I'm starting to understand that I'm nowhere near enough educated in classes and objects to be doing this. I could never get the add_widget right, because I could never 1) position the code correctly so it would even run and 2) point it at the right attributes so even if it ran, the result wouldn't appear.
The for loop I usually used for testing:
g = Stack()
for i in range(9)
btn = Button(text=("Test" + str(i), size_hint=(0.2, 0.1))
g.add_widget(btn)
I looked online and all I got was that I'm missing a "self" or "root" somewhere. I have no idea how to combine a .kv file with add_widget's. For the basic menu I followed a simple tutorial.
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()
The .kv file:
#: 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:
I know this is a not-very-specific question and I should be studying the Docs, but the amount of information there is overwhelming.
I have no idea what defining the __init__ function did, but it did the trick.
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)
And
<Other>:
name: 'other'
Stack: