Adding width to parent layout by button release using Kivy language - kivy

I have a simple problem to which I cannot seem to find a straight answer. I am trying to, on a click of a child button, increase the parent layout width by a specific increment.
Here is an example of what I am trying to achieve:
GridLayout:
width: 200
size_hint_x: None
cols:1
Button:
size_hint:1, 0.07
text:"Add"
on_release: root.parent.width + 100
Button:
size_hint:1, 0.07
text:"Remove"
on_release: root.parent.width - 100
In this simple example by pressing the Add button should add 100 width to the parent grid layout and visa versa with the Remove button. However, not sure how to call the change of width with on_release: root.parent.width.
Any ideas?

When you refer to root.parent you are referring to whatever is containing the GridLayout that appears in your kv. If you want to adjust the size of the GridLayout, just use root.width. Also, using the + operator does not change the value, it just adds to value and returns the result. Try changing your kv to:
GridLayout:
width: 200
size_hint_x: None
cols:1
Button:
size_hint:1, 0.07
text:"Add"
on_release: root.width += 100
Button:
size_hint:1, 0.07
text:"Remove"
on_release: root.width -= 100

Related

Last Item added to Scrollview is cut

As Long as all the Items inside the Scrollview are smaler than the screen itself everything is fine, as soon as they get bigger the Last Item at the bottom is beeing cut in half and i cant scroll to see it propperly.
Kivy:
GridLayout:
ScrollView:
size_hint_y: None
pos_hint: {'x':self, 'y':self}
do_scroll_x: False
do_scroll_y: True
size: root.width, root.height / 1.25
GridLayout:
cols:1
padding: 15
spacing:10
height: sum(x.height for x in self.children)
size_hint_y:None
id: Zitate
Python:
btn = Button(text = '\n"' + self.root.ids.Kommentar.text + '"' + "\n ~" + self.root.ids.field.text + "\n", size_hint = (0.8 , None))
self.root.ids.Zitate.add_widget(btn)
Replace:
height: sum(x.height for x in self.children)
with:
height: self.minimum_height
The function that you are trying to use is built in to the GridLayout.
You should also set a specific height for each Button.

Can't update LABEL text on kivy file

I have a problem with updating a label text: id: time_ // the strange thing is , i can read(print) with self.ids.time_.text. Merci for help
class Scroll(Screen):
def __init__(self, **kwargs):
super(Scroll, self).__init__(**kwargs)
self.sec = 0
self.min = 0
def Label_updater(self,time):
print(self.ids.time_.text)
#self.ids.time_.text= str(time)
self.ids.time_.text='new_ text'
def start_timer(self, *args):
self.sec += 1
self.time = f'00:0{self.sec}'
self.Label_updater(self.time)
def on_start(self):
Clock.schedule_interval(self.start_timer, 1)
kivy file :
<Scroll>:
id: scroll_id
canvas:
Color:
rgba: 149 / 250.0, 77 / 250.0, 114 / 250.0, 0.9
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
spacing:7
padding:10
size_hint:(1,0.1)
pos_hint:{'top':1,'b':1}
Label:
id: score
text: 'Score: 00'
Label:
id: level
text: 'Level: 01'
Label:
id: time_
size_hint: (1.0, 0.83)
text: '00:00'
color: 'red'
BoxLayout:
size_hint:(1,0.9)
GamePage:
padding: 20,20,20,20
will try to explain the probleme with some pictures
in class GamePage i did create some Buttons with callback = pressed( )
in pressed from there u see the starter object activating start_timer() (( this will start the timer ))
enter image description here
enter image description here
when i push a Button ---> everything is working fine till i can print(self.ids.time_.text) and i see in logfile the timer working
but in the screen the Label text still 00:00
enter image description here
but if i press the button ( added from the kivy file ) everything is working fine
i want to start the timer when i push any button
enter image description here

How to code for a variable in the Kivy label?

