How can I align the listview labels? - python

I'm learning how to use Kivy, and I would like to know how can I align the labels inside the listview I built.
The labels are centered by default, and I would like to align all the labels to the left.
My code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import SimpleListAdapter
from kivy.uix.listview import ListView
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class TestApp(App):
def build(self):
messages = ["a", "b"]
layout = BoxLayout(orientation='vertical')
btn1 = Button(text='Hello')
textinput = TextInput(text='Hello world', size_hint=(1, 0.1))
messages.append("sd")
simple_list_adapter = SimpleListAdapter(
data=messages,
cls=Label)
simple_list_adapter.cls
list_view = ListView(adapter=simple_list_adapter)
layout.add_widget(list_view)
layout.add_widget(textinput)
return layout
TestApp().run()

A simple solution is to create a Label with that desired alignment
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import SimpleListAdapter
from kivy.uix.listview import ListView
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class LeftLabel(Label):
def __init__(self, **kwargs):
super(LeftLabel, self).__init__(**kwargs)
# https://kivy.org/doc/stable/api-kivy.uix.label.html#text-alignment-and-wrapping
self.halign = "left"
self.bind(size=self.setter("text_size"))
class TestApp(App):
def build(self):
messages = ["a", "b"]
layout = BoxLayout(orientation="vertical")
btn1 = Button(text="Hello")
textinput = TextInput(text="Hello world", size_hint=(1, 0.1))
messages.append("sd")
simple_list_adapter = SimpleListAdapter(data=messages, cls=LeftLabel)
list_view = ListView(adapter=simple_list_adapter)
layout.add_widget(list_view)
layout.add_widget(textinput)
return layout
TestApp().run()
Since you are starting in kivy it is recommended that you learn RecycleView instead of ListView since the latter is deprecated:
Deprecated since version 1.10.0: ListView has been deprecated, use RecycleView instead.

Related

Kivu scrollview enlarges the size of the buttons

