I've asked in past questions how to add and remove buttons dynamically.
I want to know how to dynamically delete BoxLayout with Python's Kivy.
Here is my code.
#-*- coding: utf-8 -*-
from kivy.config import Config
from kivy.uix.button import Button
Config.set('graphics', 'width', 300)
Config.set('graphics', 'height', 300)
Config.set('input', 'mouse', 'mouse,multitouch_on_demand') # eliminate annoying circle drawing on right click
from kivy.lang import Builder
Builder.load_string("""
<AddItemWidget>:
BoxLayout:
size: root.size
orientation: 'vertical'
RecycleView:
size_hint: 1.0,1.0
BoxLayout:
id: box
orientation: 'vertical'
Button:
id: addButton
text: "Add Item"
on_press: root.buttonClicked()
""")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class RemovableButton(Button):
def on_touch_down(self, touch):
if touch.button == 'right':
if self.collide_point(touch.x, touch.y):
self.parent.remove_widget(self)
return True
return super(RemovableButton, self).on_touch_down(touch)
class AddItemWidget(Widget):
def __init__(self, **kwargs):
super(AddItemWidget, self).__init__(**kwargs)
self.count = 0
def buttonClicked(self):
self.count += 1
boxLayout = BoxLayout()
textinput = TextInput(text='Hello world'+str(self.count),size_hint_x=0.8)
deleteButton = RemovableButton(text='×',size_hint_x=0.2)
boxLayout.add_widget(deleteButton, index=1)
boxLayout.add_widget(textinput, index=1)
self.ids.box.add_widget(boxLayout, index=1)
deleteButton.bind(on_release=boxLayout.remove_widget)
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
def build(self):
return AddItemWidget()
if __name__ == '__main__':
TestApp().run()
When the above code is executed, GUI will be launched and a line will be added by pressing the "Add Item" button.
I want to remove the line, so with the "x" button, as in the image below.
Its a little different than your other example.
For this, you need to use a different function to do the removing.
You also have to import partial like this: from functools import partial
And here is the changed AddItemWidget class:
class AddItemWidget(Widget):
def __init__(self, **kwargs):
super(AddItemWidget, self).__init__(**kwargs)
self.count = 0
def buttonClicked(self):
self.count += 1
boxLayout = BoxLayout()
textinput = TextInput(text='Hello world'+str(self.count),size_hint_x=0.8)
deleteButton = RemovableButton(text='×',size_hint_x=0.2)
boxLayout.add_widget(deleteButton, index=1)
boxLayout.add_widget(textinput, index=1)
self.ids.box.add_widget(boxLayout, index=1)
deleteButton.bind(on_release=partial(self.remove_btn, boxLayout)) # change this
def remove_btn(self, boxLayout, *args): # and add this
self.ids.box.remove_widget(boxLayout)
Related
I want to align the text of the button to the left with halign='left' in Kivy, but it does not work and keeps it centered.
This is my code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
for i in range (0,10):
self.btn = Button(text=str(i),halign='left',background_color =(.3, .6, .7, 1))
self.btn.bind(on_press=self.pressed)
self.add_widget(self.btn)
def pressed(self, instance):
print ("Name:",instance.text)
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
According to the documentation for halign:
This doesn’t change the position of the text texture of the Label
(centered), only the position of the text in this texture. You
probably want to bind the size of the Label to the texture_size or set
a text_size.
So, to get the result you want, you can define an extension to Button that does what the above documentation suggests:
class MyButt(Button):
pass
Builder.load_string('''
<MyButt>:
halign: 'left'
text_size: self.size
background_color: (.3, .6, .7, 1)
''')
Then use that class in your App:
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
for i in range (0,10):
self.btn = MyButt(text=str(i))
self.btn.bind(on_press=self.pressed)
self.add_widget(self.btn)
I am using Kivy to create a GUI.
If I go back to the Kivy operation after I activate another window, the Kivy button operation is ignored once.
This is a video of the operation.
I tried the following code
#-*- coding: utf-8 -*-
from kivy.lang import Builder
Builder.load_string("""
<TextWidget>:
BoxLayout:
orientation: 'vertical'
size: root.size
TextInput:
text: root.text
Button:
id: button1
text: "Test"
font_size: 48
on_press: root.buttonClicked()
""")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
class TextWidget(Widget):
text = StringProperty()
def __init__(self, **kwargs):
super(TextWidget, self).__init__(**kwargs)
self.text = ''
def buttonClicked(self):
self.text += "test\n"
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
self.title = 'greeting'
def build(self):
return TextWidget()
if __name__ == '__main__':
TestApp().run()
Is there a solution to this problem?
I am trying to get a kivy app to open a dropdown. I am following the example here.
When I run the app I can click on the button, but no dropdown appears.
I am missing something simple, but I just can't see it. Can someone help please.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.lang import Builder
root = Builder.load_string('''
<MainFrame>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Hello'
Button:
text: 'open dropdown'
on_press: root.on_menu_button_click()
''')
class MainFrame(Screen):
def __init__(self, **kwargs):
super(MainFrame, self).__init__(**kwargs)
self.dropdown = self._create_dropdown()
def _create_dropdown(self):
dropdown = DropDown()
for index in range(5):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
return dropdown
def on_menu_button_click(self):
self.dropdown.open
class BasicApp(App):
def build(self):
return MainFrame()
if __name__ == '__main__':
BasicApp().run()
You have to use the open() method and pass the button, you must also use on_release instead of on_press.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.lang import Builder
root = Builder.load_string('''
<MainFrame>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Hello'
Button:
id: btn # <---
text: 'open dropdown'
on_release: root.on_menu_button_click(btn) # <---
''')
class MainFrame(Screen):
def __init__(self, **kwargs):
super(MainFrame, self).__init__(**kwargs)
self.dropdown = self._create_dropdown()
def _create_dropdown(self):
dropdown = DropDown()
for index in range(5):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
return dropdown
def on_menu_button_click(self, widget): # <---
self.dropdown.open(widget) # <---
class BasicApp(App):
def build(self):
return MainFrame()
if __name__ == '__main__':
BasicApp().run()
The above is clearly mentioned in the example as it indicates:
...
# show the dropdown menu when the main button is released
# note: all the bind() calls pass the instance of the caller (here, the
# mainbutton instance) as the first argument of the callback (here,
# dropdown.open.).
mainbutton.bind(on_release=dropdown.open)
...
Can someone tell me how to show highlight switch widget when I set focus on Switch?It should be look like focused.
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (200, 150)
class User(Screen):
swtch = ObjectProperty(None)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
Clock.schedule_once(self.swtch_focus, 1)
def swtch_focus(self, *args):
self.swtch.focus = True
class Test(App):
def build(self):
return self.root
if __name__ == '__main__':
Test().run()
test.kv
User:
swtch: swtch
BoxLayout:
orientation: "vertical"
GridLayout:
cols: 2
padding: 20, 20
spacing: 10, 10
Switch:
id:swtch
There is hoverableBehaviour in github here is the link https://gist.github.com/KeyWeeUsr/3166f13d363d5558f481d538c6a5d2aa copy the code and create a module to your current directory and import it to use it which you can add to your Widget to have the on focus or highlight feature. This is how to do.
class Highlight(HoverBehavior,Screen):
def on_enter(self):
print('Focus on')
def on_leave(self):
print('Focus off')
I want to show in a button and a label in a widget at left,center position and right,bottom without using .kv code . Here is my code and i am not able to figure out how to do it Can someone give advice ?
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
class Container(AnchorLayout):
def __init__(self, **kwargs):
super(Container, self).__init__(**kwargs)
btn = Button(text='Hello World',anchor_x='right',anchor_y='bottom')
self.add_widget(btn)
lbl = Label(text="Am i a Label ?",anchor_x='left',anchor_y='center')
self.add_widget(lbl)
class MyJB(App):
def build(self):
parent = Container()
return parent
if __name__ == '__main__':
MyJB().run()
The AnchorLayout aligns all widgets to a given point, not each widget to its own point. If you want widgets anchored in different locations, you need to use multiple AnchorLayouts. You also probably want to specify size and size_hint on either the AnchorLayout or the content widgets.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
class Container(FloatLayout):
def __init__(self, **kwargs):
super(Container, self).__init__(**kwargs)
anchor_rb = AnchorLayout(anchor_x='right', anchor_y='bottom')
btn = Button(text='Hello World', size=(100, 100), size_hint=(None, None))
anchor_rb.add_widget(btn)
self.add_widget(anchor_rb)
anchor_lc = AnchorLayout(anchor_x='left', anchor_y='center')
lbl = Label(text="Am i a Label ?", size=(100, 100), size_hint=(None, None))
anchor_lc.add_widget(lbl)
self.add_widget(anchor_lc)
class MyJB(App):
def build(self):
parent = Container()
return parent
if __name__ == '__main__':
MyJB().run()
Personally, I find kv to be cleaner than manual widget creation, and it helps provide a definite separation between the UI and behavior.
kv version:
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string('''
FloatLayout:
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
Button:
text: 'Hello World'
size: 100, 100
size_hint: None, None
AnchorLayout:
anchor_x: 'left'
anchor_y: 'center'
Label:
text: 'Am i a Label ?'
size: 100, 100
size_hint: None, None
''')
class MyJB(App):
def build(self):
return root
if __name__ == '__main__':
MyJB().run()