(Python Kivy) Indexing Which Button Was Pressed - python

I am trying to figure out a way to index which button was pressed in a GridLayout so that, for instance, I can put a specific image in that button's background when it is pressed. Here is what I am currently doing, using a function just to try to print the index number as a test before adding more functionality:
for x in range(15):
self.buttons.append(Button())
self.ids.grid_1.add_widget(self.buttons[x])
self.buttons[x].background_normal = 'YOUTUBE.png'
self.buttons[x].background_down = 'opacity.png'
# Make the button switch screens to input from calling the function above
if edit_mode is True:
self.buttons[x].bind(on_release=self.SwitchScreenInput)
self.buttons[x].bind(on_release=self.HoldButtonNum(x))
def HoldButtonNum(x):
print(x)
Im getting the error:
TypeError: HoldButtonNum() takes 1 positional argument but 2 were
given
Process finished with exit code 1

I will make some observations:
If HoldButtonNum is an instance method its first parameter must be self.
You must use functools.partial or lambda functions to pass arguments to event handler.
The function must receive a third parameter which is the instance of the button that launches the event.
An example:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from functools import partial
class MyGridLayout(GridLayout):
cols = 5
def __init__(self):
super(MyGridLayout, self).__init__()
self.buttons = []
for x in range(15):
self.buttons.append(Button())
self.add_widget(self.buttons[x])
self.buttons[x].bind(on_release=partial(self.HoldButtonNum, x))
def HoldButtonNum(self, x, instance):
print('Button instance:', instance)
print('Button index in list:', x)
class MyKivyApp(App):
def build(self):
return MyGridLayout()
def main():
app = MyKivyApp()
app.run()
if __name__ == '__main__':
main()
When a button is pressed the output is like:
Button index in list: 1
Button instance: <kivy.uix.button.Button object at 0x0000018C511FC798>

Related

Kivy Buttons on_press method doesn't work

So I have written this basic kivy code. I want to read sentences from a file, and dynamically create buttons for each one of them. Then, I want these buttons to disappear when they are clicked. In a for loop, I create my buttons, and put my buttons with the index i in a list. Then with the on_press method, it should delete itself.
Button_List[i].bind(on_press= lambda x: self.remove_widget(Button_List[i]))
So there is a Button in Button_List[i] , and when it is clicked, it should run:
self.remove_widget(Button_List[i])
so it should delete itself
I have 5 buttons for example, the problem is, that it whichever button I click, it deletes the button with the highest index. And the other buttons dont get deleted. I feel like kivy is only executing the last index, but I am not sure.
Here is my code:
new.py:
import kivy.uix.button as kb
from kivy.app import App
from kivy.uix.widget import Widget
from Nils_Programm_verkürzt import lektionstextlesen
from kivy.uix.gridlayout import GridLayout
sentences = ['example_sentence1','example_sentence2','example_sentence3','example_sentence4','example_sentence5',]
class Button_Widget(Widget):
def __init__(self, **kwargs):
super(Button_Widget, self).__init__(**kwargs)
global Button_List
Button_List = []
for i in range(len(sentences)):
print(i)
Button_List.append(kb.Button(text=sentences[i],pos=(self.width * i, self.height * i)))
Button_List[i].size = 50, 50
print('binding'+ str(i))
Button_List[i].bind(on_press= lambda x: self.remove_widget(Button_List[i]))
print(Button_List[i])
self.add_widget(Button_List[i])
class ButtonApp(App):
def build(self):
return Button_Widget()
if __name__ == "__main__":
ButtonApp().run()
Your help is very much appreciated :).
That's a common problem when using lambda in a loop. They all get the last value of the loop. A fix is to create a new variable that holds the current loop variable value. So, try replacing:
Button_List[i].bind(on_press= lambda x: self.remove_widget(Button_List[i]))
with:
Button_List[i].bind(on_press= lambda x, j=i: self.remove_widget(Button_List[j]))

