Auto Sweep/Scroll Carousel in KivyMD (Python) - python

I want to make a 2 Slide Carousel with auto scrolling in KivyMD with Python. On Startup, the App starts from the The first Slide of Carousel and it should be changed to 2nd slide after 3 seconds.
Here is my Code
.kv
<WelcomeScreen>:
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: caraousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
size_hint: .3, .3
MDLabel:
text: "Slide 1"
pos_hint: {"center_y": .087}
halign: "center"
font_name: "Poppins-Light"
font_size: "14sp"
color: rgba(135, 143, 158, 200)
MDFloatLayout:
Image:
source: "Assets/2.jpg"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint: .8, .8
MDLabel:
text: "Slide 2"
pos_hint: {"center_y": .47}
halign: "center"
font_name: "Poppins-Regular"
font_size: "25px"
color: rgba(1, 3, 23, 225)
.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager, NoTransition
from kivy.utils import rgba
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.clock import Clock
Window.size = (310, 580)
class WelcomeScreen(Screen):
pass
class AppApp (MDApp):
def build(self):
return Builder.load_file('app.kv')
def current_slide(self, index):
pass
AppApp().run()
Anyone can help me with this Issue? Thanks in Advance.

You can use method Clock.schedule_interval to perform automatic loading of sliders. Trigger that action from anywhere in your code, for example to make it happen from the very beginning, trigger it from the method on_start of class app as,
app.kv file.
<WelcomeScreen>:
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: caraousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
size_hint: .3, .3
MDLabel:
text: "Slide 1"
pos_hint: {"center_y": .087}
halign: "center"
# font_name: "Poppins-Light"
font_size: "14sp"
color: rgba(135, 143, 158, 200)
MDFloatLayout:
Image:
source: "Assets/2.jpg"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint: .8, .8
MDLabel:
text: "Slide 2"
pos_hint: {"center_y": .47}
halign: "center"
# font_name: "Poppins-Regular"
font_size: "25px"
color: rgba(1, 3, 23, 225)
main.py file.
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager, NoTransition
from kivy.utils import rgba
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.clock import Clock
Window.size = (310, 580)
class WelcomeScreen(Screen):
pass
class AppApp(MDApp):
def build(self):
Builder.load_file('app.kv')
return WelcomeScreen()
def on_start(self):
# Access the carousel.
carousel = self.root.ids.caraousel
# Set infinite looping (optional).
carousel.loop = True
# Schedule after every 3 seconds.
Clock.schedule_interval(carousel.load_next, 3.0)
def current_slide(self, index):
pass
AppApp().run()
Update:
Try avoiding the App's instance name as AppApp and related .kv file as app.kv simultaneously, that might cause problems during auto .kv loading. However loading the file explicitly and then returning the root from method build (or declaring in .kv file) should work.

Related

KivyMD when switching between screens the palette and the elevation gets lost

