...
How can I give function path?
I couldn't find how to solve addressing.
Anyone have any idea how to fix this?
When I click on the green area only, I want to do the operation of the button in the white area.
...
'''python
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
class Area(RelativeLayout):
pass
class Sec(RelativeLayout):
""" This is white area """
def chn(self):
self.ids.secbut.disabled = True
class Vec(RelativeLayout):
""" This is green area
I want the button to disable when the green area is clicked.
"""
def on_touch_down(self,touch):
if self.collide_point(*touch.pos):
Sec.ids.secbut.disabled = True
class runner(App):
def build(self):
self.area = Area()
return self.area
if __name__ == "__main__":
runner().run()
'''
...
runner.kv
...
'''kivy
<Area>:
size_hint: 1, 1
pos_hint: {"x":0,"y":0}
canvas:
Color:
rgba:0.9,0.9,0.9,1
Rectangle:
size:self.size
Sec:
Vec:
<Sec>:
size_hint: 1, 0.5
pos_hint: {"x":0,"y":0}
canvas:
Color:
rgba:0.9,0.8,0.7,1
Rectangle:
size:self.size
Button:
id:secbut
text:"click"
size_hint:0.5,0.5
<Vec>:
size_hint: 1, 0.5
pos_hint: {"x":0,"y":0.5}
canvas:
Color:
rgba:0.2,0.8,0.1,1
Rectangle:
size:self.size
'''
I couldn't find how to solve addressing.
You need to check if the touch actually collides with the area you care about.
if self.collide_point(*touch.pos):
self.ids.secbut.disabled = True
Kind of awkward, but you can do it by adding an id to your Sec like this:
<Area>:
size_hint: 1, 1
pos_hint: {"x":0,"y":0}
canvas:
Color:
rgba:0.9,0.9,0.9,1
Rectangle:
size:self.size
Sec:
id: sec
Vec:
Then use that in your on_touch_down() in Vec:
class Vec(RelativeLayout):
""" This is green area
I want the button to disable when the green area is clicked.
"""
def on_touch_down(self,touch):
if self.collide_point(*touch.pos):
App.get_running_app().root.ids.sec.ids.secbut.disabled = True
Related
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()
I am using Kivy to make an app, and I'm trying to display a label. Here's my main code
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
class storageApp(App):
def build(self):
return kvfile
class LoginPage(Screen):
pass
class WindowManager(ScreenManager):
pass
kvfile = Builder.load_file('storage.kv')
storageApp().run()
Here's my .kv file
WindowManager:
current: 'login'
LoginPage:
<LoginPage>:
name: 'login'
FloatLayout:
Label:
text: 'Welcome to the secret app!'
size_hint: 0.5, 0.1
pos_hint: {'center_x': 0.5, 'top': 0.95}
background_color: 1, 0, 1, 1
And I get the following output on my screen
The color of the label is still black. However, when Ichange the Label to Button it seems to work.
This seems very odd. Any idea how to fix it?
The Label doesn't have a background colour property. If you want a background, draw one:
<KvBackgroundRule>:
canvas.before:
Color:
rgba: 1, 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
I want a rectangle to stick to the bottom left corner of an image. It works, but when I change window size the rectangle is little bit off. It is where image was before update. Here's the code:
from kivy.app import App, Builder
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.graphics import Rectangle
from kivy.graphics import Color
Builder.load_string(
'''
<Screen>:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
Image:
id: im
keep_data: True
source: 'test.png'
allow_stretch: True
size_hint_y: 0.9
pos_hint: {'left':1, 'top':1}
Button:
text: 'Button'
size_hint_y: 0.1
pos_hint: {'x':0, 'y':0}
''')
class Screen(FloatLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
with self.ids.im.canvas.after:
Color(1,0,1,.5)
self.rec = Rectangle(pos=(self.ids.im.center_x - self.ids.im.norm_image_size[0] / 2,
self.ids.im.center_y - self.ids.im.norm_image_size[1] / 2),
size=(self.ids.im.norm_image_size[0]/2,
self.ids.im.norm_image_size[1]/2))
self.bind(pos=self.update_canvas, size=self.update_canvas)
def update_canvas(self, *args):
self.rec.pos = (self.ids.im.center_x - self.ids.im.norm_image_size[0] / 2,
self.ids.im.center_y - self.ids.im.norm_image_size[1] / 2)
self.rec.size = (self.ids.im.norm_image_size[0]/2,
self.ids.im.norm_image_size[1]/2)
class QuestionApp(App):
def build(self):
return Screen()
if __name__ == "__main__":
QuestionApp().run()
I'm doing this in Python because I want to randomly spawn rectangles within the image, but I'm stuck here with rectangles when window resizing being wobbly.
If you want the rectangle to use the geometry of the image as reference then you must do the binding with the image, for this change:
self.bind(pos=self.update_canvas, size=self.update_canvas)
to
self.ids.im.bind(pos=self.update_canvas, size=self.update_canvas)
I'm attempting to drag some images arranged inside a GridLayout (which is indeed inside a ScrollView) to outer Layout.
The image to be dragged has its on_touch_down event defined, when image is clicked parent is changed from WidgetMenu to MainLayout so it can be dragged between those widgets. The current problem is when I touch the image, DragBehavior is lost.
The full code:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
from kivy.uix.behaviors import DragBehavior
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.base import runTouchApp
from kivy.lang import Builder
Builder.load_string('''
<WidgetMenu>:
canvas.before:
Color:
rgb: 0.9,0.5,0.3
RoundedRectangle:
pos:self.pos
size: self.size
radius: [20,]
orientation: "vertical"
padding:30
ScrollView:
GridLayout:
cols:1
size_hint_y:None
row_default_height:root.height*.15
height:self.minimum_height
DragImage:
DragImage:
DragImage:
<DragImage>:
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 100000000
drag_distance: 0
size_hint:None,None
size:234,34
canvas:
Color:
rgb:1,0,1
Rectangle:
pos: self.pos
size: self.size
<MainLayout>:
canvas:
Color:
rgb:1,1,1
Rectangle:
size: self.size
pos: self.pos
WidgetMenu:
size_hint: 0.35,0.9
''')
class MainLayout(FloatLayout):
pass
class WidgetMenu(BoxLayout):
pass
class DragImage(DragBehavior,FloatLayout):
def on_touch_down(self,touch):
workspace = self.parent.parent.parent.parent
grid = self.parent
menu = self.parent.parent.parent
if "MainLayout" in str(workspace):
grid.remove_widget(self)
workspace.remove_widget(menu)
self.pos = Window.mouse_pos
workspace.add_widget(self)
return True
class ScrollApp(App):
def build(self):
return MainLayout()
ScrollApp().run()
Please help.
Two problems with your code are that you are not calling the super method in your on_touch_down, and placing the DragImage in the top level MainLayout changes the pos of the DragImage, thus changing its DragRectangle. That change in DragRectangle leaves the touch.pos outside of it, so DragBehavior thinks the touch is not on the DragImage.
I have fixed both those problems by calling the super method and changing the touch.pos before passing it to the super method. I also added some code to keep the DragImage in the same position relative to the mouse when it is clicked. Also added a call to self.collide_point() in order to ignore clicks not on the DragImage.
class DragImage(DragBehavior,FloatLayout):
def on_touch_down(self,touch):
if not self.collide_point(*touch.pos):
return False
workspace = self.parent.parent.parent.parent
grid = self.parent
menu = self.parent.parent.parent
if "MainLayout" in str(workspace):
grid.remove_widget(self)
workspace.remove_widget(menu)
# the following code assumes that workspace is the entire Window
self.x = Window.mouse_pos[0] - (touch.pos[0] - self.x)
self.y = Window.mouse_pos[1] - (touch.pos[1] - self.y)
workspace.add_widget(self)
touch.pos = Window.mouse_pos
return super(DragImage, self).on_touch_down(touch)
I'm trying to add a digital clock to my Kivy program, it seems to be having trouble.
Here is the .py:
import kivy
kivy.require('1.10.0')
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
import time
class IntroScreen(Screen):
pass
class ContScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
backbone = Builder.load_file("main.kv")
class Status(FloatLayout):
_change = StringProperty()
_tnd = ObjectProperty(None)
def update(self, *args):
self.time = time.asctime()
self._change = str(self.time)
self._tnd.text = str(self.time)
print (self._change)
class XGApp(App):
time = StringProperty()
def update(self, *args):
self.time = str(time.asctime()) # + 'time'?
def build (self):
Clock.schedule_interval(self.update, 1)
return backbone
xApp = XGApp()
if __name__ == "__main__":
xApp.run()
and the .kv
<ContScreen>:
FloatLayout
size_hint: .1,.1
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size
Label:
text: app.time
ContScreen is the title of the screen I want to show the clock on, it's served by a separate Builder (main.kv).
Any help would be appreciated! Been struggling with this clock for a few hours now. The trouble seems to be on the .kv side from what I can tell.
BONUS: If you want to go the extra mile, I also want to add a timer that counts down x amount on press of a button on the .kv. The x amount would be different depending on which button you press.
I have made a fork of the original kivydigitalclock here. It should be easier to use than the original. You can add it to your .kv file just as any other widget. For example, something like:
<ContScreen>:
FloatLayout
size_hint: .1,.1
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size
Label:
text: app.time
DigitalClock:
pos_hint: {'right': 1.0, 'center_y': 0.5}
size_hint: (0.2, 0.2)
should work. Note that in your main .py file you will need to include:
from digitalclock.digitalclock import DigitalClock
(assuming that the digitalclock.py file is in a digitalclock folder)