Kivy Python Scrollview event misfiring when in a class instantiated multiple times

In a Kivy/Python program, I have a class which contains a scrollview. I instantiate this class multiple times. The scrollview is bound to the on_scroll_stop event. My desired outcome is that the on_scroll_stop event will only fire within the instantiated class to which it belongs, but it seems that the event fires across all of the instantiated classes. Am I doing something wrong or is this expected behavior? Working sample below. In this example the "Left" section shows the issue most often, however the error is seen in the right section once you scroll up at the top or scroll down once reaching the bottom. To recreate scroll only one side, preferably the "Left" side. In my actual code which is far more complex the issue is much more prevalent.
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class DisplaySection(Widget):
def CreateSection(self):
self.display_panel_main_layout = BoxLayout(orientation='vertical')
self.elements_layout = FloatLayout(size_hint_y=None)
self.sv = ScrollView()
self.sv.bind(on_scroll_stop=self.on_scrolling_stop)
self.sv.add_widget(self.elements_layout)
self.display_panel_main_layout.add_widget(self.sv)
return self.display_panel_main_layout
def LoadElements(self,section_name):
self.section_name = section_name
number_of_elemements = 100
element_hieght = 40
layout_height = number_of_elemements * element_hieght
self.elements_layout.height = layout_height
xcord = 0
for x in range(number_of_elemements):
ycord = self.elements_layout.height - element_hieght*x
name = Label(text='Name' + str(x),size_hint_y=None, height=40,pos=(xcord,ycord))
self.elements_layout.add_widget(name)
def on_scrolling_stop(self, sv, value):
#print(sv,value)
print('I am',self.section_name)
class testscrollapp(App):
def build(self):
main_layout = BoxLayout()
section_names = ['Left','Right']
for x, section_name in enumerate(section_names):
section_layout = BoxLayout(orientation='vertical')
btn = Button(text=section_name,size_hint=(1,None))
section_layout.add_widget(btn)
# instantiate the class containing the scroll view
scroll_layout = DisplaySection()
scr = scroll_layout.CreateSection()
section_layout.add_widget(scr)
scroll_layout.LoadElements(section_name)
main_layout.add_widget(section_layout)
return main_layout
testscrollapp().run()
The on_scroll_stop event is dispatched just like touch events, so any ScrollView instance that subscribes to on_scroll_stop will get all the on_scroll_stop events. And just like a touch event, the subscribing ScrollView instance must determine which of the events it is interested in, and ignore the rest. Since the value argument to your on_scrolling_stop is the MouseMotionEvent, you can do a collide_point() test to determine if the scroll event is within the ScrollView:
def on_scrolling_stop(self, sv, value):
if sv.collide_point(*value.pos):
print('\tI am',self.section_name)

How to access a button in a for loop and bind it to on_press in kivy

I run into an error while trying to access two buttons from a group of buttons created in for loop and binding the button to on_press. Please what is the right way to do this (without .kv). How can I access individual button and bind them to different on_press event
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class TestApp(App):
def build(self):
layout=GridLayout(cols=1)
for i in range(6):
btn = Button(text= str(i))
layout.add_widget(btn)
# error occurred here
btn[0].bind(on_press=first)
btn[1].bind(on_press=second)
def first(self):
pass
def second(self):
pass
return layout
if __name__ == '__main__':
TestApp().run()
Code involving btn[0] implies a btn list, but there is no such list in your code. Here is a modified version of your code that does what you want:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class TestApp(App):
def build(self):
layout=GridLayout(cols=1)
for i in range(6):
btn = Button(text= str(i))
layout.add_widget(btn)
if i == 0:
btn.bind(on_press=self.first)
elif i == 1:
btn.bind(on_press=self.second)
return layout
def first(self, button_instance):
print('first')
def second(self, button_instance):
print('second')
if __name__ == '__main__':
TestApp().run()
I still haven't fully solved my problem, however, the following code returned the object created on the button and the value assigned to it. So I believe that this solution can help you.
from functools import partial
for x in test:
btn = Button(text=x["CustomerName"], background_color=[0.9, 0.9, 0.9, 0.9], size_hint_y=None, height=40)
btn.bind(on_press=partial(self.mostra,x["CustomerName"]))
self.ids.grd1.add_widget(btn)
def mostra(self,*args):
print(args)
Looking at the code I posted I used bind after creating the button (btn.bind) and in it I passed the function that shows on the screen the value saved in the x of my loop. In the function (mostra) the use of self and * args is mandatory.
At the beginning there is the line (from functools import partial) that line is necessary to execute the function show inside the bind.

