Reputation: 65
My for loop creates a simple list of labels within a scrollview, instead of changing the font size and colour in the python file, I would rather customise the labels within my KV file. Is this possible?
I know I can use ids to reference a label in the KV file, but I cant wrap my head around how to do it here.
If I create a label in my python file, is it good practice to customise in my kv file or continue to customise it in the python file. What is the best way to go about this?
*.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
class FirstWindow(Screen):
def __init__(self, **kwargs):
super(FirstWindow, self).__init__(**kwargs)
Clock.schedule_once(self.create_scrollview)
def create_scrollview(self, dt):
list1 = ['1','2','3','4','5','6','7','8','9','10','11','12']
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter("height"))
for x in list1:
l = Label(text=x, size=(10, 50), size_hint=(1, None)) <----- Change the font colour in my kv file
layout.add_widget(l)
scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
self.view.add_widget(scrollview)
scrollview.add_widget(layout)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('NearMe.kv')
class NearMeApp(App):
def build(self):
return kv
*.kv
WindowManager:
FirstWindow:
<FirstWindow>:
view: view
BoxLayout:
orientation: 'vertical'
BoxLayout:
size: (64, 64)
size_hint: (1, None)
Label:
text: "NearMeApplications"
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
ScrollView:
id: view
canvas.before:
Color:
rgba: .8, .8, .8, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Upvotes: 1
Views: 2064
Reputation: 322
if i understand your question well the best way to use kivy.properties this is a part of project a function which check the user name and passwords before saving it and change the text and color of message in each case
.py
from kivy.properties import StringProperty, ColorProperty
class SettingsWindow(Screen):
warning_message = StringProperty()
warning_color = ColorProperty()
def save_user(self, uname, pw1, pw2):
if len(uname) > 3:
if len(pw1) > 3:
if pw1 == pw2:
self.warning_color = 0.5, 1, 0.5, 1
self.warning_message = "data saved"
else:
self.warning_color = 1, 0.5, 0.5, 1
self.warning_message = "not the same password"
else:
self.warning_color = 1, 0.4, 0.4, 1
self.warning_message = "password must be more than 3 chrs"
else:
self.warning_color = 1, 0.3, 0.3, 1
self.warning_message = "User Name must be more than 3 chrs"
pass
.kv
<SettingsWindow>:
name:'setting'
GridLayout:
cols:1
Label:
text:'change user data'
font_size:18
size_hint:(0.1,0.1)
GridLayout:
padding:[10,10]
size_hint:(1,0.2)
cols:2
rows:3
Label:
text:'User Name'
font_size:18
BoxLayout:
padding:[10,10]
TextInput:
id:uname
font_size:18
Label:
text:'password'
font_size:18
BoxLayout:
padding:[10,10]
TextInput:
id:pw1
Label:
text:'repeat password'
font_size:18
BoxLayout:
padding:[10,10]
TextInput:
id:pw2
Label:
id:warning_message
font_size:18
size_hint:(1,.1)
color: root.warning_color #-------------look at this
text : root.warning_message #-------------look at this
GridLayout:
padding:[15,15]
size_hint:(0.1,.1)
cols:2
Button:
text:'Save User Data'
on_release:
root.save_user(uname.text,pw1.text,pw2.text)
Button:
text:'cancel'
Upvotes: 0
Reputation: 71
class MyTestApp(App):
def build(self):
return Button(text="HELLO", color=(0, 0, 5, 1), background_color=(7, 4, 5, 1), font_size=150)
if __name__ == "__main__":
MyTestApp().run()
Upvotes: 0
Reputation: 38822
You can define your own customized Label
like this:
def MyLabel(Label):
pass
Then in your kv
make a rule for MyLabel
:
<MyLabel>:
color: 1,0,0,1
size: 10, 50
size_hint: 1, None
And in your loop:
for x in list1:
l = MyLabel(text=x) <----- Change the font colour in my kv file
layout.add_widget(l)
Upvotes: 1
Reputation:
You just need to add color: r,g,b,a
.
Label:
text: "NearMeApplications"
color: 1,0,1,1
Upvotes: 0
Reputation: 621
Correct me if I'm wrong, but it is not possible to set the value of any widget that is initiallized in the python file from the .kv file, as it is only added AFTER the entire .kv file is loaded, which would most likely result in an error. Though you can still edit the color in the python file, which would require ids to be added into individual label, something like this:
for x in list1:
l = Label(id=x, text=x, size=(10, 50), size_hint=(1, None)) #ids from the list1 list
layout.add_widget(l)
Upvotes: 0