I found this piece of code:
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
class Test(BoxLayout):
def __init__(self, **kwargs):
super(Test, self).__init__(**kwargs)
Window.bind(on_touch_down=self.window_on_touch_down)
def window_on_touch_down(self, *args):
print(args)
# scrolling the wheel down will give <MouseMotionEvent button="scrollup"
if '<MouseMotionEvent button="scrollup"' in args:
print('hello')
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
and while it prints the scrollup motion I can't use it... args has the string in it but the if doesn't work and "hello" isn't printed... anybody know why?
What you see in the prints are python objects not strings.
So you can't just check for a string.
A better way to get what you want is to check what button produces the MouseMotionEvent
def window_on_touch_down(self, window, mouse_event):
# scrolling the wheel down will give <MouseMotionEvent button="scrollup"
if mouse_event.button == "scrollup":
print('hello')
Related
I am using kivy's TextInput in a project. In the documentation they say that TextInput.on_triple_tap is selecting the whole tapped line.
But when I tripple tap a line in my text box, nothing is selected.
I tried even to make my own on_triple_tap function, but it didn't work either.
Am I missing something?
First example - this should work, but it doesn't
from kivy.app import App
from kivy.uix.textinput import TextInput
class ManagerApp(App):
def build(self):
return TextInput(text='hello world')
if __name__ == '__main__':
ManagerApp().run()
second example - I tried to override the on_triple_tap, prints the 'reached' but doesn't select anything
from kivy.app import App
from kivy.uix.textinput import TextInput
class ManagerApp(App):
def build(self):
self.ti = TextInput(text='hello world',
on_triple_tap=self.my_triple_tap)
return self.ti
def my_triple_tap(self, ti):
print 'reached'
ti.select_all()
if __name__ == '__main__':
ManagerApp().run()
You must have at least one "line" of text in the TextInput. That means at least one newline character. Change:
class ManagerApp(App):
def build(self):
return TextInput(text='hello world')
to:
class ManagerApp(App):
def build(self):
return TextInput(text='hello world\n')
I have a button which was made in python file and I have been trying to make it so it changes screens from one to another.
def callback(instance):
print("Test 1")
sm = ScreenManager()
sm.add_widget(ScreenTwo(name="ScreenTwo"))
print("Test2")
class ScreenOne(Screen):
def on_enter(self):
self.add_widget(ImageURLButton(source=icon2, size=(100,100), size_hint=(0.1, 0.1), on_press=callback, pos_hint={"x":0.90, "top":1.0}))
class ScreenTwo(Screen):
pass
class ScreenManagement(ScreenManager):
pass
When I do click the button all it does it prints "Test1" and "Test2" without it changing the screen. Sorry if this is really obvious to others but I do not know how to fix it, could anyone help me please?
Would be better if you posted a MCVE, but I made one myself. Here is how it can be done:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
def callbackTo2(instance):
sm.current="ScreenTwo"
def callbackTo1(instance):
sm.current="ScreenOne"
class ScreenOne(Screen):
def __init__(self, *args):
super(ScreenOne, self).__init__(name='ScreenOne')
self.add_widget(Button(text='Switch To Screen Two', size_hint=(0.1, 0.1), on_press=callbackTo2, pos_hint={"x":0.90, "top":1.0}))
class ScreenTwo(Screen):
def __init__(self, *args):
super(ScreenTwo, self).__init__(name='ScreenTwo')
self.add_widget(Button(text='Switch To Screen One', size_hint=(0.1, 0.1), on_press=callbackTo1, pos_hint={"x":0.90, "top":1.0}))
sm = ScreenManager()
class ScreenPlayApp(App):
def build(self):
sm.add_widget(ScreenOne())
sm.add_widget(ScreenTwo())
return sm
if __name__ == '__main__':
app = ScreenPlayApp()
app.run()
Note that there is only one ScreenManager instance, all the screens can be added to the ScreenManager initially, and screens are switched by using sm.current=. Also, you can build your Screen in its __init__() method. Using the on_enter causes the members of the screen to be rebuilt each time the screen is displayed. Also, you cannot use both 'size' and 'size_hint' for the same widget unless you are setting 'size_hint' to 'None'.
I have simple code for kivy, on W10 runs without problem. It falls down during loading in kivy launcher. Problem is without message.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class View(BoxLayout):
def __init__(self):
super().__init__()
self.text = "No text"
but = Button(text = "Press",on_press = self.show)
self.add_widget(but)
self.lbl = Label()
self.add_widget(self.lbl)
def show(self,obj):
self.lbl.text = self.text
pass
class MyPaintApp(App):
def build(self):
return View()
if __name__ == '__main__':
MyPaintApp().run()
It does not run because you call super wrong.
As kivy launcher uses python 2, you need to pass your class (View) and the instance (self) to super.
You need to edit your class like this:
class View(BoxLayout):
def __init__(self,**kwargs):
super(View,self).__init__(**kwargs)
in every failure in kivy launcher, there is a '.kivy/log' directory inside the project directory that has a full log. you could find all the problem there.
I would like to prevent the EXIT of a kivy App on Android with the ESCAPE ARROW.
When I write exit_on_escape = 0 in the config.ini file, it does not change anything (however on WINDOWS 8 with the Esc key this works).
I have also tried unsuccessfully to intercept the on_request_close.
import kivy
kivy.require('1.0.8')
from kivy.core.window import WindowBase
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.clock import Clock
class Test(Widget):
def __init__(self, **kwargs):
super(Test,self).__init__(**kwargs)
class TestApp(App):
def build(self):
Clock.schedule_once(self.my_callback, 0)
return Test()
def my_callback(self,*dt):
print("mycallback")
win=self._app_window
win.fullscreen=1 #OK
win.bind(on_request_close=self.alwaystrue)
def alwaystrue(*largs, **kwargs) :
print("alwaystrue")#never happens ...
return True
if __name__ == '__main__':
TestApp().run()
Do something like this in your App class:
from kivy.app import App
from kivy.core.window import Window
class MyApp(App):
def build(self):
self.bind(on_start=self.post_build_init)
# do all your normal stuff as well
return whatever
def post_build_init(self,ev):
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
if keycode1 == 27 or keycode1 == 1001:
# Do whatever you want here - or nothing at all
# Returning True will eat the keypress
return True
return False
This is probably not all be necessary (using the intermediate post_build_init() method and the android.map_key() thing), but I originally got it from a mailing post and I don't think I have an updated version. Anyway, it works for me.
Add the following code snippet just below the import kivy statement
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
Here is a working example, how to use on_request_close.
The line:
Config.set('kivy', 'exit_on_escape', '0')
disables the Esc -button.
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window
class ChildApp(App):
def build(self):
Window.bind(on_request_close=self.on_request_close)
return Label(text='Child')
def on_request_close(self, *args):
self.textpopup(title='Exit', text='Are you sure?')
return True
def textpopup(self, title='', text=''):
"""Open the pop-up with the name.
:param title: title of the pop-up to open
:type title: str
:param text: main text of the pop-up to open
:type text: str
:rtype: None
"""
box = BoxLayout(orientation='vertical')
box.add_widget(Label(text=text))
mybutton = Button(text='OK', size_hint=(1, 0.25))
box.add_widget(mybutton)
popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
mybutton.bind(on_release=self.stop)
popup.open()
if __name__ == '__main__':
ChildApp().run()
I'm working through the Kivy tutorial, programming guide, and find the following code is not actually printing the button position anywhere, as far as I can tell---that is, the btn_pressed() method doesn't seem to do anything.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
self.add_widget(Button(text='btn 1'))
cb = CustomBtn()
cb.bind(pressed=self.btn_pressed)
self.add_widget(cb)
self.add_widget(Button(text='btn 2'))
def btn_pressed(self, instance, pos):
print ('pos: printed from root widget: {pos}'.format(pos=pos))
class CustomBtn(Widget):
pressed = ListProperty([0, 0])
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.pressed = touch.pos
# we consumed the touch. return False here to propagate
# the touch further to the children.
return True
return super(CustomBtn, self).on_touch_down(touch)
def on_pressed(self, instance, pos):
print ('pressed at {pos}'.format(pos=pos))
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
TestApp().run()
Does anyone have any hints or ideas why this isn't working? Is this the intended behavior and I missed something or is there an error in the tutorial?
Specifically, while the instructions above produce buttons that can be clicked and flash---there doesn't seem to be any output corresponding to the method:
def btn_pressed(self, instance, pos):
print ('pos: printed from root widget: {pos}'.format(pos=pos))
Maybe it's printing black on black?
The seemingly blank, unlabeled spot in the middle is the button that accepts location and prints location to the console. I was clicking on the buttons labeled "btn" and didn't notice this place existed.
This part of the tutorial is demonstrating how you can make custom buttons that do stuff precisely like this. It would be more clear if this were labeled, but that should be doable by looking at the API.
Anyway, the code is working as expected.