So, I can run kivy example files (like pong) on my Android but I can't run my own app, it just saying "Hello World!".
I don't understand, if someone can help me.
This is my Python code :
import kivy
from kivy.app import App
from kivy.config import Config
from kivy.uix.label import Label
from kivy.uix.widget import Widget
Config.set('graphics','width','360')
Config.set('graphics','height','640')
class Mot(Widget):
def mot(self):
test = 0
def bouger(self):
class WorDown(App):
def build(self):
return Mot()
if __name__ == '__main__':
WorDown().run()
Next my Kivy code :
#:kivy 1.0
<Mot>:
Widget:
canvas:
Color:
rgb: (255, 0, 0)
Rectangle:
size: (360,640)
pos: self.pos
Label:
text: 'WorDown'
center_x: root.width / 2
And my android.txt :
title=WorDown
author=pito
orientation=portrait
So I put these three files in a folder in the Kivy folder of my Android Phone. And when I run Kivy launcher, and click on "WorDown", Kivy Launcher runs the app but crashes just 3 seconds after...
And I don't understand why, because on my Windows Computer, it runs very well!
Please look at the python code you included. In class Mot you have
def bouger(self):
but nothing in the function body.
Do you really don't have anything in the body? Your code shouldn't even run on your computer then, so I guess you just didn't post everything.
If you are sure that the code in this function is not causing the crash, just write "pass" in the function body, so that it doesn't confuse other readers.
you could follow the code
main.py
import kivy
from kivy.uix.label import Label
from kivy.app import App
class MyApp(App):
def build(self):
return Label(text='WorDown' )
if __name__=="__main__":
MyApp().run()
Try to run your code after removing 7 and 8 lines of your code.
Related
I just started trying out kivy developing and ran into a problem. When I try to run a simple kivy file with a corresponding python file, it emits a syntax error. does anyone know why this happened? FYI I'm using Atom IDE with python 3.8.3, when I ran kivy based on python, it worked flawlessly, but when I try to run a rewritten kv code, it won't run.
Here's the python source code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
class MyGrid(Widget):
pass
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
...and the kv code:
< MyGrid >
Label:
text: "example text"
Error details:
File "C:\Users\ljhub\Code Storage\Python Projects\my.kv", line 1
<MyGrid>
^
SyntaxError: invalid syntax
The corrected kv file should be as below:
<MyGrid>:
Label:
text: "example text"
You have to add a colon after any declaration in kv language.
such as:
<MyGrid>:
or
Label:
I am trying to make an app. I have got it to work where all my files are in the same folder but it gets very messy and I would like to separate my files into separate folders of the structure: Start, Prelogin, andFirebaseLoginScreen`. So is what I am trying to do possible in kivy?
I have tried #: import name x.y.z as the pointer where x is the folder name, y is the name of kv-file and z is the class in kv-file I want to import, but I get so many weird errors when I try to do it. I have pretty much added every folder and everything to my PYTHONPATH but nothing works.
Start contains main.py and main.kv where main.kv then points to the screenmanger in ``Prelogin. Prelogin contains some files that consist of labels and text about the app and then points to the screenmanger in FirebaseLoginScreen. The FirebaseLoginScreen contains a lot of files for the login system.
Yes, I'will give you and example with this folder structure
-Project\
--main.py
--main.kv
--folder1\
----window1.py
----window1.kv
--folder2\
----window2.py
----window3.py
--folder3\
----window4.py
----window4.kv
folder\window1.py will be like
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.uix.widget import Widget
kivy.require("1.11.1")
# Builder is neccesary to work with multiple files
Builder.load_file("folder1/window1.kv")
class login(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class login_window(App):
def build(self):
return login()
if __name__=="__main__":
aplicacion=login_window()
aplicacion.run()
folder1\window1.kv is not necessary to specify.
main.py will be like
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.uix.widget import Widget
# Import files like
from folder1.window1 import login_window
from folder2.window2 import example2_window
from folder3.window3 import example3_window
class manager(BoxLayout):
# Add screens to main
login_widget=login_window()
example2_widget=example2_window()
example3_widget=example3_window()
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Check kv file to understand these lines
self.ids.screen_login.add_widget(self.login_widget)
self.ids.screen_example2.add_widget(self.example2_widget)
self.ids.screen_example3.add_widget(self.example3_widget)
class main(App):
def build(self):
return manager()
if __name__ == "__main__":
main().run()
main.kv will be like (Which has the ScreenManager!)
#:kivy 1.11.1
<manager>:
id: main_window
ScreenManager:
id: scrn_mngr_main
Screen:
id: screen_login
name:'screen_login'
Screen:
id: screen_example2
name:'screen_example2'
Screen:
id: screen_example3
name:'screen_example3'
Now, to control the program flow yo need to add these lines in window1.py
self.parent.current='screen_login'
self.parent.current='screen_example2'
self.parent.current='screen_example3'
Adapted from 3 videos of a tutorial on Youtube
The answer is simply "yes", there's nothing special about the import parsing in kv language, or in Python when using Kivy. If you're having issues, it's because what you're attempting is wrong or insufficient, which isn't possible to debug from the amount of information you've given.
If you'd like to follow it up, post another question with an example that you think should work given your PYTHONPATH manipulations.
I have a problem deploying a Kivy application using RstDocument. It works fine on PC. I can deploy it if I use Label instead of RstDocument, but it stops in the latter case.
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen
import configparser
class MainFrame(Screen):
def __init__(self, **kwargs):
super(MainFrame, self).__init__(**kwargs)
pass
def on_quit_button_click(self):
quit()
class BasicApp(App):
def build(self):
return MainFrame()
pass
if __name__ == '__main__':
BasicApp().run()
with the following basic.kv
<MainFrame>:
BoxLayout:
RstDocument:
#Label:
text: 'Hello *world*!'
Button:
text: 'Click to quit'
on_press: root.on_quit_button_click()
Nothing significant in logcat, just
WindowManager: finishDrawingWindow: Window{bf4383ed0 u0 Application Error: testrstdocument.testrstdocument} mDrawState=HAS_DRAWN
Buildozer.spec
I am able to run a Kivy App with rstDocument on an Acer Android tablet by adding docutils into buildozer's requirement.
requirements = kivy, configparser, docutils
Could somebody post a small working example of a kivy Filechooser with the following simple doubleclick function: doubleclicking on a file will print out the filename?
Here is an example of that.
from kivy.app import App
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.boxlayout import BoxLayout
class MyFileChooser(FileChooserListView):
def on_submit(*args):
print(args[1][0])
class MyLayout(BoxLayout):
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
# filter added. Since windows will throw error on sys files
self.fclv = MyFileChooser(filters= [lambda folder, filename: not filename.endswith('.sys')])
self.add_widget(self.fclv)
class MyApp(App):
def build(self):
return MyLayout()
MyApp().run()
I think it is simpler than that.
FileChooser has an argument dirselect. By default it is False making it single-click. If you change dirselect to True, it works as double-click.
For example, in kivy language
BoxLayout:
FileChooserIconView:
size_hint: (0.3, 0.4)
dirselect: True
For example, in python language
FileChooserListView(size_hint_x=0.3, size_hint_y=0.4, dirselect=True)
Hope it helps somebody
I have written a code with GridLayout which need to be added with buttons in Python file. So, the add_widget() mainpulation should be done in build(). I am getting errors and couldn't get it.Someone Please help me.
In short, instead of add_btn(), I need it in build() of MineApp class.
Thanks in advance.`
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class MainLayout(BoxLayout):
def build(self):
pass
def add_btn(self,id):
for i in range(100):
id.add_widget(Button())
class MineApp(App):
def build(self):
return MainLayout()
if __name__ == '__main__':
MineApp().run()
mine.kv file:
<MainLayout>:
orientation:'vertical'
BoxLayout:
orientation:'horizontal'
height: '30px'
size_hint_y:None
TextInput:
id: tinput
text:'10'
Button:
text:'start'
on_press:root.add_btn(grid)
Label:
id:mylabel
text:'0'
GridLayout:
id: grid
cols:10
rows:10
It took some time but think I get what you're trying to say! You can get the same effect by passing the id parameter of Grid in the .py file like so...
class MainLayout(BoxLayout):
def build(self):
for i in range(100):
self.ids.grid.add_widget(Button())
Then, you can just take your start button and...
Button:
text:'start'
on_press:root.build()
This works because "self" in .py refers to the class while in the .kv a similar wording would be 'root' (while 'self' in .kv refers to the widget!) Is this what you kind of had in mind? Let me know! I tried it out myself and had no problem running it through :)
Also, it wasn't put explicitly here that to test the code you'd need to import Builder via:
from kivy.lang import Builder
and do...
Builder.load_file("mine.kv")
as your .py and .kv don't share the same name (which you'd still need to add a "#File name: main.py" to the .kv if they did match names! Other than that, it looks good!