Display Kivy Color Wheel only - python

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)

Related

Why cannot change the colors KivyMD

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

Home/Restart button in Kivy

I have made a random workout generator and i would like there to be a home button on the final screen so that users can go back to the first screen and generate a new workout (my attempts so far have ended up with a second workout overlapping the first or a completely blank screen after using root.clear_widgets).Some widgets are added in the .py file and some from .kv and i never initialised my screen classes, new to kivy/python so not sure if this will affect potential solutions... Would appreciate any help! I've included my app class which has a currently empty home method called from a button in my .kv file.
kv = Builder.load_file("my.kv")
class MyMainApp(App):
sm = WindowManager()
def home(self):
pass
def build(self):
Window.clearcolor = (1, 1, 1, 1)
return kv
if __name__ == "__main__":
MyMainApp().run()
I think what you are looking for is the ScreenManager
https://kivy.org/doc/stable/api-kivy.uix.screenmanager.html

Can we use ColorPicker in on of the screens in a kv lang and use that in our kivy file?

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

Why isn't kv binding of the screen change working?

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)

Loading one line of text from file to Kivy label

I want to make a simple program that is just showing definitions that are stored in text file.One label and button to show next definition. I try to do it with documentation but i cannot find how to load text into label. Can someone show me to some good resources or code samples ?
My code for now (i want to build in on top of example from kivy website):
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text = 'Hello world')
if __name__ == '__main__':
MyApp().run()
The easiest way to update widgets in the UI are by binding to their properties. This can be done in code, but the real power of kivy in my opinion comes from using it's declarative UI language. Using kv, you get automatic binding.
Here is a quick example of what you might do:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
kv = '''
BoxLayout:
orientation: 'vertical'
Label:
text: app.text
Button:
text: 'click me'
on_press: app.clicked()
'''
class MyApp(App):
text = StringProperty("hello world")
def build(self):
return Builder.load_string(kv)
def clicked(self):
self.text = "clicked!"
if __name__ == '__main__':
MyApp().run()
In the kv description of the UI, you tell kivy that you want the text on the Label to be bound to a StringProperty on the app which you defined on the class. The auto-binding means that anytime you set a value to that property (like in the clicked function), the UI will update with the new value automatically.

Categories