blank Kivy window opens at random - python

I'm developing a python app using Kivy.
Sometimes when I'm launching other python scripts without kivy imports a blank Kivy window opens randomly without any reason.
Is this a bug? Or am I missing something on the app closing?
I've seen this strange behaviour only under Windows (8), Python 2.7.
The window is completely white and seems not responding (I know it's a Kivy window from the little icon on the top-left corner), few seconds later the system says python.exe has stopped working.

This is not a bug. You have to add child widgets e.g. button, label, textinput, etc. into your root widget. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class MyRootWidget(FloatLayout):
pass
class TestApp(App):
title = "Kivy Demo"
def build(self):
return MyRootWidget()
if __name__ == '__main__':
TestApp().run()
test.kv
#:kivy 1.10.0
<MyRootWidget>:
canvas:
Color:
rgba: [1, 1, 1, 1] # White color
Rectangle:
size: self.width, self.height
BoxLayout:
orientation: "vertical"
Label:
font_size: 50
text: "[color=ff3333][b]Hello[/b][/color]"
markup: True
Label:
font_size: 20
text: "[color=3333ff]World[/color]"
markup: True
Output

Related

kivy box layout spacing not working in .kv file

I wrote a simple kivy app that show a root widget with 3 buttons vertically , with some padding and spacing between the buttons , when i use kivy BoxLayout spacing in the same python script it works perfectly , but when try to do the same thing using .kv file , only padding works , the spacing doesn't work .
here is the code :
main script :
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kivy.require('2.0.0')
Builder.load_file('temp001.kv')
class Root(Widget):
pass
class Main(App):
def build(self):
return Root()
if __name__ == '__main__':
Main().run()
The .kv file code :
<Root>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
sapcing: 50
padding: 100
Button:
text: "hello"
Button:
text: "hey"
Button:
text: "wow"
The Result :
Did you mistype "spacing" only in this example or in your actual .kv file as well?
In case you did, try typing it correctly.

Height and Width values don't work on Kivy Python

I am developing a Kivy app on Python and wanted to place my widgets relative to the height and width of the main layout but it doesn't work till I manually change the size of app on pc, which is not allowed manually on Android.
Here is .py code:
from kivy.app import Appfrom kivy.uix.widget import Widget
class UI(Widget):
pass
class UIApp(App):
def build(self):
return UI()
app = UIApp()
app.run()
And .kv part:
#:kivy 1.0.9
<UI>:
padding: root.height*.05,root.height*.05
spacing: root.height*.05,root.height*.05
Label:
center_x: root.width*.25
center_y: root.height*.9
text: "Hello World"
color: 0,1,0,1
size: root.width*.3, root.height*.06
font_size: root.height*.03

Kivy (Python) Problem with AttributeError after add_widget with kv-file

I'am new in Kivy and have follow problem (Environment is Python 3.7 with Kivy-1.11.1.):
I need a navigation area and a view area (=ViewScreen). With the navigation area i change the view area (change of kv-files - look later at 'def next_screen'). My problem is, that i can't interact with widgets (e.g. 'lblTest') in the view area.
I use follow files:
testGUI.py (= GUI Application)
testGUIRoot.kv (= RootWidget as kv-file)
testGUIBtn1.kv (= view area 1 as kv-file)
testGUIBtn2.kv (= view area 2 as kv-file)
The GUI Application is simple and starts the GUI and change the view area.
testGUI.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# runs select application
def startApplication (self, instance):
print(self.lblTest)
class mainGUI(App):
def build(self):
# loading the content of root.kv
self.root = Builder.load_file('testGUIRoot.kv')
def next_screen(self, screen):
#Clear container and load the given screen object from file in kv folder.
filename = screen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.root.container.clear_widgets()
# load the content of the .kv file
screen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.root.container.add_widget(screen)
if __name__ == '__main__':
'''Start the application'''
mainGUI().run()
I use follow kv-files:
testGUIRoot.kv:
#:kivy 1.11.1
RootWidget:
container: container
orientation: 'horizontal'
# Navigation
BoxLayout:
orientation: 'vertical'
size_hint: (0.35, 1)
Button:
text: 'testButton1'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn1')
Button:
text: 'testButton2'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn2')
# ViewScreen
BoxLayout:
size_hint: (0.65, 1)
id: container
orientation: 'vertical'
padding: 0
spacing: 3
Label:
text: 'no data'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn1.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 1'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn2.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 2'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
Follow error appears:
AttributeError: 'RootWidget' object has no attribute 'lblTest'
Have you a solution to interact with the Object 'lblTest'? For example like self.lblTest.text = 'Test-Text'.
Thank you in advance
I have been working with Kivy just this week, also as a new user.
The thing I have learned is to define properties on your RootWidget, just as the labels you already defined in the .kv files. This 'links' the layout and the Python code to eachother.
First of all you need to import the Kivy ObjectProperty by adding:
from kivy.properties import ObjectProperty
Next up is to declare the property on your RootWidget class.
You can add lblTest = ObjectProperty(None) right after the class-declaration.
The top of your file testGUI.py should look like this then:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# Link the layout objects
lblTest = ObjectProperty(None)
# runs select application
def startApplication (self, instance):
print(self.lblTest)
The page that really helped me with this is https://techwithtim.net/tutorials/kivy-tutorial/object-properties/
A little sidenote is it would be best to keep your id attributes fully unique.
I found the solution
Content of 'def next_screen' of mainGUI(App) to 'def startApplication'. Now i can change widgets in object oScreen or using the object in other python libs.
def startApplication (self, instance, sScreen):
filename = sScreen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.container.clear_widgets()
# load the content of the .kv file
oScreen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.container.add_widget(oScreen)
print(oScreen.ids.lblTest.text)
The follow should be added in kv-files testGUIBtn1.kv, testGUIBtn2.kv:
RootWidget:
In kv-file testGUIRoot.kv you have change on_release to
on_release: root.startApplication(self,'testGUIBtn1')

the .kv file is not being attached on Ubuntu

I just started using Python Kivy
I have this two files
# main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class Container(BoxLayout):
pass
class MyApp(App):
def build(self):
return Container()
if __name__ == '__main__':
MyApp().run()
and
# My.kv
<Container>:
orientation: 'vertical'
padding: 50
spacing: 25
Button:
size: 100, 100
size_hint: None, None
text: 'Hello'
Button:
text: 'World'
everything works on Windows, but I need Linux to build it.
if I run the same files on Ubuntu 18.04, I will get a black screen. When working without .kv there is no such problem, all widgets are displayed.
What's the matter?
Your My.kv should be named my.kv. Since Windows doesn't care about upper/lower case in file names, it works there.

Kivy Popup Shows Background Widgets

I am trying to use the popup widget in Kivy, but everytime I run the code, the popup just has 2 smaller versions of the widget on the main screen.
This is my Python code (the .py file):
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.widget import Widget
class Layout(Widget):
pass
class KivyTestApp(App):
def build(self):
return Layout()
app = KivyTestApp()
app.run()
and this is my Kivy code (the .kv file):
#: import Factory kivy.factory.Factory
<MyPopup#Popup>:
title: 'Test'
size_hint: None, None
size: 400, 400
<Layout>:
Button:
id: but
size: root.width, root.height
background_normal: ''
background_color: .5, .7, .9, 1
text: 'Press me to open the popup'
pos: 0, 0
on_press: Factory.MyPopup().open()
This creates a window that looks like this:
And the popup looks like this:
As you can see, I have added no content to the popup, yet Buttons still appear! If anyone could help me, that would be great, and thanks in advance!
Kivy already has an internal widget called Layout, which is subclassed all over the place. Try naming your own widget something else.

Categories