I found that whenever I have multiple screens (managed by the MDScreenManager) and I switch between them, certain properties (the elevation of the cards on the screens, or the changes on the primarly palette) are lost.
Run the project:
Switch to dark mode (theme and palette changes working fine):
Change to the second screen (in dark or light mode, doesn't matter):
Come back to the first creen (I already lost the MDCard elevation):
When switching between themes, the primarly palette has been lost on the dark mode:
This is my main.py:
from kivy.config import Config
Config.set('graphics', 'fullscreen', 0)
Config.set('graphics', 'window_state', 'maximized')
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.screen import MDScreen
from kivy.core.window import Window
class Home(MDScreen):
pass
class Settings_1(MDScreen):
pass
Window.size = (800, 480)
sm = MDScreenManager()
sm.add_widget(Home(name='home'))
sm.add_widget(Settings_1(name='settings_1'))
class Test(MDApp):
dialog = None
def build(self):
self.theme_cls.primary_palette = "Blue"
self.theme_cls.theme_style = "Light"
self.theme_cls.theme_style_switch_animation = True
self.theme_cls.material_style = "M3"
return Builder.load_file('kivy.kv')
def on_checkbox_active(self, value):
if not value.active:
self.theme_cls.primary_palette = "Gray"
self.theme_cls.theme_style = "Dark"
else:
self.theme_cls.primary_palette = "Blue"
self.theme_cls.theme_style = "Light"
return
if __name__ == "__main__":
print('Starting')
Test().run()
and this the kivy.kv file:
ScreenManager:
Home:
Settings_1:
<Home>:
name:'home'
MDCard:
elevation: 3
orientation: 'vertical'
adaptive_height: True
radius: 20
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: 0.3, 0.3
md_bg_color: app.theme_cls.bg_darkest
padding: dp(5), dp(10)
spacing: dp(10)
MDLabel:
text: 'Label 1'
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.8}
MDSeparator:
MDSwitch:
active: True
icon_active: "white-balance-sunny"
icon_inactive: "moon-waning-crescent"
on_active: app.on_checkbox_active(self)
theme_text_color: app.theme_cls.theme_style
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDSeparator:
MDRectangleFlatButton:
text: 'Next screen'
pos_hint: {'center_x': 0.5,'center_y':0.2}
on_press:
root.manager.current ='settings_1'
root.manager.transition.direction = 'left'
<Settings_1>:
name:'settings_1'
MDCard:
elevation: 3
orientation: 'vertical'
adaptive_height: True
radius: 20
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: 0.3, 0.3
md_bg_color: app.theme_cls.bg_darkest
padding: dp(5), dp(10)
spacing: dp(10)
MDLabel :
text: 'Label 2'
halign: 'center'
MDRectangleFlatButton:
text: 'Previous screen'
pos_hint: {'center_x': 0.5,'center_y':0.2}
on_press:
root.manager.current ='home'
root.manager.transition.direction = 'right'
I had that problem too, turns out that its a bug in kivyMD version 1.1.1, it is solved when I upgraded to version 1.2.0, as recommended in: https://github.com/kivymd/KivyMD/issues/1439#issuecomment-1365039955
BTW, it will demend upgarding to a non-released version of kivy as well (2.2.0.dev0) which was the main difficulty here - as wheels are not available for this version, I had to install some additional GL files for it to work.

I can't read the value of text input in Kivy

