How to access a property in kv from python script in kivy - python

Following is the main.py:
from kivy.app import App
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *
class EmotionsScreen(Screen):
def button_press(self, emotion_str):
print (self.ids[emotion_str].emotion)
class ScreenManagement(ScreenManager):
pass
class HappyButton(ButtonBehavior, Image):
def on_press(self):
print(self.emotion)
class SadButton(ButtonBehavior, Image):
def on_press(self):
print(self.emotion)
class TiredButton(ButtonBehavior, Image):
def on_press(self):
print(self.emotion)
class MyApp(App):
def build(self):
sm = ScreenManagement()
sm.current = 'Emotions'
return sm
if __name__=='__main__':
MyApp().run()
Following is the contents of myapp.kv file:
:
EmotionsScreen:
<EmotionsScreen>:
name:'Emotions'
HappyButton:
id: happy
source: "happy.png"
pos: (-200, 100)
emotion: "Happy"
SadButton:
id: sad
source: "sad.png"
pos: (0, 100)
emotion: "Sad"
TiredButton:
id: tired
source: "tired.png"
pos: (200, 100)
emotion: "Tired"
Upon running the application, and clicking on the three buttons, I am getting the following behavior:
Clicking the happy button prints "Sad" :)
Clicking the sad button prints "Tired"
Clicking the tired button prints "Tired"
The above happens even if I call a single callback (inside app, with arguments supplied), as follows:
main.py:
from kivy.app import App
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *
class EmotionsScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class ImageButton(ButtonBehavior, Image):
pass
class MyApp(App):
def build(self):
sm = ScreenManagement()
sm.current = 'Emotions'
return sm
def button_press(self, *args):
print (args)
if __name__=='__main__':
MyApp().run()
myapp.kv:
<ScreenManagement>:
EmotionsScreen:
<EmotionsScreen>:
name:'Emotions'
ImageButton:
source: "happy.png"
pos: (-200, 100)
on_press: app.button_press("Happy")
ImageButton:
source: "sad.png"
pos: (0, 100)
on_press: app.button_press("Sad")
ImageButton:
source: "tired.png"
pos: (200, 100)
on_press: app.button_press("Tired")
I expect it to print, "Happy", "Sad" and "Tired" upon clicking the three buttons respectively. May I know where I have gone wrong?

These images seem to be placed randomly. Example using buttons as mockups:
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *
from kivy.lang import Builder
Builder.load_string('''
<EmotionsScreen>:
name:'Emotions'
HappyButton:
id: happy
pos: (-200, 100)
text: "Happy"
emotion: "Happy"
SadButton:
id: sad
pos: (0, 100)
text: "Sad"
emotion: "Sad"
TiredButton:
id: tired
pos: (200, 100)
text: "Tired"
emotion: "Tired"
''')
class EmotionsScreen(Screen):
def button_press(self, emotion_str):
print (self.ids[emotion_str].emotion)
class ScreenManagement(ScreenManager):
pass
class HappyButton(Button):
def on_press(self):
print(self.emotion)
class SadButton(Button):
def on_press(self):
print(self.emotion)
class TiredButton(Button):
def on_press(self):
print(self.emotion)
class MyApp(App):
def build(self):
sm = ScreenManagement()
sm.add_widget(EmotionsScreen())
sm.current = 'Emotions'
return sm
if __name__=='__main__':
MyApp().run()
You should rather put a layout inside your screen to manage its widgets:
Builder.load_string('''
<EmotionsScreen>:
name:'Emotions'
BoxLayout:
HappyButton:
id: happy
text: "Happy"
emotion: "Happy"
SadButton:
id: sad
text: "Sad"
emotion: "Sad"
TiredButton:
id: tired
text: "Tired"
emotion: "Tired"
''')

Related

Screen no exists for Python's Kivy

