Center Title in actionbar - kivy - kivy

Is there any chance to center title or some text in general in actionbar in kivy?
I am new in kivy so i will be happy for any help.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
kv=Builder.load_string("""
GridLayout:
cols:1
size: root.width, root.height
ActionBar:
ActionView:
minimum_width: root.width
use_separator: True
ActionPrevious:
app_icon:""
title:"Hey"
with_previous: False
ActionOverflow:
ActionLabel:
text: "centered text"
minimum_width:300
ActionLabel:
text: "time"
""")
class ButtonsApp(App):
def build(self):
return kv
if __name__ == "__main__":
ButtonsApp().run()

Related

KivyMD, MDFloatingActionButton: correct placement

I've been trying to use floating action buttons alongside FileChooser:
import kivy
kivy.require("2.1.0")
from kivy.utils import platform
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivymd.icon_definitions import md_icons
import os
USERPATH = os.path.expanduser("~")
if platform == "android":
from android.storage import primary_external_storage_path
from android.storage import secondary_external_storage_path
from android.permissions import request_permissions, Permission
USERPATH = primary_external_storage_path()
request_permissions(
[Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE]
)
KV = '''
#:kivy 2.1.0
#:set dark_gray (.5, .5, .5, 1)
MDBoxLayout:
orientation: "vertical"
MDTabs:
id: ps_tabs
TabList:
id: ps_tab_list
icon: "folder"
FileChooserListView:
id: ps_filechooser
canvas.before:
Color:
rgba: dark_gray
Rectangle:
size: self.size
pos: self.pos
MDFloatingActionButton:
id: fc_playdir
icon: "folder"
pos_hint: {"center_x": .4, "center_y": .5}
MDFloatingActionButton:
id: fc_playfile
icon: "file"
pos_hint: {"center_x": .6, "center_y": .5}
TabDetails:
id: ps_tab_details
icon: "book"
MDScrollView:
'''
class TabList(FloatLayout, MDTabsBase):
"""The engaged power supplies tab."""
class TabDetails(FloatLayout, MDTabsBase):
"""The engaged power supply details tab."""
class Ron(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.theme_cls.primary_palette = "Gray"
self.theme_cls.material_style = "M3"
if platform != "android":
self.root.ids.ps_tabs.lock_swiping = True
self.root.ids.ps_filechooser.rootpath = USERPATH
if __name__ == "__main__":
Ron().run()
Mouse clicks, as well as taps, shoot right through the buttons and into the FileChooser, no matter what I do. I tried placing the buttons into their own box layout, tried moving them up and down the widget tree. Nothing helps, while the buttons remain visible. Besides, on visiting the second tab, one can see two thick black button footprints. Something's very wrong and I can't think of anything else to try.

Using builder in Kivy

I want to create a simple app that has GUI with a button that allows me to vibrate an android phone. Im using a .kv file for the layout and the Builder in my .yp file
.py file:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from plyer import vibrator
class AndroidApp(GridLayout):
def vibrate(self):
vibrator.vibrate()
kv = Builder.load_file("android_app.kv")
class MainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MainApp().run()
.kv file:
#:import utils kivy.utils
<AndroidApp>:
FloatLayout:
canvas.before:
Color:
rgb: utils.get_color_from_hex("#ffffff")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 1
cols: 2
Label:
text:"Android Vibrate"
Button:
text:"Android Vibrate"
on_press:
root.vibrate()
When I try to run the app, I get the following error:
enter image description here
This should be straightforward app but somehow I find a way to make it crash. The android_app.py & .kv files are in the same folder. Any ideas why the window wont be created? I aprreciate any help.
Thanks,
Alex
You have to return the main class, not the result from building. Here I'm using Builder.load_string() for convenience, and not importing player, but otherwise the only substantive change is that I changed return kv to return AndroidApp()
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
KV = '''
#:import utils kivy.utils
<AndroidApp>:
FloatLayout:
canvas.before:
Color:
rgb: utils.get_color_from_hex("#ffffff")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 1
cols: 2
Label:
text:"Android Vibrate"
Button:
text:"Android Vibrate"
on_press:
root.vibrate()
'''
class AndroidApp(GridLayout):
def vibrate(self):
vibrator.vibrate()
Builder.load_string(KV)
class MainApp(App):
def build(self):
return AndroidApp()
if __name__ == "__main__":
MainApp().run()

Kivy ActionDropDown does not open

In the kivy gui below, when I press the button 'Pen', an ActionDropDown is supposed to come. I do not see it opening. But the 'Pen' button behaves as if a DropDown was opened with it (e.g. you can not press on it on the first try). What is wrong?
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.actionbar import ActionDropDown
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
Builder.load_string("""
<NoteScreen>:
BoxLayout:
orientation: 'vertical'
NoteScreenMenu:
Label:
text: 'i am a label'
<NoteScreenMenu>:
orientation: 'horizontal'
size_hint_y: None
height: 48
ActionBar:
pos_hint: dict(top=1)
ActionView:
use_separator: True
ActionPrevious:
title: 'Drawing Screen'
with_previous: False
ActionOverflow:
ActionButton:
id: pen_button
text: 'Pen'
on_release: root.pen_drop_down.open(self)
ActionButton:
text: 'Eraser'
<PenDropDown>:
Button:
text: 'Close this'
Button:
text: 'btn1'
""")
class NoteScreen(Screen):
pass
class NoteScreenMenu(BoxLayout):
def __init__(self, **kwargs):
super(NoteScreenMenu, self).__init__(**kwargs)
self.pen_drop_down = PenDropDown()
class PenDropDown(ActionDropDown):
pass
class TheApp(App):
def build(self):
return NoteScreen(name='note')
if __name__ == '__main__':
TheApp().run()
The problem was here:
<PenDropDown>:
Button:
text: 'Close this'
Button:
text: 'btn1'
I needed to add height information to buttons in the dropdown:
<PenDropDown>:
Button:
text: 'Close this'
height: 48
size_hint_y : None
Button:
text: 'btn1'
height: 48
size_hint_y : None

Kivy using the StackLayout

I want to use a StackLayout but it only display one button and not two as I expected. The code is:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
Builder.load_string("""
<ScreenUI>:
orientation: 'lr-bt'
Button:
text: 'Button 1'
Button:
text: 'Button 2'
""")
class ScreenUI(StackLayout):
pass
class WidgetApp(App):
def build(self):
app = ScreenUI()
return app
if __name__ == '__main__':
WidgetApp().run()
How do I use a StackLayout and add a List of Button?
Update (to includes comments): To obtain the resize behaviour I used
Builder.load_string("""
<ScreenUI>:
input1: input1
button1: button1
height: self.input1.height
spacing: 5
orientation: 'horizontal'
id: layout1
Label:
text: 'Button 1'
id: button1
size: len(self.text) * root.input1.font_size, 2 * root.input1.font_size
size_hint: None, None
TextInput:
id: input1
text: 'Button 2'
size: root.width - root.button1.width - root.spacing, 2 * self.font_size
size_hint: None, None
""")
Does a more elegant method exists?
It looks like this is a size_hint problem. The buttons have no size specified, so the StackLayout automatically sizes them with their size hints. However, its layout doesn't depend on any sensible way of doing that, so it just makes them 100% of its own size...meaning only one fits in the window. That's the one you see.
Here's a slightly modified version of your example where sizes are specified. It works fine for me, with two differently sized buttons placed in the bottom left:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
Builder.load_string("""
<ScreenUI>:
orientation: 'lr-bt'
Button:
text: 'Button 1'
size: 100, 100
size_hint: None, None
Button:
text: 'Button 2'
size_hint: None, None
size: 200, 100""")
class ScreenUI(StackLayout):
pass
class WidgetApp(App):
def build(self):
app = ScreenUI()
return app
if __name__ == '__main__':
WidgetApp().run()
In general, the way to fix this is to give them a sensible size hint for whatever your particular goal is, or to set their sizes manually as I did. If you want a certain number to be sized properly to fill a space or similar, a GridLayout or BoxLayout may be more suitable.

How to create simple Kivy app (form with field name)

How to create simple Kivy app?
If user type text into field "Name" using keyboard like on android
phone, this name is display
I need this to learn
Here's as simple an example as i can figure out.
Here's the KVLANG code.
<LblTxt#BoxLayout>:
orientation: 'horizontal'
lblTxtIn: 'default'
theTxt: iAmTxt
Label:
text: root.lblTxtIn
TextInput:
id: iAmTxt
text: 'txt'
<MyLayout#BoxLayout>:
orientation: 'vertical'
LblTxt:
id: lt0
lblTxtIn: 'LblTxtInput0'
LblTxt:
id: lt1
lblTxtIn: 'LblTxtInput1'
LblTxt:
id: lt2
lblTxtIn: 'LblTxtInput2'
Button:
text: 'print LblTxtInput [0, 1, 2]'
on_release: print lt0.theTxt.text, lt1.theTxt.text, lt2.theTxt.text
MyLayout
Here's the Python code.
import kivy
kivy.require('1.8.0') # replace with your current kivy version !
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.core.window import Window
Window.size = (400,130)
from kivy.uix.boxlayout import BoxLayout
class LblTxt(BoxLayout):
from kivy.properties import ObjectProperty
theTxt = ObjectProperty(None)
class MyApp(App):
def build(self):
self.root = Builder.load_file('simpleForm.kv')
return self.root
if __name__ == '__main__':
MyApp().run()
Here's a run screenshot. It will print a b c to the command line when the 'print LblTxtInput [0, 1, 2]' button is released.
I hope this helps you out.

Resources