Python Kivy: Problem with dynamically assigning widget ids

Im writing an app in Kivy which automaticaly adds Buttons and gives them a unique id using a for loop. This id is then used as a key in the dictionary for a link. So the dictionary works fine and after printing it, it outputs {'button0': 'somewebsite', 'button1': 'other website', 'button2': 'andanotherwebsite'} which is exactly what I want but the button callback function always prints out button2 instead of its own id. Am I assigning the ids wrong? The example below demonstrates my problem.
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivymd.utils import asynckivy
from kivy.clock import Clock
class TestButton(Button):
def callback(self):
print(self.id)
class RootWidget(BoxLayout):
def __init__(self):
super().__init__()
self.links = ["somewebsite", "other website", "andanotherwebsite"]
self.dic_btn_to_lnk = {}
self.size_hint = (None, None)
self.size = ("600dp", "50dp")
Clock.schedule_once(self.add_widgets, 0)
def add_widgets(self, *args):
async def update():
number = 0
for link in self.links:
button = TestButton()
button.text = link
button.size = ("200dp", "50dp")
button.pos_hint = {"center_x": .5}
btn_id = "button" + str(number)
button.id = btn_id
button.bind(on_release=lambda x: button.callback())
number += 1
self.dic_btn_to_lnk[btn_id] = link
self.add_widget(button)
print(self.dic_btn_to_lnk)
asynckivy.start(update())
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
TestApp().run()
The problem is that your on_release binding is calling button.callback(), and button will be the last Button added by the time the on_release is triggered. The solution is to use partial, which freezes its arguments to their values when partial is executed, so the on_release calls the correct button.callback. Like this:
button.bind(on_release=partial(button.callback))
And to simplify the above, the definition of callback is changed to:
class TestButton(Button):
def callback(self, instance):
print(self.id)

Kivy: iterate list with lambda

I want to iterate through a list of button and bind each button with a different function. But the result is always the last Button's function, not all in the list.
Here's my code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
class MyApp(App):
def build(self):
parent = StackLayout()
button_list = []
for i in range(0,11):
button_list.append( Button(text = str(i), size_hint = (None,0.15)) )
print i
for a_button in button_list:
parent.add_widget(a_button)
a_button.bind( on_press = lambda x: parent.add_widget( Label(text = a_button.text) ) )
return parent
if __name__ == "__main__":
MyApp().run()
There is some problem with my lambda function, I guessed.
This is a classic python problem, not actually related to kivy. It is discussed e.g. here.
For those still running into this problem 6 years later, here is my solution:
def buttongenerator(layout):
hellodict = {}
for k in range(50):
hellodict[str(k)] = 'Hello' + str(k)
btn = Button(text=str(k),
size_hint_y=None,
height='40dp')
btn.bind(on_release=(lambda instance, var=hellodict[str(k)]: print(var)))
layout.add_widget(btn)
Layout simply refers to a grid/box layout.
The instance variable is necessary because otherwise the button object will be passed into the lambda function and (in this case) will be printed to the console as a string. The instance variable sort of "absorbs" the button object, if that makes any sense. (The variable does not have to be named instance, it just has to be present)

Categories

Resources