I have my python code:
from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class Premade(BoxLayout):pass
class MyFirstApp(App):
def build(self):
return Premade()
if __name__ == '__main__':
MyFirstApp().run()
and my .kv file:
#:kivy 1.10.0
<Premade>:
orientation: 'Verticle'
TextInput:
id: my_textinput
font_size: 150
size_hint_y: None
height: 200
text: 'default'
FloatLayout:
Scatter:
Label:
text: my_textinput.text
font_size: 150
Supposedly, when I run the python code, the .kv file would be loaded, but all I get is a black screen. I named my .kv file according to the rule, in this case, is myfirst.kv and the .kv file is also in the same directory as the python module file. I also tried to use the build function but that didn't work either. Can anyone help?
Check your .kv filename, if you want it to be loaded automatically you should name it MyFirst.kv because your app is called MyFirstApp. An alternative solution would be to load the file manually as the following:
from kivy.lang import Builder
Builder.load_file('filename.kv')
Has to be called myfirstapp.kv or the same name as the app class
Related
Here is the kivy file code:
#:kivy
<MyGridLayout>:
GridLayout:
cols:1
GridLayout:
cols:2
Label:
text: "Name"
TextInput:
multiline:False
Label:
text: "Time"
TextInput:
multiline:False
Label:
text: "Mood"
TextInput:
multiline:False
Button:
text:"Submit"
here is the main file code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
# from numpy import full
class MyGridLayout(Widget):
def button_pressed(self, instance):
name = self.name.text
time = self.time.text
mood = self.mood.text
self.add_widget(Label(text=f'hey, {name}\nAccording to your {mood} mood at {time}, we have some suggestions in songs: '))
self.name.text=""
self.time.text=""
self.mood.text=""
class QuizMasters(App):
def build(self):
return MyGridLayout()
if __name__=='__main__':
QuizMasters().run()
Whenever running the file facing the same issue of blank screen, i have saved the main file as QuizMasters.py and the kivy file as QuizMasters.kv , can someone please help...
You haven't specified the kivy version in .kv file. Try setting it like,
#:kivy 2.0.0 # Or replace with your installed version.
.
.
.
When I ran your code, I got an error in the .kv file. Once I deleted the #:kivy, it worked out for me.
If that doesn't work, MyGridLayout inherits from the Widget which makes it a Widget that must be added. Try switching into a screen, and tell me if that works.
Also, don't forget, the name of the .kv file must be the same as your QuizMasters main class. If your main class includes app, you should not include it. In your case, your .kv file should be named quizmasters.kv ALL LOWERCASE
OK, so i'm trying to make an app using python and kivy.From tutorials and from search on google i've seen people using kivy language to simplify their code but when i try to create a kv file for my program i get an "Invalid property name" ERROR and i dont know what to do.I did everything the guy on the tutorial did and followd every instruction on google but still i get this error.Did anyone had the same problem before and how did you solve it? Thanks in advance!
This is my python file:
import kivy
#kivy.require('1.0.6') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang import Builder
from kivy.properties import NumericProperty
class QuizWidget(BoxLayout):
pass
class MyQuizApp(App):
def build(self):
return QuizWidget()
if __name__ == '__main__':
MyQuizApp().run()
This is my kivy file:
<QuizWidget>:
orientation = 'vertical'
TextInput:
id: Start_text
font_size: 100
size_hint_y: None
height: 200
text: 'Wellcome to my quiz.Press START to continue'
Button:
text:'START'
font_size: 150
orientation = 'vertical'
This should be orientation: 'vertical'. Kivy is trying to parse the whole thing as a property name since you didn't have a :. The error should probably point to this line, but you didn't give enough info to check.
I'm currently writing an app in Kivy for a school project (I have very much had to jump in the deep end with Kivy). I have written the kv code for the text input, which you can see below:
AnswerInput:
<AnswerInput#BoxLayout>:
orientation: "vertical"
BoxLayout:
height: "40dp"
size_hint_y: None
TextInput:
size_hint_x: 20
Button:
text: "Check Answer"
size_hint_x: 25
I now need to get the text box to display in the Python file; however, I am at something of a loss as to how I would do this? My Python code is below:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class TextInputTest(App):
def __init__(self, *args, **kwargs):
return TextInput
if __name__ == '__main__':
TextInputTest().run()
I am almost certain that I am missing something here, probably something very simple, but I am very much a beginner with Kivy. If anyone could put me on the right track, I would be very grateful.
Firstly, this isn't clear but you need to seperate your code into a py file and a kv file. It seems like you've done this already. Your kv file also needs to be all lowercase
In your py file, you then add a class for the kivy widget. In this case:
from kivy.uix.boxlayout import BoxLayout
class AnswerInput(BoxLayout):
pass
Then in your kv file:
<AnswerInput>:
orientation: "vertical"
BoxLayout:
height: "40dp"
size_hint_y: None
TextInput:
size_hint_x: 20
Button:
text: "Check Answer"
size_hint_x: 25
AnswerInput from your py looks into your loaded kv file to see if there is a root widget with the same name as itself.
(RootWidget meaning the top widget of a bunch of kv logic encased in <>)
You have to however first know how to load a kv file, there are two ways to do this. If you're using just one kv file, you can name your app the same as your kv file.
So if your kv file is
textinputtest.kv
Your app class in py would read
TextInputTest(App):
or
TextInputTestApp(App):
You don't need to do this, you can also use the builder module to load the file itself (and in fact you will need to do this if you have more than one kv file).
To do this, you do this in your py file:
from kivy.lang.builder import Builder
Builder.load_file('textinputtest.kv')
You're also returning an object of the textinput class, what you want to do is return an object of your customized textinput class.
Your Py file would look like this:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class AnswerInput(BoxLayout):
pass
class TextInputTest(App): # If your kv file is called textinputtest.kv
def build(self):
return AnswerInput()
if __name__ == '__main__':
TextInputTest().run()
Or you can name your app anything you want and then use builder to load the relevant kv file directly into your app.
I'm making an app to rename files that come from a scanner in a specific format.
The idea is to scan the directory and load a list of files, which are renamed according to the text in the textinput box. I've stripped down the app to come directly to the problem
import csv, os, datetime, string, shutil
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.properties import ObjectProperty, ListProperty, StringProperty, DictProperty
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from functools import partial
from kivy.uix.scrollview import ScrollView
class TestScreen(Screen):
def update_text(self, text):
print(text)
class ScreenManagement(ScreenManager):
pass
class TestApp(App):
default_font_size = 15
def build(self):
compiled_kv_file = Builder.load_file("TestApp.kv")
return compiled_kv_file
if __name__ == "__main__":
print("Main loop")
TestApp().run()
and the kv file is
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition:FadeTransition()
TestScreen:
<TestScreen>:
name: "TestScreen"
Button:
text:"Test Screen"
size_hint: 0.2,0.1
pos_hint: {"x":0.2,"y":0.8}
font_size: 20
TextInput:
id: my_textinputa
text:"File Screen"
size_hint: 0.2,0.1
pos_hint: {"x":0.4,"y":0.8}
font_size: 20
on_text:
root.update_text(self.text)
Label:
size_hint: 1,0.5
pos_hint: {"x":0,"y":0}
text: my_textinputa.text
font_size: 20
I'm using windows 10 and eclipse with pydev, if thats relevant. The textinput returns the entered text to the .py file. But the textinput does not update the entered text. Furthermore, There seem to be two labels rather than 1.
My gut says that I'm somehow adding two sets of widgets on top of each other. Since only the top one is changed, the text does not change on the bottom one. The output in the console is correct. Yet the textinput box doesnt update itself.
Can someone suggest how to get the textinput to update correctly?
Having multiple .kv files defining the root widget can produce this issue. One solution would be to drop the Builder.load_file("TestApp.kv")
class TestApp(App):
default_font_size = 15
def build(self):
pass
and rename the .kv file to the name of the App instance (test.kv in this case). In this way, kivy will only load one .kv file as the root widget.
Kv language has a way to import names from other files using the following syntax:
#:import name x.y.z
# The same as `from x.y import z as name` in Python code
But it doesn't mention how to import the values from the same module where that kv language is used.
Let's say I have the following code:
from kivy.app import App
from kivy.lang import Builder
from kivy.atlas import Atlas
from kivy.uix.button import Button
theme = Atlas('path/to/atlas')
Builder.load_string('''
<MyWidget>:
background_normal: theme['widget_bg']
''')
class MyWidget(Button):
pass
class TestApp(App):
def build(self):
return MyWidget()
TestApp().run()
I'd like to import the theme Atlas object into the kv code to set the proper background. How could this be done?
You can refer to your current module as __main__
#:import theme __main__.theme
<MyWidget>:
background_normal: theme['widget_bg']