I'm trying to write to a file via a mobile app GUI constructed with kivy. I was originally getting an error that json was not defined, so i added import json to the main.py file. Now, I'm getting this error. I've tried setting the encoding and still no luck. I've created an empty users.json file that only contains a pair of curly brackets as well and nothing.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from datetime import datetime
import json
Builder.load_file('design.kv')
class LoginScreen(Screen):
def sign_up(self):
self.manager.current = "sign_up_screen"
class RootWidget(ScreenManager):
pass
class SignUpScreen(Screen):
def add_user(self,uname,pword):
with open( "users.json",encoding='utf-8=sig', errors='ignore' ) as file:
users = json.load(file, strict=False)
users[uname] = {"username": uname, "password": pword,
"created": datetime.now().strftime("%Y-%m-%d %H-%M-%S")}
with open( "users.json" , "rb") as file:
json.dump(users, file)
print(users)
class MainApp(App):
def build(self):
return RootWidget()
if __name__=='__main__':
MainApp().run()
THIS IS MY KV FILE
<LoginScreen>:
GridLayout:
cols:1
GridLayout:
cols:1
Label:
text: 'User Login'
TextInput:
hint_text:'Username'
TextInput:
hint_text:'Password'
Button:
text:'Login'
GridLayout:
cols:2
Button:
text:'Forgot Password?'
Button:
text:'Sign Up'
on_press: root.sign_up()
<SignUpScreen>:
GridLayout:
cols:1
Label:
text:'Sign uo here'
TextInput:
id: username
hint_text:'Enter your Username'
TextInput:
id:password
hint_text:'Create a password'
Button:
text:'Submit'
on_press: root.add_user(root.ids.username.text, root.ids.password.text)
<RootWidget>:
LoginScreen:
name:'login_screen'
SignUpScreen:
name:'sign_up_screen'
This is the terminal output from VSC
[Running] python -u "c:\Users\joemc\Desktop\PYTHON 10 PROJECTS\MOBILE_APP\main.py"
[INFO ] [Logger ] Record log in C:\Users\joemc\.kivy\logs\kivy_22-09-10_38.txt
[INFO ] [deps ] Successfully imported "kivy_deps.angle" 0.3.2
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.4.5
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "C:\Users\joemc\anaconda3\lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Users\joemc\anaconda3\python.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.6.0 - Build 30.0.101.1340'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics 520'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60 - Build 30.0.101.1340'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: sdl2
[INFO ] [GL ] NPOT texture support is available
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "c:\Users\joemc\Desktop\PYTHON 10 PROJECTS\MOBILE_APP\main.py", line 31, in <module>
MainApp().run()
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\app.py", line 955, in run
runTouchApp()
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\base.py", line 339, in mainloop
self.idle()
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\base.py", line 383, in idle
self.dispatch_input()
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\base.py", line 334, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\base.py", line 263, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\core\window\__init__.py", line 1660, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\core\window\__init__.py", line 1677, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\screenmanager.py", line 1210, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\relativelayout.py", line 306, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
File "C:\Users\joemc\anaconda3\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
exec(__kvlang__.co_value, idmap)
File "c:\Users\joemc\Desktop\PYTHON 10 PROJECTS\MOBILE_APP\design.kv", line 35, in <module>
on_press: root.add_user(root.ids.username.text, root.ids.password.text)
File "c:\Users\joemc\Desktop\PYTHON 10 PROJECTS\MOBILE_APP\main.py", line 17, in add_user
users = json.load(file, strict=False)
File "C:\Users\joemc\anaconda3\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\joemc\anaconda3\lib\json\__init__.py", line 359, in loads
return cls(**kw).decode(s)
File "C:\Users\joemc\anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\joemc\anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[Done] exited with code=1 in 10.06 seconds