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.
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 have my simple python code:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
Builder.load_file('Matrixkivy.kv')
class MyLayout(Widget):
pass
class MatrixCalc(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
MatrixCalc().run()
And simple .kv code:
<MyLayout>
GridLayout:
size_hint: root.width, root.height
Button:
text: "Button1"
Button:
text: "Button2"
I want to get a GridLayout which is of size of the entire app and two buttons, which both take half of it. However, app displays all things that the app contain in the bottom left corner. How to fix that?
The main problem is that you are using the base Widget class as a container. You can do that, but the Layout classes already have the code to handle things like size_hint and pos_hint. One fix for your code is to replace:
class MyLayout(Widget):
pass
with:
class MyLayout(FloatLayout):
pass
You should also remove the line:
size_hint: root.width, root.height
Size_hint values should be numbers typically in the range 0-1. And add a line that defines either rows or cols for the GridLayout, so your kv could look like:
<MyLayout>
GridLayout:
cols: 1
Button:
text: "Button1"
Button:
text: "Button2"
Since you are using GridLayout as a child of MyLayout, you can just make MyLayout extend GridLayout, and then you can eliminate the GridLayout line from your kv. With this approach, your MyLayout class would look like:
class MyLayout(GridLayout):
pass
and the corresponding kv:
<MyLayout>
cols: 1
Button:
text: "Button1"
Button:
text: "Button2"
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.
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.