I am new on python and on kivyMD.
I am trying to change the colors of the project throw the them_cls function. However, the app shows the default colors even when I am changing the colors. For example, when I put Amber, green, or even Red, the buttons show the default blue color. This also happens with the general background of the app. When I put dark on the style theme color and run the app, it continues showing in white.
I don't know what to do. Here is the code
Phyton file:
from kivymd.uix.boxlayout import MDBoxLayout
class Ma(MDBoxLayout):
pass
class MyApp(MDApp):
def Build(self):
self.theme_cls.primary_palette = "Amber" #"Red" "Green"
self.theme_cls.theme_style = "Dark"
return 0
pass
MyApp().run()
Kivy file:
<Ma>:
MDRaisedButton:
text: "Hello World"
md_bc_color: app.theme_cls.primary_light
MDRectangleFlatButton:
text: "Hello World"
md_bg_color: app.theme_cls.primary_dark
Related
I am writing an app that has a need for the Kivy Color Wheel (https://kivy.org/doc/stable/api-kivy.uix.colorpicker.html) to choose a color value to assign to different users. However, I intend for it to be used on a phone screen, and as such I do not need the various sliders and color codes that make up the second half of the widget - simply tapping the desired colour is sufficient. I know that there is a question very similar to this that is answered, however this does not account for the .kv language that I am using in my program.
This is my .kv code:
<ColourScreen>:
Label:
text: 'Please Choose the colour for Team 1'
ColorPicker:
and my .py file:
class ColourScreen(Screen):
pass
class TheApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(ColourScreen(name='colour_screen'))
sm.current ='colour_screen'
return sm
def main():
Builder.load_file('menu.kv')
app = TheApp()
app.run()
if __name__ == '__main__':
main()
Could anybody help? Thanks in advance
Try using ColorWheel instead. You can use it in the kv as:
<ColourScreen>:
Label:
text: 'Please Choose the colour for Team 1'
ColorWheel:
on_color: root.color_selected(self.color)
And the color_selected() method:
class ColourScreen(Screen):
def color_selected(self, color):
print('got color', color)
Whenever I am trying to update my label in the app, it is always overlapping the previous text. I don't know what is the mistake I am making for it to happen. If anyone knows please guide me.
here is the .py file:
import json
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
Builder.load_file('rough.kv')
class MyLayout(Widget):
f=open('questions.json')
global data
data=json.load(f)
def printing_questions(self):
a=0
question='Blue whale is a large animal'
self.ids.question.text=''
self.ids.question.text=question #i am changing text here, but it overlaps
class rough(App):
def build(self):
return MyLayout()
if __name__=='__main__':
rough().run()
Here is the .kv file:
#:kivy 2.1.0
<MyLayout>:
BoxLayout:
orientation:'vertical'
size:root.width,root.height
Label:
id:question #in this label i am updating the text
text:'Questions coming soon...444444444444442222222222222222ssssssssss\n555555555555211111assssssssss'
multiline:True
Button:
text:'Next'
on_release:
root.printing_questions()
Not sure what exactly you are expecting. But if you want the text to wrap into its container (or, cease to overflow) you can modify its text_size property as,
Label:
id:question #in this label i am updating the text
text:'Questions coming soon...444444444444442222222222222222ssssssssss\\n555555555555211111assssssssss'
halign: "center" # To set in the middle.
text_size: self.width, None # Extends upto it's width.
The previous text is:'Hello my name is mrsomethng' and when i update the label with:'What was the original color of ferrari models' they are both overlapping.
i want to show only the updated label not the previous text with it
I have a raspberry pi. I am using a screen manager in kv. There are 4 screens. 1st screen has 3 buttons and other screens have a colorpicker from which I need to take values when the colorpicker is pressed.
I want to use the value from the. Kv file and use it in the py file for furthur processing of the colorwheel. Only valye important to me is colorpicker.value
User's Kivy App Features
I am using a screen manager in kv. There are 4 screens. 1st screen has
3 buttons and other screens have a colorpicker.
Solution - ColorPicker & ScreenManager with 1 Screen
Yes, in Python code/script, you can access the selected color of ColorPicker widget defined in kv file. Please refer to the small/skeleton example and output for details.
Note
The value of interest is ColorPicker().color, self.color or instance.color in the example.
Color Picker ยป color
color
The color holds the color currently selected in rgba format.
color is a ListProperty and defaults to (1, 1, 1, 1).
Snippets
kv file
<MainScreen>:
ColorPicker:
on_color:
root.on_color(self, self.color)
Python Script
class MainScreen(Screen):
def on_color(self, instance, value):
print("\non_color:")
print("\tvalue(rgba)={}".format(value))
print("\tcolor(rgba)={}".format(instance.color))
print("\tcolor(hex)={}".format(instance.hex_color))
print("\tcolor(hsv)={}".format(instance.hsv))
Example
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class ScreenManagement(ScreenManager):
pass
class MainScreen(Screen):
def on_color(self, instance, value):
print("\non_color:")
print("\tvalue(rgba)={}".format(value))
print("\tcolor(rgba)={}".format(instance.color))
print("\tcolor(hex)={}".format(instance.hex_color))
print("\tcolor(hsv)={}".format(instance.hsv))
class TestApp(App):
title = "Kivy ColorPicker Demo"
def build(self):
return ScreenManagement()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.11.0
<ScreenManagement>:
MainScreen:
name: 'main'
<MainScreen>:
ColorPicker:
on_color:
root.on_color(self, self.color)
Output
I'm developing a python app using Kivy.
Sometimes when I'm launching other python scripts without kivy imports a blank Kivy window opens randomly without any reason.
Is this a bug? Or am I missing something on the app closing?
I've seen this strange behaviour only under Windows (8), Python 2.7.
The window is completely white and seems not responding (I know it's a Kivy window from the little icon on the top-left corner), few seconds later the system says python.exe has stopped working.
This is not a bug. You have to add child widgets e.g. button, label, textinput, etc. into your root widget. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class MyRootWidget(FloatLayout):
pass
class TestApp(App):
title = "Kivy Demo"
def build(self):
return MyRootWidget()
if __name__ == '__main__':
TestApp().run()
test.kv
#:kivy 1.10.0
<MyRootWidget>:
canvas:
Color:
rgba: [1, 1, 1, 1] # White color
Rectangle:
size: self.width, self.height
BoxLayout:
orientation: "vertical"
Label:
font_size: 50
text: "[color=ff3333][b]Hello[/b][/color]"
markup: True
Label:
font_size: 20
text: "[color=3333ff]World[/color]"
markup: True
Output
I've defined two buttons: one in kv and one in Python. They are located in different screens and are used to navigate between them. What I found strange is that the button that was defined in Python successfully switched the screen, while the one defined in kv did not. Perhaps I'm not accessing the App class method properly?
Here is the code of the issue:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.button import Button
Builder.load_string('''
<MyScreen1>:
Button:
id: my_bt
text: "back"
on_release: app.back
''')
class MyScreen1(Screen):
pass
class TestApp(App):
def here(self, btn):
self.sm.current = "back"
def back(self, btn):
self.sm.current = "here"
def build(self):
self.sm = ScreenManager()
s1 = Screen(name = "here")
bt = Button(text = "here",
on_release = self.here)
s2 = MyScreen1(name = "back")
#s2.ids['my_bt'].bind(on_release = self.back)
self.sm.add_widget(s1)
s1.add_widget(bt)
self.sm.add_widget(s2)
return self.sm
TestApp().run()
So if I define the switching function in kv (on_release), I can't go to the "here" screen. But if I uncomment that line in Python and comment the on_release: app.back instead, everything works fine.
I'm pretty sure that this is the correct way to access the current app, since it doesn't give me any errors (which means that the method was successfully located)
That's a subtle difference between kv and python: In kv you actually have to write the callback as a function call (a python expression), in this case:
on_release: app.back(self)