AttributeError: 'Snackbar' object has no attribute 'show' - python

I'm making a desktop app using kivymd and i need to show a simple Snackbar on a button click that just lets the user know that the process went smoothly, but no matter what i try to do i get the error message: AttributeError: 'Snackbar' object has no attribute 'show'.
I installed kivy and kivymd correctly, i know this because i already made an app without using Snackbars. Hope you can help me, here's the code:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.snackbar import Snackbar
KV = '''
BoxLayout:
MDRaisedButton:
text: "click"
on_press: app.test_bar()
'''
class TestApp(MDApp):
def build(self):
return Builder.load_string(KV)
def test_bar(self):
Snackbar(text="Hello world!").show()
TestApp().run()

Snackbar(text="Hello world!").open()

Related

KivyMD BottomNavigation with different .kv files

Im working on an mobile calculator app. Please be aware that this is my first own project.
The app should be able to offer a simple calc advanceded and should display different functions.
I achive this with the BottomNavigation but the file gets huge. Any troubleshooting is not possible at least not for me.
with the screenmanager I could separate the different screens into different files but I cannot do this BottomNavigator.
Could Anybody give me a hint please.
working with the screenmanager
found it just in case anybody doesnt know either:
#main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.bottomnavigation.bottomnavigation import MDBottomNavigationItem
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
return Builder.load_file("my.kv")
MainApp().run()
#my.kv
#:include advanced_calc_screen.kv
BoxLayout:
orientation: "vertical"
MDBottomNavigationItem:
name: "advanced_calc"
text: "advanced"
icon: "calculator"
Advanced:
#advanced_calc_screen.kv
<Advanced>:
Button:
text: "You are at Homescreen"

How to repair an error "Pyperclip could not find a copy/paste mechanism in your device" in Python Kivy?

I am using Pyperclip library in Python in Kivy UI, and I want to be able to copy a text by pressing a button, but it gives me this error message:
pyperclip.PyperclipException:
Pyperclip could not find a copy/paste mechanism for your system.
For more information, please visit https://pyperclip.readthedocs.io/en/latest/index.html#not-implemented-error
My .py file is:
import kivy
import pyperclip
kivy.require("1.10.1")
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.app import App
class Screen1(Screen):
def copy_on_press(self):
pyperclip.copy("text_which_should_be_copied")
class Copy(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Screen1(name = "scr1"))
return sm
app = Copy()
app.run()
My .kv file is:
<Screen1>:
id: scr1
orientation: "vertical"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "Background.png"
Button:
text: "Copy"
on_press: scr1.copy_on_press()
Thanks for any answer.
I haven't found any informations which could solve my error.
I found similar errors, but their solutions didn't work for me.
Thanks to the comments, I've found it. The .py:
import kivy
from kivy.core.clipboard import Clipboard
kivy.require("1.10.1")
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.app import App
class Screen1(Screen):
def copy_on_press(self):
Clipboard.copy("text_which_should_be_copied")
class Copy(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Screen1(name = "scr1"))
return sm
app = Copy()
app.run()
The .kv remains the same.

Unable to change kivy icon

I'm trying to change icon from kivy default one to some ico/png file that i provide
But it seems to be not working
I get this
Code:
from kivymd.app import MDApp
from kivy.lang import Builder
KV="""
Button:
text:""
"""
class Main(MDApp):
def build (self):
self.icon="image.png"
return Builder.load_string(KV)
Main().run()
Update:
Fixed the problem by changing the png,
But it still shows kivy logo for the loading screen ,is there any fix for this.???

Kivy Python - Button to increment variable, without using NumericProperty

I'm a complete novice to both python and kivy having learnt python from codeacademy about a week ago and kivy from youtube tutorials.
Could someone please explain to me why the code below does not result in a screen with a label displaying n, which is incremented by the button?
Python file
import kivy
from kivy.app import App
from kivy.uix.button import Button, Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
class Example(BoxLayout):
n = 0
def n_plus(self):
self.n += 1
class ExampleApp(App):
def build(self):
return Example()
example = ExampleApp()
example.run()
KV file
<Example>:
BoxLayout:
Label:
text: str(root.n)
Button:
text: "+1"
on_press: root.n_plus()
Then could you explain why making n = NumericProperty(0) makes this work?
I'd like to run some functions on n which don't seem to work on numeric properties.
Because when you use NumericProperty()
As the official document said:
It produces events such that when an attribute of your object changes,
all properties that reference that attribute are automatically
updated.
So, in short, it creates a binding relationship between your UI(.kv) and attribute of its class(.py)
But, actually, you can modify the UI by yourself without the help from the kivy framework. I changed your example as the following:
Add an id attribute to your widget
Access the id attribute by using self.ids.your_id_in_kv_file
But it's obviously not good, since now you need to update your UIby yourself everytime you want to update your UI. But with that XXXXProperty from kivy, you just need to change the value of that attribute, you don't need to worry about the UI at all.
Another disadvantage of the solution is that, when you need to change the UI, you need to change tons of code if you modify them all by yourself...
Here is the example:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string("""
<Example>:
BoxLayout:
Label:
id: lbl
text: "0"
Button:
text: "+1"
on_press: root.n_plus()
""")
class Example(BoxLayout):
def n_plus(self):
value = self.ids.lbl.text
self.ids.lbl.text = str(int(value) + 1)
class ExampleApp(App):
def build(self):
return Example()
if __name__ == '__main__':
ExampleApp().run()

Kivy: how to instantiate a dynamic classes in python

I'm having a hard time trying to figure out how to instantiate a dynamic class I created using kv lang on my python code, consider the following code:
My test.kv file looks like this:
<MyPopup#Popup>:
title:'hello'
size_hint:(1, .6)
GridLayout:
id:root_grid
cols:2
padding:['8dp', '4dp','8dp','4dp']
spacing:'8dp'
Label:
text:'some text here'
Button:
text:'Ok'
on_press:do_something()
<MyGrid>:
rows:1
Button:
text:'Show Popup'
on_press:root.pop.show()
Then in my test.py:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.factory import Factory
class MyGrid(GridLayout):
pop = Factory.MyPopup()
pass
class Test(App):
def build(self):
return MyGrid()
if __name__=='__main__':
Test().run()
The I get the following error: raise FactoryException('Unkown class <%s>' % name) kivy.factory.FactoryException: Unkown class
Can somebody please explain me how to properly do it, what am I missing? If you need any more information please let me know. Thanks.
Your call to the Factory takes place before the kv file is loaded, therefore the class you want does not yet exist.
Unless there is some reason to need a class level attribute, set self.pop in the __init__ of MyGrid instead.
You could also just include a Python class declaration. I generally prefer to do this for anything that I want to interact with from python, though opinions vary.

Categories