kivy file returning blank screen - python

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

Related

Can someone provide a working code of loading data of text file to Label of kivy

I am able to get the data in main app class and run it to get as Label but when I want to do the same by getting data in main app class where as defining Label in another class, it's giving name error.
import kivy
from kivy.app import App # import Kivy App module to create
from kivy.uix.label import Label # import Label Module
kivy.require('1.11.1')
class MyKivyApp(App):
def build(self):
f=open('tesit.txt','r')
t=f.read()
return Label(text =t)
rt=MyKivyApp()
rt.run()
Your lines:
rt=MyKivyApp()
rt.run()
are inside the MyKivyApp. Just unindent those two lines to get them outside of the App class.
This's how we can read a text file and display it as label in another screen in kivy.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class Screen1(Screen):
f=open('test.txt','r')
t=f.readlines()
b=t[0]
f.close()
class Screen2(Screen):
username = StringProperty("")
root = Builder.load_string('''
<Screen1>:
name:'xy'
id:loginscreen
BoxLayout:
orientation: "vertical"
Button:
text:'Execution button'
on_release: root.manager.current = "screen2"
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: root.username
Button:
text:'Back to menu'
size_hint:.3,.3
on_release: root.manager.current = "xy"
ScreenManager:
Screen1:
id:loginscreen
Screen2:
username: loginscreen.b
''')
class MyApp(App):
def build(self):
return root
r=MyApp()
r.run()

Display widget when the user wants to in kivy

it is a simple program but i can not find a way to make it work. i just want to add a widget in boxlayout2 when the user presses the button (and has not writed anything in textinput) which is located in boxlayout1 .The widget do not display in screen.What should i do?
main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
BoxLayout2()
class BoxLayout2(BoxLayout):
def Print(self):
self.add_widget(Button(text='hello'))
class TestApp(App):
pass
TestApp().run()
and here is my kivy code
test.kv
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
I see the presentation layout i want but the button is nowhere to be found.
To make it clear let's follow the stream of the app you've written.
it creates a BoxLayout and puts BoxLayout1 and BoxLayout2 in it, the second one doesn't have any content. When you click on write, the app checks the content of the text box and if valid, calls the constructor of BoxLayout2! Now at this point it creates an instance of this class, but does not keep it's reference so it will be discarded immediately. Now what you want is to call a function of a currently existing instance, not to create another one. Here's the code:
python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
self.parent.ids.bxl2.addButton()
# BoxLayout2()
class BoxLayout2(BoxLayout):
def addButton(self):
button=Button(text='hello')
self.add_widget(button)
kivy language:
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
id:bxl2

How to get a text input box to display with Kivy?

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.

python code won't run with .kv file

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

Why isnt the app updating the textinput

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.

Categories

Resources