My problem is when I want to display the value given by the user in the Text input text field.
When I want to get the text value from the input I get an empty value and I cannot display anything in the label field .
Here is my code :
The kv file :
MainGridLayout:
<MainGridLayout>:
cols: 1
rows: 2
BoxLayout:
orientation: 'vertical'
TextInput:
id: my_text_input
text: root.text_input_1
hint_text :'Operations'
multiline:False
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
font_size: '30dp'
Label:
text: root.Display_text
font_size: '45dp'
StackLayout:
Button:
text:"+"
spacing: .2, .2
size: 85, 85
size_hint: None, None
on_press: root.on_button_plus()
The py file :
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
# different layouts:
class MainGridLayout(GridLayout):
text_input_1 = StringProperty()
Display_text = StringProperty()
def on_button_plus(self):
print(f'{self.text_input_1}')
# run application:
class CalculatorApp(App):
pass
CalculatorApp().run()
I have created a function in MainGridLayout, on_new_text, that takes as a parameter a TextInput and updates text_input_1 with its value.
In the kv file I have set the on_text property of the TextInput to the on_new_text function and gave the TextInput as a parameter. Every time you modify the text input, the on_new_text function will be called, modifying the text_input_1 property.
You can find more properties like on_text in the kivy documentation.
The kv file:
MainGridLayout:
<MainGridLayout>:
cols: 1
rows: 2
BoxLayout:
orientation: 'vertical'
TextInput:
id: my_text_input
hint_text :'Operations'
multiline:False
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
font_size: '30dp'
on_text: root.on_new_text(self)
Label:
text: root.Display_text
font_size: '45dp'
StackLayout:
Button:
text:"+"
spacing: .2, .2
size: 85, 85
size_hint: None, None
on_press: root.on_button_plus()
The py file:
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
# different layouts:
class MainGridLayout(GridLayout):
text_input_1 = StringProperty()
Display_text = StringProperty()
def on_new_text(self, text_input):
self.text_input_1 = text_input.text
def on_button_plus(self):
print(f'{self.text_input_1}')
class CalculatorApp(App):
pass
CalculatorApp().run()
Actually, you don't need the variables text_input_1 and Display_text. To display the text from the my_text_input on the label you can add text: my_text_input.text instead of text: root.Display_text. Now, when the text of my_text_input changes, the label's text will change automatically and you don't need any functions. In on_button_plus() method you can do the same and print my_text_input's text using its text property (from all MainGridLayout's ids you take only my_text_input and print its text). The line text: root.text_input_1 unnecessary, too.
Here is my editted version of your code:
The .py file
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
class MainGridLayout(GridLayout):
def on_button_plus(self):
print(f'{self.ids.my_text_input.text}')
class CalculatorApp(App):
pass
CalculatorApp().run()
The .kv file
MainGridLayout:
<MainGridLayout>:
cols: 1
rows: 2
BoxLayout:
orientation: 'vertical'
TextInput:
id: my_text_input
hint_text :'Operations'
multiline:False
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
font_size: '30dp'
Label:
text: my_text_input.text # change the text automatically
font_size: '45dp'
StackLayout:
Button:
text:"+"
spacing: .2, .2
size: 85, 85
size_hint: None, None
on_press: root.on_button_plus()

How to integrate elements from KV to Python?

I'm trying to make a simple application that uses buttons to cycle through different strings with a label, but how would one get the strings from the .py file to the KV string?
For example, I want it to say '[x_string]' in the middle of the screen. When the user presses a button (arrow facing right), it goes to '[y_string]' instead. When the user presses the other button (arrow facing left), it goes back to '[x_string]', etc.
Here is the code I have so far.
from PyDictionary import PyDictionary
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem
from kivymd.uix.menu import MDDropdownMenu
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.toolbar import MDToolbar
from kivy.core.window import Window
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivymd.uix.swiper import MDSwiper
from kivymd.uix.label import MDLabel
from pathlib import Path
KV = '''
ScreenManager:
Screen1:
<Screen1>:
name: 'screen'
MDScreen:
name: 'screen'
MDToolbar:
title: 'Test App'
elevation: 20
pos_hint: {'top': 1}
md_bg_color: (1, 1, 1, 1)
specific_text_color: (76/255, 76/255, 77/255, 1)
right_action_items: [["information"],["home"]]
FloatLayout:
# Left Arrow
Button:
background_normal: 'buttonsNormal/chevron_arrow_left.png'
background_down: 'buttonsDown/chevron_arrow_left_down.png'
size_hint: (0.06, 0.16)
pos_hint: {'center_x': 0.06, 'center_y': 0.445}
border: [0, 0, 0, 0]
# Right Arrow
Button:
background_normal: 'buttonsNormal/chevron_arrow_right.png'
background_down: 'buttonsDown/chevron_arrow_right_down.png'
size_hint: (0.06, 0.16)
pos_hint: {'center_x': 0.94, 'center_y': 0.445}
border: [0, 0, 0, 0]
'''
Window.size = (1280, 720)
class Screen1(Screen):
def build(self):
MDLabel(
text = 'Raise your arm.',
font_size = dp(56),
halign = 'center'
)
class MainApp(MDApp):
dialog = None
def build(self):
sm = ScreenManager()
sm.add_widget(Screen1(name = 'follow_screen'))
return Builder.load_string(KV)
MainApp().run()
I am leaving out about 800 lines of completely unrelated code, so that's why there are so many imported modules as the top.
Thank you!
You can add a Label to the Screen1 kv:
Label:
text: root.text
size_hint: 0.25, 0.25
color: 0,0,0,1
size: self.texture_size
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
The text for this Label is set to root.text, which should be a StringProperty in the Screen1 class:
class Screen1(Screen):
text = StringProperty('x_string')
Then, you can adjust the value of the StringProperty Like this:
# Left Arrow
Button:
background_normal: 'buttonsNormal/chevron_arrow_left.png'
background_down: 'buttonsDown/chevron_arrow_left_down.png'
size_hint: (0.06, 0.16)
pos_hint: {'center_x': 0.06, 'center_y': 0.445}
border: [0, 0, 0, 0]
on_press:
root.text = 'x_string'
# Right Arrow
Button:
background_normal: 'buttonsNormal/chevron_arrow_right.png'
background_down: 'buttonsDown/chevron_arrow_right_down.png'
size_hint: (0.06, 0.16)
pos_hint: {'center_x': 0.94, 'center_y': 0.445}
border: [0, 0, 0, 0]
on_press:
root.text = 'y_string'

Kivy Python using a button to open a .kv file to change the GUI

I'm trying to use a button to cycle through GUI displays on a screen
Here is the code that i've tried for using a main file and then the 2 extra files. The code itself works i keep getting a "Builder is not defined" error which won't load the new file onto screen
The goal is to have a button which will change screens to the next screen and back again
Any help is much appreciated
Main file
# import subprocess
# import kivy
from kivy.uix.button import Button
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '300')
class CustomRow(FloatLayout):
pass
class CustomGrid(FloatLayout):
pass
class RotationApp(App):
OilTemp = NumericProperty(0)
print(OilTemp)
def build(self):
print("building...")
return Builder.load_file('oiltemp.kv')
# the __name__ idiom executes when run from command line but not from import.
if __name__ == '__main__':
RotationApp().run()
Second file
<oiltemp>:
CustomRow:
FloatLayout:
id: 0
FloatLayout:
id: 1
# etc, creating as many FloatLayouts as many number of columns you want.
CustomGrid:
CustomRow:
id: 0
CustomRow:
id: 1
Image:
source: 'OilTempV2.png'
size: self.texture_size
Image:
source: 'NeedleV1.png'
size: self.texture_size
pos_hint: {'center_x': .5, 'center_x': .5}
Button:
text: 'Next Screen'
size_hint: None, None
pos_hint: {'left_x': .5, 'bottom_y': .5}
canvas.before:
PushMatrix
Rotate:
angle: 0
origin: self.center
canvas.after:
PopMatrix
Third file
<airtemp>:
CustomRow:
FloatLayout:
id: 0
FloatLayout:
id: 1
# etc, creating as many FloatLayouts as many number of columns you want.
CustomGrid:
CustomRow:
id: 0
CustomRow:
id: 1
Image:
source: 'AirTempV1.png'
size: self.texture_size
Image:
source: 'NeedleV1.png'
size: self.texture_size
pos_hint: {'center_x': .5, 'center_x': .5}
Button:
text: 'Next Screen'
size_hint: None, None
pos_hint: {'left_x': .5, 'bottom_y': .5}
canvas.before:
PushMatrix
Rotate:
angle: 0
origin: self.center
canvas.after:
PopMatrix

