How to change screen by swiping in kivy python - python

I am trying to change to another screen by swiping the screen. I've tried carousel but it seems that it only works with images, so I've tried detecting a swipe motion and changing the screen after it has been detected.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class HomeScreen(Screen):
def on_touch_move(self, touch):
if touch.x < touch.ox: # this line checks if a left swipe has been detected
MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen
class ImageButton(ButtonBehavior, Image):
pass
class LabelButton(ButtonBehavior, Label):
pass
class SettingsScreen(Screen):
pass
class SwipeDhikrScreen(Screen):
pass
#def quit_verification():
# pop = Popup(title="verification", content=Label(text= "Are you sure?"))
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
# get the screen manager from the kv file
screen_manager = self.root.ids["screen_manager"]
screen_manager.transition.direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()
MainApp().run()
I got an attribute error: "None type object has no attribute 'ids'"

MainApp().change_screen(screen_name="swipedhikr_screen")
This line creates a new instance of MainApp, which doesn't have any widgets and therefore naturally fails when you try to access them.
Use the existing instance of MainApp, i.e. the one that you're actually running, via MainApp.get_running_app().
Also you are not correct that Carousel works only with images.

Related

MDSlider Bug in KivyMD - on_touch_up gets fired when clicked on the screen

I am facing a problem, where I want to get the value of the MDslider in KivyMD using the on_touch_up() method, it works but the problem is even when I click/touch anywhere on the screen apart from the MDSlider the on_touch_up() method gets fired.
And when I click/touch on the MDSlider the method gets fired twice
Code
from kivy.lang import Builder, builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.slider import MDSlider
from kivymd.uix.gridlayout import MDGridLayout
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
self.slider = MDSlider(
min=0,
max=100,
size_hint=(0.4,0.1),
pos_hint={'center_x':0.5, 'center_y':0.5}
)
self.slider.bind(on_touch_up = self.get_slider_value)
self.add_widget(self.slider)
def get_slider_value(self, obj, obj_prop):
print(obj.value)
class MyApp(MDApp):
def build(self):
return MyScreen()
if __name__ == "__main__":
MyApp().run()
Thank you for your time :)
That's not a bug, it is the designed behavior. All Widgets get the on_touch_up event. You must use the collide_point(*touch.pos) method to determine if the touch is on your Slider. However, the on_touch_up() being fired twice does seem like a bug. Consider using:
self.slider.bind(value = self.get_slider_value)
instead of on_touch_up.
To use collide_point() in the get_slider_value() (if you bind to on_touch_up) try something like:
def get_slider_value(self, slider, touch):
if slider.collide_point(*touch.pos):
print(slider.value)

Input methods in Kivy

I was trying to get continuous touch motion in Kivy to move a rectangle for which I wrote a code
from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.core.window import Window
class Game(Widget):
def __init__(self,**kwargs):
super().__init__(**kwargs)
with self.canvas:
Color=(0,1,0,1)
self.player=Rectangle(pos=(50,0),size=(20,50))
#touch and motion of our player
def on_motion(self,etype,motionevent,**kwargs):
print('hello')
touch_condition=False
x_touch=motionevent.spos[0]
xa=self.player.pos[0]
ya=self.player.pos[1]
if etype=='begin' or etype=='update':
touch_condition=True
if etype=='end':
touch_condition=False
if touch_condition and x_touch>0.5:
xa+=10
if touch_condition and x_touch<0.5:
xa-=10
self.player.pos=(xa,ya)
Window.bind(on_motion=on_motion)
class Try(App):
def build(self):
return Game()
if __name__=="__main__":
Try().run()
On mouse click as an input its giving an error
File "kiv.py", line 21, in on_motion
xa=self.player.pos[0] AttributeError: 'WindowSDL' object has no attribute 'player'
I read all documentation on Kivy inputs and Window.bind but I am still unable to understand how to solve it.
Your class structure is messed up, you're binding on_motion at the class level. I'm surprised this is the error you get, but that looks like the problem.
Add Window.bind(on_motion=self.on_motion) in the __init__ of your Game widget instead.

kivy how to use online images in python

I'm trying to make a button which uses an image from URL.
icon= str('https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png')
self.add_widget(ImageButton(source=(icon), size=(100,100), size_hint=(0.1, 0.1), on_press=callback, pos_hint={"x":0.90, "top":1.0}))
class ImageButton(ButtonBehavior, Image):
pass
It shows as an white image for some reason. Could anyone help me please?
As eyllanesc comments, you must use AsyncImage subclass to load the image asynchronously from the server. Otherwise, the image will not be available when you instantiate the widget.
On the other hand, the code you show in your comment;
icon = AsyncImage(source='https://.../icon.png')
self.add_widget(ImageButton(source=(str(icon)))
is also incorrect, You are trying to pass to source (StringPropery) an instance of AsyncImage. The simple solution to that is to inherit from AsyncImage and not from Image:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
class ImageButton(ButtonBehavior, AsyncImage):
pass
class MainWindow(BoxLayout):
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
icon = 'https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png'
self.add_widget(ImageButton(source=icon))
class Test(App):
def build(self):
return MainWindow()
if __name__ == "__main__":
Test().run()

kivy python widget instance or all widgets

Please help me with understanding classes/instances in python. I want to make a few buttons, and change color of the button, when it's clicked. I don't understand why on_touch_down changes the color of all the instances of the class, not the one that is touched. It's difficult for me to find answer because I don't know how to name it, I don't have much experience with objects. Please explain this. Thank you a million.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.graphics import Color, Ellipse
class MemoWidget(Button):
def on_touch_down(self, touch):
self.background_color=[100,100,1,1]
class MyApp(App):
def build(self):
root = BoxLayout(orientation='vertical',spacing=4)
m1 = MemoWidget()
m2 = MemoWidget()
m3 = MemoWidget()
root.add_widget(m1)
root.add_widget(m2)
root.add_widget(m3)
return root
if __name__ == '__main__':
MyApp().run()
You might think that on_touch_down only affects the widget you touch. But it affects all widgets of that class.
So what you might want, is on_press or on_release, to only affect the widget itself.
class MemoWidget(Button):
def on_release(self):
self.background_color=[100,100,1,1]

FactoryException when trying to create a "IconButton" in Kivy

I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.

Categories

Resources