I don't understand what I'm doing wrong. I'm learning Kivy but when I want to load the new screen it tells me that it doesn't exist.
edited : [ how to work with screen in kivy correctly ]
Error :
raise ScreenManagerException('No Screen with name "%s".' % name)
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "startup_screen".
main.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from startup_screen import StartupScreen
class MyApp(MDApp):
def build(self):
Window.size = (300, 500)
sm = ScreenManager()
self.title = "App Test"
return sm
def on_start(self):
self.root.current = "startup_screen"
MyApp().run()
startup_screen.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
kv = """
<StartupScreen>
name: 'startup_screen'
canvas:
Color:
rgb: (.93, .93, .93)
Rectangle:
size: self.size
pos: self.pos
Image:
id: img_logo
source: "resources/img/logo.png"
size_hint: (.8,.8)
pos_hint: {'center_x': .5, 'center_y': .6}
"""
class StartupScreen(Screen):
Builder.load_string(kv)
def __init__(self):
super(StartupScreen, self).__init__()
def on_enter(self, *args):
from kivy.clock import Clock
Clock.schedule_once(lambda dt: self.load_navigation(), 5)
#staticmethod
def load_navigation():
from kivymd.theming import ThemeManager
app = MDApp.get_running_app()
app.theme_cls = ThemeManager()
app.theme_cls.primary_hue = "600"
app.theme_cls.accent_palette = "Teal"
app.theme_cls.theme_style = "Light"
from navigation import NavigationScreen
app.root.current = "navigation"
The only problem with your code is that you are not creating a StartupScreen. Your kv string defines a rule for how a StartupScreen should be built, but it does not build one. A simple fix is to just add a line in your build() method to create the StartupScreen:
def build(self):
Window.size = (300, 500)
sm = ScreenManager()
sm.add_widget(StartupScreen()) # createe the StartupScreen
self.title = "App Test"
return sm

(Kivy) How do I display new content on my canvas in Kivy?

This should be a pretty easy fix. I'm new to Kivy. I'm trying to have the canvas cleared on a button press, and then display a new widget to essentially move to another page. When I run it and press the button, the canvas is cleared, but I get nothing from IntroPage.
Python Script:
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
class ABT(App):
def build(self):
return WelcomePage()
class WelcomePage(Widget):
def btn(self):
self.canvas.clear()
print('pressed')
return IntroPage()
class IntroPage(Widget):
def __init__(self):
pass
if __name__ == '__main__':
ABT().run()
KV File:
<WelcomePage>
canvas.before:
Color:
rgba: (.43,.51,.92,.26)
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols:1
size: root.width, root.height
Image:
source: 'abt1t.png'
size_hint: (1,.8)
Button:
text:"begin"
background_color: (.43,.51,.92,.26)
size_hint: (1,.2)
on_press: root.btn()
<IntroPage>
GridLayout:
cols:1
Label:
text:"This won't show up!"
Returning IntroPage from btn won't work, as btn isn't a build method.
The best way to implement multiple pages is probably to use ScreenManager:
from kivy.uix.screenmanager import ScreenManager, Screen
class ABT(App):
def build(self):
sm = ScreenManager()
sm.add_widget(WelcomePage(name="welcome"))
sm.add_widget(IntroPage(name="intro"))
return sm
class WelcomePage(Screen):
def btn(self):
print('pressed')
self.manager.current = "intro"
class IntroPage(Screen):
def __init__(self):
pass

kivymd Card scroll list items with the help of loop?

