kivy BoxLayout is not displaying my image when using vertical orientation? - python-3.x

i am using kivy BoxLayout with two Buttons, one of the buttons is to display an image. the image displays fine when using the horizontal orientation, but when I use the vertical orientation it will not display my image. windows 10, python 3.6.4, kivy 1.10.0
.py file:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
class ClickerScreen(Screen):
pass
class Manager(ScreenManager):
clicker_screen = ObjectProperty(None)
class ClickerApp(App):
def build(self):
return Manager()
if __name__=='__main__':
ClickerApp().run()
.kv file:
#: import Window kivy.core.window.Window
<Manager>:
id: screen_manager
clicker_screen: clicker_screen
ClickerScreen:
id: clicker_screen
name: 'clicker'
manager: screen_manager
BoxLayout:
orientation: 'vertical'
Button:
background_color: 0,0,0,0
Image:
source: 'Images/button.png'
allow_stretch: False
keep_ratio: True
size: self.parent.size
Button:

Related

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

Python - Kivy: Label does not update during a function excution

I saw that there are other topics related to this one but I could make this simple project to work.
I need a label to be updated with the function status while it is running just to let the user know that the program still working while the function is processing, but the label is only updated when the function is completed
Here are the files
my.kv
WindowManager:
ChangeLabel:
<ChangeLabel>:
BoxLayout:
orientation: 'vertical'
padding: 200, 20
Label:
id: labeltxt
text: 'Text to change'
font_size: 30
height: 50
Button:
text:'Run Function'
size_hint_y: None
height: 30
on_release:
root.changeLabel()
Label:
test.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock
class ChangeLabel(Screen):
def changeLabel(self):
Clock.schedule_once(self.test)
def test(self, dt):
self.ids.labeltxt.text = 'Function is running...'#I wanna this in lablel while function is running
time.sleep(2)# function to run
self.ids.labeltxt.text = 'Completed'
class WindowManager (ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MyApp(App):
def build(self):
return kv
MyApp().run()
I did this solution, now it is working the way I want, but not sure if this is the best way.
test.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock
class ChangeLabel(Screen):
i = 0
def changeLabel(self):
Clock.schedule_interval(self.test, 0.05)
def test(self, dt):
if self.i == 0:
self.ids.labeltxt.text = 'Function is running...'
self.i = 1
else:
time.sleep(2)# function to run
self.ids.labeltxt.text = 'Completed'
self.i = 0
Clock.unschedule(self.test)
class WindowManager (ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MyApp(App):
def build(self):
return kv
MyApp().run()

KivyMD DatePicker not resizing with screen size

I'm not sure how to dynamically resize the MDDatePicker with the screen size, it doesn't adjust correctly like Labels and Buttons but rather adjusts erratically.
I couldn't find much on KivyMD documentation like there in on Kivy widgets.
You've got to click the Select Date button to open the Date Picker
py file
import kivy
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.text import Label as CoreLabel
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.theming import ThemeManager
import mysql.connector
from kivymd.uix.picker import MDDatePicker
from kivy.uix.scrollview import ScrollView
Window.clearcolor = (1,1,1,1)
class Information(Screen):
def select_date(self):
picker = MDDatePicker(callback=self.got_date)
picker.open()
def got_date(self, the_date):
print(the_date)
class WindowManager(ScreenManager):
pass
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
kv = Builder.load_file("kivy.kv")
sm = WindowManager()
screens = [Information(name="information")]
for screen in screens:
sm.add_widget(screen)
sm.current = "information"
return sm
if __name__ == '__main__':
MyApp().run()
kv file
<Information>:
name: "information"
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
NavigationDrawerIconButton:
text: "Test"
on_release: app.root.current = "login"
FloatLayout:
MDToolbar:
pos_hint: {'top': 1}
md_bg_color: 0.2, 0.6, 1, 1
left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]
MDRaisedButton:
text: "Select date"
pos_hint: {"x": 0.35, "top": 0.6}
on_release: root.select_date()
Thanks in advance
you could try under MDRaisedButton putting this:
MDRaisedButton:
size: root.width, root.height
or where ever you need to resize. The size commands format is size: x, y in kv lang

return a BoxLayout inside another one in Kivy?

I'm trying to make a simple application in Python 3.5 and kivy that starts with a simple screen and when you click on it, goes to another one which shows 3 lists that let you chose the data:
The Python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.properties import ListProperty
from dataTopy import rlists
# Transition des ecrans:
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("ex44.kv")
#
class FirstListItemButton(ListItemButton):
pass
class SecondListItemButton(ListItemButton):
pass
class ThirdListItemButton(ListItemButton):
pass
class Ex44(BoxLayout):
d1 = ListProperty([str(i) for i in range(1990,2014)] )
d2 = ListProperty(['']*100)
d3 = ListProperty(['']*100)
def change(self,c):
try: self.d2,self.d3 = rlists(int(c.text))
except:
import os
CurDir = os.getcwd()
print('Can not find data in ' + CurDir)
def change1(self,c):
print('M => '+c.text)
def change2(self,c):
print('F => '+c.text)
class Ex44App(App):
def build(self):
return presentation
if __name__ == '__main__':
Ex44App().run()
The kivy file:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ex44 ex44
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "other"
text: "Next Screen"
font_size: 50
<AnotherScreen>:
name: "other"
BoxLayout:
Ex44
Button:
color: 0,1,0,1
font_size: 25
size_hint: 0.3,0.2
text: "Back Home"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
<FirstListItemButton>:
on_press: app.root.change(*args)
<SecondListItemButton>:
on_press: app.root.change1(*args)
<ThirdListItemButton>:
on_press: app.root.change2(*args)
<Ex44>:
ListView:
adapter:
ListAdapter(data=root.d1,
selection_mode='single',
cls=ex44.FirstListItemButton)
ListView:
adapter:
ListAdapter(data=root.d2,
selection_mode='single',
cls=ex44.SecondListItemButton)
ListView:
adapter:
ListAdapter(data=root.d3,
selection_mode='single',
cls=ex44.ThirdListItemButton)
When I try to run the app, it tells me: "Unknown class "
It is weird because the class Ex44 works alone but not when I'm trying to add it into the main application logic.
I've tried to return a widget instead of a BoxLayout for the class, to return Ex44 alone in the kivy file, etc. but I always get the same error in return.
Is it possible to return a BoxLayout inside another one in Kivy?
You are building the kv file too soon (before the class is defined). move the Builder.from_file call to the build method
...
def build(self):
return Builder.load_file("ex44.kv")

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.

Resources