I'm trying to create a simple Kivy function that counts and updates the display in the label as either a variable or a variable that has been converted to string. Using Python 3.7 and Kivy 1.10.1
I've been reading previous questions related to labels, but they don't seem to fix my issue. Thanks.
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
import time
class SomeData():
num = 0
while num < 1000:
time.sleep(1)
num+=1
class FirstScreen (Screen):
runTouchApp(Builder.load_string('''
ScreenManager:
FirstScreen:
<FirstScreen>
BoxLayout:
orientation: 'vertical'
GridLayout:
cols: 3
spacing: '10dp'
Button:
Button:
Button:
Label:
size_hint_y: None
text: "Below is a scroll of numbers."
ScrollView:
Label:
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
halign: 'left'
valign: 'top'
text: (num)
'''))
The file never creates the Kivy screen and num variable is considered an error in the text label.
Here is a version for your code that will, at least, display:
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.screenmanager import Screen
#import time
# class SomeData:
# num = 0
# while num < 1000:
# time.sleep(1)
# num+=1
class FirstScreen (Screen):
num = NumericProperty(7)
runTouchApp(Builder.load_string('''
ScreenManager:
FirstScreen:
<FirstScreen>
BoxLayout:
orientation: 'vertical'
GridLayout:
cols: 3
spacing: '10dp'
Button:
Button:
Button:
Label:
size_hint_y: None
text: "Below is a scroll of numbers."
ScrollView:
Label:
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
halign: 'left'
valign: 'top'
text: str(root.num)
'''))
The SomeData class is commented out, since it does nothing but delay the display. Also, note that changing the value of num in a loop will not create a list of numbers, but will just change the number that is displayed in the Label. There is no need to import the time too. So, commented it.

Change nested property in KV code

In my KV file, I have created a Spinner widget on my window with about 10 items in it. However when I open the dropdown, it fills the whole vertical space of the window.
I've found a few solutions online, but they only show it with direct Python code.
Example:
spinner = Spinner(text='Test', values=('Hi', 'how', 'are', 'you', '?'))
spinner.dropdown_cls.max_height = 100
Here is my current KV code:
Spinner:
size_hint_x: None
width: 300
font_size: 30
text: "Static"
values: "Static", "Breathing", "Spectrum Cycle", "Rainbow", "Wipe", "Bullet", "Strobe", "Starlight", "Nightlight"
My question is how do I set the 'dropdown_cls.max_height' property in my KV code?
Solution
In the kv file, do the following. Please refer to snippets and example for details.
DropDown max_height
Create a class rule, <MyDropdown#DropDown>: and add max_height: 100.
<MyDropdown#DropDown>:
max_height: 100
Spinner fills whole vertical window space
Replace size_hint_x: None with size_hint: (None, None) to prevent Spinner from filling the whole vertical space of the window.
Example
main.py
from kivy.lang import Builder
from kivy.base import runTouchApp
runTouchApp(Builder.load_string('''
#:import Factory kivy.factory.Factory
<MySpinnerOptions#SpinnerOption>:
background_color: .4, .4, .4, 1
<MyDropdown#DropDown>:
max_height: 100
<MySpinner#Spinner>:
size_hint: (None, None)
width: 300
font_size: 30
text: "Static"
values: "Static", "Breathing", "Spectrum Cycle", "Rainbow", "Wipe", "Bullet", "Strobe", "Starlight", "Nightlight"
dropdown_cls: Factory.MyDropdown
option_cls: Factory.MySpinnerOptions
GridLayout:
cols: 1
MySpinner:
'''))
Output

How to set fixed height of text input and stop overlapping of two text input in kivy?

Basically i want fixed text input height and don'n overlap two textinput
TextInput:
focus:True # cursor into textfield
cursor_color: .26,.26,.3,1 #cursor color
#size_hint: None, 1
#size: self.texture_size
foreground_color: .26,.26,.3,1 # Input text color
pos: root.width * .24,root.height * .47
width: root.width * .55
height:'34dp'
size_hint: (.2, None)
#size: (min(self.width,self.height),min(self.width,self.height))
#size_hint_min_y:self.minimum_height #used for fixed minimum height
#height:root*.9
#height:self.minimum_height
font_size:root.height * .03
hint_text:"please Enter your captha"
multiline:False
background_normal: "resource/images/rounded-image-green.jpg"
background_active: "resource/images/rounded-image-red.jpg"
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]

Resources