My kivy code is not displaying anything. Please tell me where I am going wrong - python-3.x

i have designed a kivy form which takes some information from the user..But none of it is getting displayed..Please tell me where I am going wrong.
My code:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.floatlayout import FloatLayout
class Widgets(Widget):
pass
class YourDiaryApp(App):
def build(self):
return Widgets()
if __name__ == "__main__":
YourDiaryApp().run()
Kivy code:
<Widgets>:
FloatLayout:
Label:
text:'What do you want me to call you'
TextInput:
id:username
Label:
text:'What do you wanna call me'
TextInput:
id:diaryname
Label:
text:'What will be your password'
TextInput:
id:password
Button:
text:'Complete Setup'

Is your KV file named "yourDiaryApp.kv"? From the docs:
There are two ways to load Kv code into your application:
By name convention:
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
My guess would be the KV code isn't being loaded.

Related

How to use Splitters in kivy with multiple screens?

This is a simple example but essentially I need to use Splitter to separate and resize two individual Text input boxes horizontally, however when I'm trying to use the Splitter widget in a Screen its creating a double behind the original content and the Splitter is not functioning properly. Any help will be appreciated. Thank You.
main file
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen
from kivy.core.window import Window
from kivy.factory import Factory
class TestScreen1(Screen):
pass
class TestScreen2(Screen):
pass
class Manager(ScreenManager):
pass
class MyApp(App):
def build(self):
return Builder.load_file("D:\MainProject\TestFiles\my.kv")
if __name__ == "__main__":
MyApp().run()
KV file
Manager:
TestScreen1:
TestScreen2:
<TestScreen1>:
name:"abc1"
BoxLayout:
orientation: 'horizontal'
Label:
text:"Next"
Splitter:
sizable_from:"left"
Button:
text: "next"
on_press: root.manager.current="abc2"
<TestScreen2>:
name:"abc2"
BoxLayout:
orientation: 'horizontal'
Label:
text:"pre"
Splitter:
sizable_from:"left"
Button:
text: "pre"
on_press: root.manager.current="abc1"
Your kv file is being loaded twice. Once by you Builder.load_file() and once by the App (see documentation). Just remove that Builder.load_file() line.

Kivy/md - How to change Screens with ScreenManager, can I do it this way...?

I've got this simple code to change screens. I'm just wondering what the correct variable to change in callback() is to invoke a screen change. I've seen this done other ways, but wanted to make sure I have a static MDToolbar so when the screen changes, the toolbar does not move.
The code below does does not change screen in callback()in this
.py code:
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivy.loader import Loader
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.label import MDLabel
from kivymd.uix.floatlayout import MDFloatLayout
class TCalc(MDApp):
def callback(self,obj):
self.root.current = "tipscreen2"
def build(self):
return Builder.load_file('dip.kv')
TCalc().run()
dip.kv:
MDNavigationLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
right_action_items: [["cog", lambda x: app.callback(x)]]
ScreenManager:
id: screen_manager
Screen:
name: "tipscreen1"
MDFloatLayout:
MDLabel:
text: "Screen 1"
Screen:
name: "tipscreen2"
MDFloatLayout:
MDLabel:
text: "Screen 2"
MDNavigationDrawer:
id: nav_drawer
MDNavigationDrawerMenu:
The code below does does not change screen in callback()in...
That's because in method callback you did self.root.current. This is supposed to be applicable if your self.root is a ScreenManager instance which is indeed not the case here.
Now, by doing lambda x: app.callback(x) you are actually passing the instance itself (here, MDActionTopAppBarButton). So if you want to pass some var. through the callback method to change screens, one of the different ways could be simply pass the ScreenManager object (here a weak reference actually). Or, you can just access the ScreenManager by ids (again a weak reference) directly from python. Both way the solution could be something like this,
In kvlang,
right_action_items: [["cog", lambda x: app.callback(x)]]
# or, right_action_items: [["cog", app.callback]]
In python,
def callback(self,sm):
# or, def callback(self, *args):
sm.current = "tipscreen2"
# or, self.root.ids.screen_manager.current = "tipscreen2"

Kivy ScreenManager: App stopped running after trying to add 2nd Window

