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.
Related
I'm following a tutorial on youtube and when I run the code, the window shows the labels half off the screen on the left-bottom side. I uploaded a screenshot of the window.
The .kv file is:
<MyGrid>
GridLayout:
cols: 1
GridLayout:
cols: 2
Label:
text: "Name: "
TextInput:
multiline:False
Label:
text: "Email: "
TextInput:
multiline:False
Button:
text: "Submit"
And the py file is:
from kivy.app import App
from kivy.uix.widget import Widget
class MyGrid(Widget):
pass
class MyApp (App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
You are trying to use the base Widget class as a container, but it was not designed for that use. Try just using some Layout class as the base for MyGrid, like this:
class MyGrid(FloatLayout):
pass
Note that this will result in a GUI that is a FloatLayout that contains a GridLayout which contains another GridLayout. If that is not your intention, consider changing the base class of MyGrid to GridLayout and eliminate one or two of the contained GridLayouts.
I'm trying to learn how to use multiple kv files in one project. But I ran into this issue:
Here's the screeenshot of my issue.
.py
from kivy.lang.builder import Builder
from kivymd.app import MDApp
Builder.load_file("cm_dd_btns.kv")
kv = """
#:import hex kivy.utils.get_color_from_hex
MDScreen:
md_bg_color: hex("#07074D")
DD:
"""
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
MyApp().run()
.kv file
<DD#MDBoxLayout>
cols: 1
spacing: 1
orientation: "vertical"
Button:
text: "First btn"
Button:
text: "2nd btn"
I tried modify the size of the layout and widget in both the files. But it's not making both buttons visible.
Can someone explain how to fix this and why this is happening?
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.
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
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