I am trying to increase the width and the height of a kivyMD button, but it is not supported (size_hint).
•Should I make my own button class that inherits from the Button class ?
•Would that be bad considering I want my app to go on android ?
I would like to know if anyone solved this issue before. Also, if I increase the dimensions of the button, I want the size of the text to change, too...
For example with MDRaised button type
This is how you do it:
MDRaisedButton:
text: "Do Something"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.do_anything()
size: 30, 30
size_hint: None, None
Make sure you set size_hint to None, None and then specify size: width, height
This is an old post but I ran into the same issue.
I had to use it a percentage of the root width and it worked.
Example below:
MDRectangleFlatButton:
text: "Some text"
pos_hint: {"center_x": .5}
size_hint: None, None
width: root.width*0.4
Related
I'm still learning and I'm getting an error *Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute. *
BoxLayout:
size_hint: None, None
size: area_img.size
pos_hint: {'center_x': .5, 'center_y': .5}
Court:
id: area_img
source: 'half-court.png'
size: self.texture_size
MDBottomNavigation:
MDBottomNavigationItem:
name: "btnSave"
text: "Save"
icon: "images/save.png"
selected_color_background: 147/255,84/255,48/255
on_tab_release: root.show_save()
MDBottomNavigationItem:
name: "btnClear"
text: "Clear Screen"
icon: "images/clear.png"
selected_color_background: 147/255,84/255,48/255
on_tab_release: root.clear_drawing()
MDBottomNavigationItem:
name: "btnBack"
text: "Home"
icon: "images/home.png"
selected_color_background: 147/255,84/255,48/255
# on_tab_release: app.root.current = 'main'
I'm trying to include the MDBottomNavigation to the screen so I can save, clear the output of the data and can easily go back to home screen. This is my kv file. Without the MDBottomNavigation it's working well, but when I include it, that's the error being said.
How it looks like with MDBottomNavigation
Without
So I'm very new to KIVY and I'm trying to make a simple menu sort of thing. The python file is:
from kivy.app import App
class MenuApp(APP):
pass
MenuApp().run()
and the kivy file:
Interface:
<Interface#BoxLayout>:
Label:
text: "logo"
BoxLayout:
orientation: "vertical"
BoxLayout:
Button:
text: "b1"
size_hint: .3, 1
Button:
text: "b2"
Button:
text: "b3"
size_hint: .3, 1
pos_hint: {"center_x": 0.5}
Button:
text: "b4"
size_hint: .3, 1
pos_hint: {"right": 1}
The problem comes from buttons "b1" and "b2". when I give them a size_hint it doesn't change anything. The size_hints on the rest work and these 2 work when i take them out of their BoxLayout. Is this just a thing? that you cant put boxlayers inside 2 other boxlayers? or am I doing something wrong?
from the BoxLayout documentation::
The size_hint uses the available space after subtracting all the
fixed-size widgets.
So the BoxLayout divides the available space according to the size_hint value. In your code the b1 Button has size_hint of .3, while b2 has the default size_hint of 1.0. The sharing algorithm then gives b1 a 0.3/(1.0+0.3) (or 0.230769) share of the available space, and b2 gets 1.0/(1.0+0.3) (or 0.769231) share of the available space. You must consider the size_hints of both of the Buttons, including the default values if you don't assign one.
I'm currently working on an application with Kivy.
On one screen I have a TextInput and a button. I can input text and go to the next screen by pressing the button.
My question is: Is there a way to just hitting the 'return'-key after typing in your text and automatically press the button without actually touching it?
And a small question on the side: I put
focus: True
under TextInput so it would focus the textinput immediately when I go to the screen. But it won't work. After some google searching a lot of people said it was a bug, although those comments are fairly old (< 1 year). Is this still bugged or is there a solution to this?
TextInput:
focus: True
pos_hint: {'x': .3, 'top': .8}
size_hint: .4, .05
id: search
multiline:False
Button:
text: 'back'
font_size: 25
size_hint: .3, .15
pos_hint: {'x':.15, 'top': .4}
on_release:
app.root.current = 'main'
root.manager.transition.direction = 'right'
You should use this in your TextInput:
on_text_validate:
app.root.current = 'main'
root.manager.transition.direction = 'right'
For the auto focus thingy, I use sub-classing.
In the .py I create a new class for TextInput and use this instead..
class MyTextInput(TextInput):
""" TextInput with auto-focus
"""
def __init__(self, **kwargs):
super(MyTextInput, self).__init__(**kwargs)
def set_focus(dt):
self.focus = True
Clock.schedule_once(set_focus, .1)
In my kivy project, I have a button allowing me to generate a matplotlib graph in .png format. Generating this image takes time (around 20 seconds), and I would like to display a pop-up window to warn the user.
What i tried :
<MyPopup#Popup>:
auto_dismiss: False
Button:
text: 'This could take time, please wait :) '
on_release: root.dismiss()
and :
ActionButton:
text: 'generate graph'
on_release: Factory.MyPopup().open()
#on_release: root.generate_graph()
Unfortunately, if I uncomment the second "on_release", the pop_up window never appears?
Do you have any guess?
Thank you in advance!
To display a popup while a method is running I use threads. When the popup fires up, the method runs in a thread.
CODE:
def popupThread(self):
#set the popup structure
self.popup = ActivityBox(self)
self.popup.open()
# run the method in threads
t1 = threading.Thread(target = self.someMethod)
t1.start()
The popup is defined in the Builder.load_string():
def build(self):
sm = Builder.load_string("""
<ActivityBox>:
size_hint: 1, .7
auto_dismiss: False
title: 'some activity'
title_align: "center"
title_size: 30
BoxLayout:
orientation: "vertical"
Label:
font_size: '30sp'
text: 'work in progress'
BoxLayout:
orientation: "horizontal"
spacing: 10
size_hint: 1, .5
""")
You were overwriting the on_release method.
ActionButton:
text: 'generate graph'
on_release:
Factory.MyPopup().open()
root.generate_graph()
I'm trying to center the text of a TextInput vertically in Kivy.
But no solution yet.
How can I do a valign for text input in the kv file? Also centering horizontally would be great to know, how to do it.
For labels I have checked the text align example from Kivy and there the alginment is working because there you can use 'valign' and 'halign' to do the alignment, but that's not available for TextInputs.
Maybe a trick with texture_size could help, but I need to check how this works. I have seen such a trick for a label, but I don't know if it works for the TextInput.
Here's my kv code that I have right now:
#: set Buttonheight1 40
BoxLayout:
size_hint_y: None
height: Buttonheight1
Label:
id: _number_label
text: "Number:"
font_size: 10
size_hint_x: None
width: 50
canvas.after:
Color:
rgba: 1,0,0,.5
Rectangle:
pos: self.pos
size: self.size
TextInput:
multiline: False
size_hint_y: None
height: _number_label.height
#padding_top: 10
font_size: 10
text: str(self.font_size)
#text: '%s, %s' % (self.get_center_x(), self.get_center_y()) #position test
Explanation of the kv code:
Buttonheight1 is a constant with kv set
Canvas.after is just for debugging the size of the label
The text of the text input shows font size as dummy text
Maybe it's simple to fix but I'm pretty new to Kivy and haven't found an example for this.
Here is how it looks like at the moment:
(Note: The OK button in the screenshot is not in the kv code above)
Looking at the api, all I could suggest is that you could try using padding, since you can specify:
Padding of the text: [padding_left, padding_top, padding_right, padding_bottom].
padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].
Maybe usingpadding_top and/or padding_bottom for instance you could center the text vertically.
As already suggested in the comments by AWolf.
This seems to work best:
padding: [0, (self.height-self.line_height)/2]
What worked for me was using pos_hint as follows:
TextInput:
multiline: False
size_hint_y: None
height: _number_label.height
font_size: 10
text: str(self.font_size)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
I know this is old post .. I just wanna put the answer if anyone search for this again.
Here is the solution:
In the kivy file add this line in the TextInput Properties
padding : 6,self.height/2 - self.font_size/2,6,6
I used this code in my custom text input.
As you see it works fine: