Open tealamore opened 10 years ago
Yeah, I think the best bet here is to validate the user input and if it doesn't match, provide an error message somehow.
Any ideas on how best to do user input validation in Kivy? I could brute force it and just try to cast it to whatever type I expect and if that fails, show some error, but I'd expect there's a better way to do this.
I think you just need to include a slight bit of code that casts it to an int.
example code:
flo = 1.1
flo -= flo % 1
I entered 20. in the box for time. The app crashed. I have python 2.7. Here's the error code:
[INFO ] Kivy v1.8.0 [INFO ] [Logger ] Record log in /home/brad/.kivy/logs/kivy_14-07-27_3.txt [INFO ] [Factory ] 157 symbols loaded [DEBUG ] [Cache ] register with limit=None, timeout=Nones
[DEBUG ] [Cache ] register with limit=None, timeout=60s
[DEBUG ] [Cache ] register with limit=None, timeout=Nones
[INFO ] [Image ] Providers: img_tex, img_dds, img_pygame, img_pil, img_gif
[DEBUG ] [Cache ] register with limit=1000, timeout=60s
[DEBUG ] [Cache ] register with limit=1000, timeout=3600s
[INFO ] [Text ] Provider: pygame
[DEBUG ] [Cache ] register with limit=500, timeout=60s
[INFO ] [Loader ] using a thread pool of 2 workers
[DEBUG ] [Cache ] register with limit=None, timeout=60.0s
[DEBUG ] [Cache ] register with limit=None, timeout=60.0s
[DEBUG ] [Window ] Ignored (import error)
[INFO ] [Window ] Provider: pygame(['window_egl_rpi'] ignored)
[DEBUG ] [Window ] Display driver x11
[DEBUG ] [Window ] Actual window size: 800x600
[DEBUG ] [Window ] Actual color bits r8 g8 b8 a8
[DEBUG ] [Window ] Actual depth bits: 24
[DEBUG ] [Window ] Actual stencil bits: 8
[DEBUG ] [Window ] Actual multisampling samples: 4
[INFO ] [GL ] OpenGL version <3.0 Mesa 10.1.3>
[INFO ] [GL ] OpenGL vendor
[INFO ] [GL ] OpenGL renderer <Mesa DRI Intel(R) Ivybridge Mobile >
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version <1.30>
[INFO ] [GL ] Texture max size <8192>
[INFO ] [GL ] Texture max units <16>
[DEBUG ] [Shader ] Fragment compiled successfully
[DEBUG ] [Shader ] Vertex compiled successfully
[DEBUG ] [ImagePygame ] Load </usr/local/lib/python2.7/dist-packages/kivy/data/glsl/default.png>
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[DEBUG ] [App ] Loading kv <./my.kv>
[DEBUG ] [App ] kv <./my.kv> not found
[INFO ] Menu Screen init.
[INFO ] Rendering Menu Screen.
[INFO ] [GL ] NPOT texture support is available
[DEBUG ] [Atlas ] Load </usr/local/lib/python2.7/dist-packages/kivy/data/../data/images/defaulttheme.atlas>
[DEBUG ] [Atlas ] Need to load 1 images
[DEBUG ] [Atlas ] Load </usr/local/lib/python2.7/dist-packages/kivy/data/../data/images/defaulttheme-0.png>
[DEBUG ] [ImagePygame ] Load </usr/local/lib/python2.7/dist-packages/kivy/data/../data/images/defaulttheme-0.png>
<main.MenuScreen object at 0x23b2188>
[INFO ] [OSC ] using for socket
[DEBUG ] [Base ] Create provider from mouse
[DEBUG ] [Base ] Create provider from probesysfs
[DEBUG ] [ProbeSysfs ] using probsysfs!
[DEBUG ] [ProbeSysfs ] found device: SynPS/2 Synaptics TouchPad at /dev/input/event6
[INFO ] [ProbeSysfs ] device match: /dev/input/event6
[INFO ] [MTD ] Read event from </dev/input/event6>
[INFO ] [Base ] Start application main loop
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in *bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(_self.__args, _self.kwargs)
File "/usr/local/lib/python2.7/dist-packages/kivy/input/providers/mtdev.py", line 193, in _thread_run
_device = Device(_fn)
File "/usr/local/lib/python2.7/dist-packages/kivy/lib/mtdev.py", line 131, in init**
self._fd = os.open(filename, os.O_NONBLOCK | os.O_RDONLY)
OSError: [Errno 13] Permission denied: '/dev/input/event6'
[INFO ] Confirm Button Callback. [INFO ] Menu Screen confirm callback. [INFO ] Rendering Boggle Setup Screen. [INFO ] Rendering Boggle Layout. [INFO ] Confirm Game Button Callback. [INFO ] [Ignore indexes are] [] [INFO ] [Base ] Leaving application in progress... Traceback (most recent call last): File "boggle.py", line 234, in
MyApp().run()
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 792, in run
runTouchApp()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 481, in runTouchApp
EventLoop.window.mainloop()
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 381, in mainloop
self._mainloop()
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 287, in _mainloop
EventLoop.idle()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 324, in idle
self.dispatch_input()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 309, in dispatch_input
post_dispatch_input(*pop(0))
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 220, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4552)
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/init.py", line 645, in on_motion
self.dispatch('on_touch_down', me)
File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4552)
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/init.py", line 657, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4552)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/widget.py", line 284, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4552)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/widget.py", line 284, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4552)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/behaviors.py", line 93, in on_touch_down
self.dispatch('on_press')
File "_event.pyx", line 312, in kivy._event.EventDispatcher.dispatch (/home/brad/Desktop/pycon/pyohio-kivy-2014/build/kivy/kivy/_event.c:4506)
File "boggle.py", line 75, in confirm_game_button
self.game_time = int(self.game_time_input.text)
ValueError: invalid literal for int() with base 10: '20.'