I need light/dark theme. I can make it work but I want another solution (I know about KivyMD too but in this case I don't want to use it).
In short: I have multiple screens and on all screens there is a Popup button.
On the Popup, there are two buttons: light and dark. So when I press light or dark it should changes the background color of the screens.
My problem is that I don't know how to access to Popup class from the .kv file. Could you give me a solution or a hint how to achive this?
Minimal code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.properties import ListProperty
Builder.load_string("""
<FirstScreen>:
canvas.before:
Color:
#--------------> how to access to popup class from here?
#rgba: ???.my_background_color
rgba: 240/255, 240/255, 240/255, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Popup'
size_hint: 0.5, 0.5
on_release:
root.the_popup()
<MyPopup>
size_hint: 0.75 , 1
title: 'T H E M E'
BoxLayout:
Button:
text: 'D A R K'
on_release:
root.dark()
Button:
text: 'L I G H T'
on_release:
root.light()
"""
)
class FirstScreen(Screen):
def the_popup(self):
the_popup = MyPopup()
the_popup.open()
class MyPopup(Popup):
my_background_color = ListProperty([240/255, 240/255, 240/255, 1])
def light(self):
self.my_background_color = [240/255, 240/255, 240/255, 1]
def dark(self):
self.my_background_color = [48/255, 48/255, 48/255, 1]
class myApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(FirstScreen(name='first'))
return sm
if __name__ == '__main__':
myApp().run()
Related
I'm writing a kivy app that lets the user enter and store content, but I've searched the web and can't find a solution
This is the python program
from kivy.app import App
from kivy.graphics import Line, Color
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.config import Config
Config.set('graphics', 'resizable', False)
from kivy.core.window import Window
Window.size = (450, 800)
class DrawCanvasWidget(Widget):
def __init__(self,**kwargs):
super().__init__(**kwargs)
class PaintApp(App):
def build(self):
self.draw_canvas_widget = DrawCanvasWidget()
return self.draw_canvas_widget # 返回root控件
if __name__ == "__main__":
PaintApp().run()
This is the kv program
<DrawCanvasWidget>
orientation: "vertical"
canvas.before:
Color:
rgba: [1,1,1,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
multiline:False
font_size:20
pos:75,450
size:300,40
allow_copy:False
cursor_color:[0,1,0,1]
Button:
text:'ok'
bold:10
size_hint:None,None
size:100,50
pos:180,380
I want to let the user save the content of the input box after pressing the button
This is the basic idea. Create a function in the draw canvas widget and pass the text of the text input by using the id tag.
<DrawCanvasWidget>
orientation: "vertical"
canvas.before:
Color:
rgba: [1,1,1,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
id: text_input
multiline:False
font_size:20
pos:75,450
size:300,40
allow_copy:False
cursor_color:[0,1,0,1]
Button:
text:'ok'
bold:10
size_hint:None,None
size:100,50
pos:180,380
on_release: root.submit(text_input.text)
class DrawCanvasWidget(Widget):
def __init__(self,**kwargs):
super().__init__(**kwargs)
def submit(self, text_input):
print(text_input)
# Should print whatever is in the textinput to the terminal
# Here you can save it to your database or do anything with the input.
Not much code yet, but I am trying to make the buttons stay if they are on the right side, but if they are on the left, to disappear. I am not sure if this is possible, but if you are able to help, I would love it!main.py:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.button import Button
Builder.load_file('main.kv')
class DragButton(DragBehavior, Button):
pass
class MainScreen(Widget):
pass
class WorkingApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
WorkingApp().run()
main.kv
<DragButton>
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000
drag_distance: 0
text: 'Hi'
size_hint: (1, .5)
<MainScreen>
BoxLayout:
size: root.width, root.height
orientation: 'horizontal'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Widgets'
size_hint: (1, .25)
DragButton:
DragButton:
DragButton:
Splitter:
sizable_from: 'left'
Label:
text: 'Check If they are here'
One way to do this is to bind a method to the center_x property of the DragButtons. That method can then check the position of the DragButton, and remove it, if it is to the right of the Splitter:
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.button import Button
Builder.load_file('main.kv')
class DragButton(DragBehavior, Button):
pass
class MainScreen(Widget):
splitter = ObjectProperty(None)
boxlayout = ObjectProperty(None)
def dragged(self, dragButton):
if dragButton.center_x > self.splitter.x:
self.boxlayout.remove_widget(dragButton)
class WorkingApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
WorkingApp().run()
with some small modifications to the 'kv':
<DragButton>
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000
drag_distance: 0
text: 'Hi'
size_hint: (1, .5)
<MainScreen>
splitter: split
boxlayout: box
BoxLayout:
size: root.width, root.height
orientation: 'horizontal'
BoxLayout:
id: box
orientation: 'vertical'
Label:
text: 'Widgets'
size_hint: (1, .25)
DragButton:
on_center_x: root.dragged(self)
DragButton:
on_center_x: root.dragged(self)
DragButton:
on_center_x: root.dragged(self)
Splitter:
id: split
sizable_from: 'left'
Label:
text: 'Check If they are here'
Key modifications are the addition of ids in the kv for the Splitter and the BoxLayout and methods bound to the center_x property of each DragButton. In the python, ObjectProperties for those new ids and the dragged() method are added to the MainScreen class. The dragged() method just checks if the center of the DragButton is roght of the Splitter and removes that DragButton if it is.
I'm new to kivy and python so my code isn't perfect.
I'm trying to make a program with 2 screens, a first screen where there is a label with a text that is not defined and that can change and a second screen that keeps the same text as the first screen.
I've been searching for a week and I tried to make a global variable that I edit and that becomes the text of the second label but it doesn't work.
I also tried with String. property () or object. property () but I didn't get any more results and I didn't really understand how to use it.
Any help would be welcome <3
(sorry for my english)
Here is a part of my code that I have simplified as much as possible:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<MenuScreen>:
label_wid : ratio
FloatLayout:
Button:
text: "options"
pos: 270, 240
size_hint: .30, .10
background_color: 0,1,0,0.75
on_press: root.manager.current = 'settings'
Label:
id:ratio
text: ""
pos: 0,90
font_size:30
color:1,0,0,1
<SettingsScreen>:
label_wid2 : ratio
FloatLayout:
Label:
id:ratio
text: str(root.texte2())
pos: 0,90
font_size:30
color:1,0,0,1
""")
u=""
class MenuScreen(Screen):
def texte(self):
global u
u= self.label_wid.text = 'exemple'
pass
class SettingsScreen(Screen):
def texte2(self):
self.label_wid2.text=u
pass
class Quizz(App):
def build(self):
self.title = 'Quizz'
Window.clearcolor = (0, 1, 1, 0.25)
return sm
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
if __name__ == '__main__':
Quizz().run()
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<MenuScreen>:
label_wid : ratio
FloatLayout:
Button:
text: "options"
pos: 270, 240
size_hint: .30, .10
background_color: 0,1,0,0.75
on_press: root.manager.current = 'settings'
Label:
id:ratio
text: ""
pos: 0,90
font_size:30
color:1,0,0,1
<SettingsScreen>:
label_wid : ratio
FloatLayout:
Label:
id:ratio
text: root.texte2()
pos: 0,90
font_size:30
color:1,0,0,1
""")
u=""
class MenuScreen(Screen):
def texte(self):
global u
u= self.label_wid.text = 'exemple'
pass
class SettingsScreen(Screen):
def texte2(self, text):
u
pass
class Quizz(App):
def build(self):
self.title = 'Quizz'
Window.clearcolor = (0, 1, 1, 0.25)
return sm
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
if __name__ == '__main__':
Quizz().run() ```
Using a StringProperty in the ScreenManager and another in SettingsScreen provide a source of text for both Labels in the different Screens. And changing the StringProperty can change both Labels:
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
# this is the text to be used when the first Label text is not '1'
theOtherText = StringProperty('Not One')
class MyScreenManager(ScreenManager):
# This is the text that both Labels may display
theText = StringProperty('0')
kv = """
MyScreenManager:
MenuScreen:
name: 'menu'
SettingsScreen:
name: 'settings'
<MenuScreen>:
label_wid : ratio
FloatLayout:
Button:
text: "options"
pos: 270, 240
size_hint: .30, .10
background_color: 0,1,0,0.75
on_press: root.manager.current = 'settings'
Label:
id:ratio
# Use theText Property from MyScreenManager
text: root.manager.theText
pos: 0,90
font_size:30
color:1,0,0,1
<SettingsScreen>:
label_wid : ratio
FloatLayout:
Label:
id:ratio
# Use theText Property from MyScreenManager
text: '1' if root.manager.theText == '1' else root.theOtherText
pos: 0,90
font_size:30
color:1,0,0,1
"""
class Quizz(App):
def build(self):
self.title = 'Quizz'
Window.clearcolor = (0, 1, 1, 0.25)
Clock.schedule_once(self.change_text, 4)
return Builder.load_string(kv)
def change_text(self, dt):
print('changing text')
self.root.theText = '1'
if __name__ == '__main__':
Quizz().run()
Building the MyScreenManager in the kv eliminates the need to check if the manager attribute of the Screens is set. That simplifies the logic in the kv. Then the line in kv:
text: '1' if root.manager.theText == '1' else root.theOtherText
sets the text of the second Label to 1 if the first Label text is 1, otherwise it is set to the value of theOtherText in the SettingsScreen.
I have added a Clock.schedule_once() just for testing.
I am attempting to create a Kivy/Python application which gets the user to push a button on one screen, and in turn adds several widgets to the next screen. However, when I run the script, the button appears and can be clicked on the first page, and the widgets are visible on the second page, but they can not be interacted with.
Here is an example of my code, any help is appreciated:
py:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ListProperty, StringProperty, ObjectProperty
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
sm = None
class RcGUITestApp(App):
def build(self):
global sm
Window.clearcolor = (0, 0, 0.35, 1)
sm = ScreenManager()
sm.add_widget(NewJobPage(name="new_job_page"))
sm.add_widget(IPMConfirmPage(name = "ipm_confirm_page"))
return sm
class NewJobPage(Screen):
def retrieve(self):
global rc
global sm
sm.get_screen("ipm_confirm_page").displayIPM()
class IPMConfirmPage(Screen):
grid = ObjectProperty(None)
def displayIPM(self):
for ipm in range(50):
self.grid.add_widget(Label(text="A"))
self.grid.add_widget(Spinner(text="B"))
self.grid.add_widget(Spinner(text="C"))
def main():
rc_gui = RcGUITestApp()
rc_gui.run()
if __name__ == "__main__":
main()
kv:
<NewJobPage>:
BoxLayout:
orientation: "horizontal"
size_hint: (1, .35)
Button:
text: "RETRIEVE"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: (.33, .33)
color: (1,1,1,1)
on_press:
root.retrieve()
root.manager.transition.direction = "left"
root.manager.transition.duration = .8
root.manager.current = "ipm_confirm_page"
<IPMConfirmPage>:
grid: Grid
GridLayout:
cols: 1
size_hint_x: 1
padding: 10
spacing: 10
BoxLayout:
size_hint: (1, 0.1)
Label:
text: "IPM"
Label:
text: "CD#"
Label:
text: "CONDUCTOR"
ScrollView:
GridLayout:
id: Grid
cols: 3
height: self.minimum_height
size_hint: 1, None
spacing: 50
padding: 30
First Page output
Second Page output
You need to provide the height of each widget added to grid. There are two solutions to the problem.
Method 1 - kv file
This method involves changing only the kv file by adding row_force_default: True and row_default_height: 40.
Snippets - kv file
ScrollView:
GridLayout:
row_force_default: True
row_default_height: 40
...
Method 2 - py file
This method involves changing only the py file by adding size_hint_y=None and height=40.
Snippets
def displayIPM(self):
for ipm in range(50):
self.grid.add_widget(Label(text="A", size_hint_y=None, height=40))
self.grid.add_widget(Spinner(text="B", size_hint_y=None, height=40))
self.grid.add_widget(Spinner(text="C", size_hint_y=None, height=40))
I am currently working on a bit of code using Kivy and Python. I am trying to make it so when you click a button, the button's text changes color.
When I click on the button, however, the color doesn't like I want it to.
Any ideas how this can be fixed? I am just learning Kivy and perhaps the answer is easier than I think. The .py file is below
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.graphics import Color
class TextBubbleSample(Widget):
bubble = ObjectProperty(None)
class TextBubble(Widget):
pass
class Talk(Button):
btn = Button
def button_press(self):
btn.bind(on_state = self.on_event)
def on_event(self):
btn.color = 1,0,0,1
class TextBubbleApp(App):
def build(self):
return TextBubbleSample()
if __name__ == '__main__':
TextBubbleApp().run()
and here is the .kv file
#:kivy 1.0.9
<TextBubbleSample>:
bubble: text_bubble
btn: click_here
TextBubble:
id: text_bubble
x: root.x
center_y: root.center_y
Talk:
id: click_here
x: 10
center_y: 220
text: "Talk to me."
color: 0,0,1,1
<TextBubble>:
canvas:
Color:
rgba: 1,0,0,1
Rectangle:
pos: 10, 10
size: 780, 150
Label:
color: 0,0,1,1
font_size: 35
center_x: 200
top: root.top - 200
text: "I am talking"
You shouldn't/can't use the variable btn like that. Use self.bind(on_state=self.on_event) then self.color = (1, 0, 0, 1).
You should try to reduce the code length, you don't need to use any id in this case.
There could be an easy alternative to inclement's answer.
In your .py file (Talk class) add.
class Talk(Button):
....
def on_release(self):
self.color = 1,0,0,1
and in your .kv file add
Talk:
text: "talk to me"
....
on_release: self.on_release
EDIT:
you can do it this way too
class Singularity(BoxLayout):
def __init__(self,**kwargs):
super(Singularity,self).__init__(**kwargs)
self.b = Button(text = "hello",on_press = self.on_press)
self.add_widget(self.b)
def on_press(self,event):
if event.color == [1,0,0,1]:
event.color = [0,0,1,1]
else:
event.color=[1,0,0,1]