Python : How add a datepicker in .kv file

Can anyone tell me how to add a date picker or kivy calendar on date TextBox?
I have two file test.py and test.kv file.
test.py
import kivy
import sqlite3 as lite
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.label import Label
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 125)
class Testing(Screen):
pass
class Testing(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Testing().run()
test.kv
Testing:
BoxLayout:
orientation: "vertical"
padding : 20, 20
BoxLayout:
orientation: "horizontal"
padding: 10, 10
spacing: 10, 10
size_hint_x: .55
Label:
text: "Date"
text_size: self.size
valign: 'middle'
size_hint_x: .2
TextInput:
size_hint_x: .3
BoxLayout:
orientation: "horizontal"
padding : 10, 0
spacing: 10, 10
size_hint: .5, .7
pos_hint: {'x': .25, 'y':.25}
Button:
text: 'Ok'
on_release:
root.dismiss()
Button:
text: 'Cancel'
on_release: root.dismiss()
.kv file
#:import MDTextField kivymd.uix.textfield.MDTextField
MDTextField:
id: set_date
hint_text: 'Start Date'
write_tab: False
on_focus: root.setDate()
.py file
from kivymd.uix.picker import MDDatePicker
def setDate(self,dobj):
self.ids.set_date.text = str(dobj)
pass
def fromDate(self):
self.foc = self.ids.set_date.focus
if(self.foc==True):
MDDatePicker(self.setFDate).open()
else:
print("not")

Categories