This is the .py file
from kivy.uix.widget import Widget
from kivy.graphics import Color
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import Screen
m=123
class Scroll(ScrollView):
pass
class Stack(StackLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
k=0
for char in range(0,m):
k=k+1
b1 = Button(text=str(k),size_hint=(.2,.2))
self.add_widget(b1)
class MainWidget(Widget):
pass
class GameApp(App):
def build(self):
return Scroll()
GameApp().run()```
```<Scroll>:
Stack:
size_hint:1,None
height:4000
In the output I am getting the buttons but they are enlarged and when I don't apply scroll view they are of normal size.
(This is For error: fhxsyjvdfhgsyhxthdfofuddoydyeitefutfrfueitfuiefuite)

Substituting text inside buttons with widgets

My app (using kivy) needs to substitute text with widgets inside an unspecified number of buttons using iteration. I don't use kv language. Only the last button displays any content.
My app should display an unspecified number of buttons. Simplified example:
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
class Layout(Widget):
def __init__(self, app):
super().__init__()
self.layout = BoxLayout(orientation='vertical', size=Window.size)
contents = ['text1', 'text2', 'text3']
for text in contents:
button = Button(height=300, size_hint=(1, None))
button.text = text
self.layout.add_widget(button)
self.add_widget(self.layout)
class MyApp(App):
def build(self):
self.form = Layout(self)
return self.form
def on_pause(self):
return True
if __name__ in ('__main__', '__android__'):
MyApp().run()
Only instead of text buttons display images with descriptions. I change the code:
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
class Layout(Widget):
def __init__(self, app):
super().__init__()
self.layout = BoxLayout(orientation='vertical', size=Window.size)
contents = [('img1', 'text1'), ('img2', 'text2'), ('img3', 'text3')]
for img, text in contents:
button = Button(height=300, size_hint=(1, None))
button_layout = BoxLayout(orientation='vertical', size_hint=(1, 1))
label = Label(text=text)
image = Image(source=f'{img}.jpg', size_hint=(1, 1), allow_stretch=True)
button_layout.add_widget(image)
button_layout.add_widget(label)
button.add_widget(button_layout)
self.layout.add_widget(button)
self.add_widget(self.layout)
class MyApp(App):
def build(self):
self.form = Layout(self)
return self.form
def on_pause(self):
return True
if __name__ in ('__main__', '__android__'):
MyApp().run()
And only the last button displays content.
As far as I can understand, at some point Python automatically gets read of the content of the previous button. How can I prevent it? Also what is the best way to center it?
I'm not a native speaker, so sorry for bad grammar. Thank you in advance.

KivyMD Dialog not sizing over it's widgets

After some days looking for an answer, I finally decided that it was time to ask some more experienced users ! Here is my problem : in the following piece of code (simplified version of the original code), when I open the Dialog by clicking on the button, the opened window doesn't have the right size, and so one part of the GridLayout is appearing outside this popup.
I anyone has an idea, thanks in advance !
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivymd.uix.dialog import MDDialog
class AppClass(MDApp):
def build_toolbar(self):
button = Button(text="Press")
button.bind(on_press=self.popup)
return button
def build(self):
self.theme_cls.theme_style = "Dark"
layout = BoxLayout(orientation='vertical')
toolbar = self.build_toolbar()
layout.add_widget(toolbar)
return layout
#==============================
def popup(self, instance):
print("called")
panel = self.build_settings_panel()
self.dialog = MDDialog(
type="custom",
title="Settings",
content_cls=panel
)
self.dialog.open()
def build_settings_panel(self):
panel = GridLayout(cols=2, row_default_height=100)
for i in range(4):
panel.add_widget(Label(text="Number"))
panel.add_widget(Label(text=str(i)))
return panel
if __name__ == '__main__':
AppClass().run()
It appears that the problem is the size of the panel. way to fix that is to just calculate that size and set it in the build_settings_panel():
def build_settings_panel(self):
row_height = 100
total_height = 0
panel = GridLayout(cols=2, row_default_height=row_height)
for i in range(4):
panel.add_widget(Label(text="Number"))
panel.add_widget(Label(text=str(i)))
total_height +=row_height
panel.height = total_height
return panel
You can try:
row_default_height=9
In your code:
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivymd.uix.dialog import MDDialog
class AppClassjbsidis(MDApp):
def build_toolbar(self):
button = Button(text="Press")
button.bind(on_press=self.popup)
return button
def build(self):
self.theme_cls.theme_style = "Dark"
layout = BoxLayout(orientation='vertical')
toolbar = self.build_toolbar()
layout.add_widget(toolbar)
return layout
#==============================
def popup(self, instance):
print("called")
panel = self.build_settings_panel()
self.dialog = MDDialog(
type="custom",
title="[color=ffffff]Settings",
content_cls=panel
)
self.dialog.open()
def build_settings_panel(self):
panel = GridLayout(cols=2, row_default_height=9)
for i in range(4):
panel.add_widget(Label(text="Number"))
panel.add_widget(Label(text=str(i)))
return panel
if __name__ == '__main__':
AppClassjbsidis().run()
Pictures:

How to display slider value as it changes in python kivy

how do i change the text of a label to the value of the slider immediately.
this is my current code
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label``
class app(App):
def build(self):
layout = BoxLayout(orientation='vertical')
slide = Slider(min=-100, max=100,value=0)
label = Label(text=str(slide.value))
layout.add_widget(slide)
layout.add_widget(label)
return layout
app().run()
i want the answer in python
You need to create a Kivy Property and bind the slider value to an on_ event where you can then update the label's text. Here's one way to do that:
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import NumericProperty
import kivy
kivy.require('1.10.0')
class Container(BoxLayout):
slider_val = NumericProperty(0)
def __init__(self, *args, **kwargs):
super(Container, self).__init__(*args, **kwargs)
self.orientation = 'vertical'
slide = Slider(min=-100, max=100, value=0)
slide.fbind('value', self.on_slider_val)
self.label = Label(text=str(self.slider_val))
self.add_widget(slide)
self.add_widget(self.label)
def on_slider_val(self, instance, val):
self.label.text = str(val)
class app(App):
def build(self):
return Container()
app().run()

how can i change the text box position so it will be next to the button?(kivy)

i have a kivy code that makes a label, button and a text box. i want to put the textbox next to the button and not under it, how can i do that?
import socket
import sys
import os
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.bubble import Bubble
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class TextInputApp(App):
def build(self):
layout = BoxLayout(padding=10, orientation='vertical')
btn1 = Button(text="OK", size_hint=(0.49, 0.1),pos_hint={'x': .51, 'center_y': .5})
btn1.bind(on_press=self.buttonClicked)
layout.add_widget(btn1)
self.txt1 = TextInput(multiline=False, text='',
size_hint=(0.5, 0.1))
layout.add_widget(self.txt1)
self.lbl1 = Label(text="Write your guess in the blank text box", size_hint=(1, None), height=30)
layout.add_widget(self.lbl1)
return layout
def buttonClicked(self,btn):
print "hi"
TextInputApp().run()
You could create another BoxLayout with horizontal orientation to harbor the Button and TextInput.
class TextInputApp(App):
def build(self):
layout = BoxLayout(padding=10, orientation='vertical')
# Second boxlayout
layout2 = BoxLayout()
# Add BoxLayout do main layout
layout.add_widget(layout2)
# Drop old size and pos_hints
btn1 = Button(text="OK")
btn1.bind(on_press=self.buttonClicked)
# Add Button to secondary boxlayout
layout2.add_widget(btn1)
self.txt1 = TextInput(multiline=False, text='',
size_hint=(0.5, 0.1))
layout.add_widget(self.txt1)
# Drop size_hint
self.lbl1 = Label(text="Write your guess in the blank text box")
layout2.add_widget(self.lbl1)
return layout
If you want to change the size of the Button and TextInput, you can set that in the secondary BoxLayout:
layout2 = BoxLayout(size_hint_y=None, height=30)

Categories

Resources