I am working on a mobile app and I want to create multiple of scoll items by using for loop but whenever I use for loop, gives me some error It has already parent widget. how can I make list of MDCard?
My App:
Click here
My Code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
Window.size = (450, 740)
kv = '''
ScreenManager:
Main:
<main>:
name: 'main'
video_list: video_list
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Video downloader'
ScrollView:
Screen:
id: video_list
'''
class Main(Screen):
pass
sm = ScreenManager()
sm.add_widget(Main(name='main'))
class Ytube(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls.colors = 'Red'
self.theme_cls.primary_palette = "Red"
self.root = Builder.load_string(kv)
image = AsyncImage(
source='https://static.hub.91mobiles.com/wp-content/uploads/2020/05/How-to-download-youtube-videos.jpg', size_hint=(1, .7), )
screen_id = self.root.get_screen('main').ids.video_list
for i in range(1):
card = MDCard(orientation='vertical', pos_hint={
'center_x': .5, 'center_y': .7}, size_hint=(.9, .4))
card.add_widget(image)
card.add_widget(MDLabel(
text='Phishing attacks are SCARY easy to do!! (let me show you!)', size_hint=(.6, .2), ))
screen_id.add_widget(card)
def build(self):
return self.root
if __name__ == "__main__":
Ytube().run()
Is there any way to make it
look like this.
The problem is that you are trying to use the same image widget for every MDCard. Any widget can only have one parent. You can only use that image widget once. You can fix that by moving the creation of the image widget inside the loop.
Also, a BoxLayout is a better choice for the child of a ScrollView, since it has a minimum_height property that you can use. Here is a modified version of your code that applies both those suggestions:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.image import AsyncImage
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
Window.size = (450, 740)
kv = '''
ScreenManager:
Main:
<main>:
name: 'main'
video_list: video_list
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Video downloader'
ScrollView:
do_scroll_x: False
BoxLayout:
id: video_list
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
'''
class Main(Screen):
pass
sm = ScreenManager()
sm.add_widget(Main(name='main'))
class Ytube(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls.colors = 'Red'
self.theme_cls.primary_palette = "Red"
self.root = Builder.load_string(kv)
screen_id = self.root.get_screen('main').ids.video_list
for i in range(5):
# Make a new image widget for each MDCard
image = AsyncImage(
source='https://static.hub.91mobiles.com/wp-content/uploads/2020/05/How-to-download-youtube-videos.jpg', size_hint=(1, .7), )
card = MDCard(orientation='vertical', pos_hint={
'center_x': .5, 'center_y': .7}, size_hint=(.9, None), height=200)
card.add_widget(image)
card.add_widget(MDLabel(
text='Phishing attacks are SCARY easy to do!! (let me show you!)', size_hint=(.6, .2), ))
screen_id.add_widget(card)
def build(self):
return self.root
if __name__ == "__main__":
Ytube().run()

How can I print the position of the pointer every time the mouse is moved over a widget in kivy?

I want to print out the position of the pointer (mouse) everytime it passes over a widget. Actually to be more specific I'd like to print the current position of the mouse everytime it moves. Is that at all possible?
Right now all I've managed to do is:
Print the position of the mouse whenever the mouse is clicked
(in two different ways)
Print the position of the widget when it's clicked
I'm sure it's easier than I think, thanks in advance.
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.input.motionevent import MotionEvent
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
def on_mouse_pos(self, pos):
if Window.mouse_pos == self.ids.example_button:
print("You've made it")
# With out this if statement it does print the position every time
# That the mouse is moved. But can I print "You've made it" every time
# that the mouse is moved over the position of a widget?
print(pos)
def test_print(self):
print(str(self.ids.example_button.pos))
Window.bind(mouse_pos = on_mouse_pos)
class MyScreenManager(Screen):
pass
root_widget = Builder.load_string('''
MyScreenManager:
MainScreen:
<MainScreen>:
name: 'Example'
Button:
id: example_button
text: 'Print position of widget when mouse touches it'
size_hint: 1, .05
pos_hint: {'x': 0, 'y': .5}
on_release: root.test_print()
''')
class TestApp(App):
def build(self):
self.title = 'Example Application'
return root_widget
if __name__ == '__main__':
TestApp().run()
A possible solution is to use mouse_pos:
from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MainScreen(Screen):
def on_mouse_pos(self, pos):
print(pos)
Window.bind(mouse_pos = on_mouse_pos)
class MyScreenManager(Screen):
pass
root_widget = Builder.load_string('''
MyScreenManager:
MainScreen:
<MainScreen>:
name: 'Example'
Button:
id: example_button
text: 'I am button'
size_hint: 1, .05
pos_hint: {'x': 0, 'y': .5}
''')
class TestApp(App):
def build(self):
self.title = 'Example Application'
return root_widget
if __name__ == '__main__':
TestApp().run()
Update:
If you want to verify that the mouse point is inside a widget you must use collide_point.
from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
Window.bind(mouse_pos = self.on_mouse_pos)
def on_mouse_pos(self, instance, pos):
if self.ids.example_button.collide_point(*pos):
print("You've made it")
class MyScreenManager(Screen):
pass
root_widget = Builder.load_string('''
MyScreenManager:
MainScreen:
<MainScreen>:
name: 'Example'
Button:
id: example_button
text: 'I am button'
size_hint: 1, .05
pos_hint: {'x': 0, 'y': .5}
''')
class TestApp(App):
def build(self):
self.title = 'Example Application'
return root_widget
if __name__ == '__main__':
TestApp().run()

Changing kivy widget properties within Class

I'm having difficulty figuring out how to change the text of a label within a kivy widget. For simplicity, I have a label set to 0 and I would like to change the text to read 30 in this example. However, I get the following error.
AttributeError: 'super' object has no attribute 'getattr'
I understand that I'm probably not properly targeting that widget and I am hoping someone can please explain how to specifically reference the text of this label (self.ids.mainel1temp.stuff_r.text = '30') to update (with more detail than fixing the code)
#!/usr/bin/kivy
import kivy
from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.switch import Switch
from kivy.uix.label import Label
from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
Builder.load_string("""
<Menuscreen>:
#Handling the gesture event.
ScreenManager:
id: manager
Screen:
id: main_screen
name:'main_screen'
stuff_r: mainel1temp
FloatLayout:
Label:
id: mainel1temp
size: self.texture_size
text:'0'
size_hint: None, None
text_size: 75,75
pos: 295,308
font_size:'20sp'
halign: 'center'
""")
class Thermostuff(Screen):
stuff_r = ObjectProperty(None)
def starttherm(self):
Clock.schedule_interval((self.read_temp), 1)
def read_temp(self, dt):
self.ids.mainel1temp.stuff_r.text = '30'
Thermrun = Thermostuff()
Thermrun.starttherm()
class MenuScreen(Screen):
pass
sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
You do a couple of things wrong here.
You dont want to put a ScreenManager inside a Screen
Only one ScreenManager is needed.
Then you can start the Clock in the __init__ of the Thermostuff(Screen)
Or if you want it to initiate on_enter you need to overrite that. In that case you might want to check somehow, if its allready started, so you wont have multiple clocks running.
Then when you create an ObjectProperty you dont need self.ids, because you allready created that property. So self.stuff_r is now the label.
I rewrote your example a bit, to demonstrate this.
Try this:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.clock import Clock
sm = """
#Handling the gesture event.
ScreenManager:
id: manager
MenuScreen:
Button:
text: "Go to Thermostuff"
on_release:
root.current = "main_screen"
Thermostuff:
name:'main_screen'
stuff_r: mainel1temp
FloatLayout:
Label:
id: mainel1temp
size: self.texture_size
text:'0'
size_hint: None, None
text_size: 75,75
pos: 295,308
font_size:'20sp'
halign: 'center'
"""
class Thermostuff(Screen):
stuff_r = ObjectProperty(None)
test_temp = 0
def __init__(self,**kwargs):
super(Thermostuff,self).__init__(**kwargs)
Clock.schedule_interval((self.read_temp), 1)
def read_temp(self, dt):
self.test_temp += 1
self.stuff_r.text = str(self.test_temp)
class MenuScreen(Screen):
pass
class TestApp(App):
def build(self):
return Builder.load_string(sm)
if __name__ == '__main__':
TestApp().run()

Categories