I was building an app for my thesis and it worked so far quite well while using only one window. Now I was trying to add other windows for e.g. instructions. I was following the documentation for screen Manager and some examples for apps and tried to add my 2nd window alike. But somehow it fails to identifiy the ScreenManager class in my kv.file. Maybeyou can help out, I'm not sure what I'm missing.
main py: Peenomat.py
import kivy
# -*- coding: iso-8859-1 -*-
from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.core.text import LabelBase
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.properties import ObjectProperty
#additional .kv files used to speciffy layout and functions of peenomat.kv
Builder.load_file('Header.kv')
Builder.load_file('Statusbar.kv')
Builder.load_file('Inputparameters.kv')
Builder.load_file('Outputparameters.kv')
pm = Builder.load_file('peenomat.kv')
#Layout
"""
class Peenomat(AnchorLayout):
pass
"""
class Peenomat(Screen):
pass
class Instruction(Screen):
pass
class WindowManager(ScreenManager):
pass
#App
class PeenomatApp(App):
def build(self):
return pm
if __name__=="__main__":
PeenomatApp().run()
and the minimalized .kv file
peenomat.kv:
WindowManager:
Peenomat:
Instruction:
<Peenomat>
name: "peenomat"
AnchorLayout:
anchor_x: 'left'
anchor_y: 'bottom'
GridLayout:
cols: 1
Header:
id:
size_hint:
InputParameters:
id:
size_hint:
StatusBar:
id:
size_hint:
OutputParameters:
id:_
size_hint:
<Instruction>:
name: "instruction"
Button:
text: "Verstanden!"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
So like I said the App worked before adding the Screen and ScreenManager classes and I'm getting the error:
Traceback (most recent call last):
File "C:/Users/schum/Dokumente/TUD/Masterthesis/Peenomat.py", line 21, in <module>
pm = Builder.load_file('peenomat.kv')
File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
return self.load_string(data, **kwargs)
File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\lang\builder.py", line 399, in load_string
widget = Factory.get(parser.root.name)(__no_builder=True)
File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\factory.py", line 131, in __getattr__
raise FactoryException('Unknown class <%s>' % name)
kivy.factory.FactoryException: Unknown class <WindowManager>
eventhough they got the same name. Hope you can help me out!
So I found a solution by using an implementation of the ScreenManager in the py file. I switched this with the class WindowManager(ScreenManager) class:
sm = ScreenManager()
sm.add_widget(Peenomat(name='peenomat'))
sm.add_widget(Instruction(name='instruction'))
and therefore added to my builder:
class PeenomatApp(App):
def build(self):
return sm
I fully deleted the 3 lines with the WindowManager class out of my kv.file. Now it works perfectly fine

How to load string from text file to kivy label, Python 3.5

I've been searching around for answers on stackoverflow for a couple of days now to be honest but I could not find the thing for me, lets say I have a text file named bind.txt with a couple lines of text, how can i load that text file to a kivy label? be it directly or indirectly. I've been trying to teach myself python and this is kind of in the way of me building my first app. Thank you in advance, and heres the code.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MainScreen(Screen):
pass
class MainLabel(ScrollView):
text = StringProperty("")
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("bind.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
And the kv file.
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
<MainLabel>:
text: #bind.txt here, somehow..
Label:
text: root.text
font_size: 15
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
<MainScreen>:
name: "main"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'img/Fundal.png'
MainLabel
You can open file in reading mode, store contents in a variable and assign it to the text property.
For an example
with open("bind.txt") as f:
contents = f.read()
main_label.text = contents # main_label is an instance of kivy's Label class.

Updating a Label In Kivy

I am trying to update a label in python. It is a simple Guess the num game. Currently it prints the Computer responses in PC but I want it to print those to a label.
here is the main .py file
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random
secret=random.randint(1,100)
class Application(BoxLayout):
label1=""
def button_press(self):
global label1
if (int(self.user_guess.text)<secret):
print("Higher")
self.label1="Higher"
elif(int(self.user_guess.text)>secret):
print("Lower")
self.label1="Lower"
elif(int(self.user_guess.text)==secret):
print("You WOn")
self.label1="You WOn"
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()
the .kv file
Application:
<Application>:
size:(480,30)
user_guess:guess
size_hint:(None,None)
text:""
orientation: "vertical"
BoxLayout:
Button:
text:'Play'
on_press:root.button_press()
TextInput:
id:guess
text:''
Label:
text:root.label1
I think you should use
self.label1.text="Higher"

Resources