Buttonbehaviour with buttons generated dynamically by a pyhton list (kivy) [duplicate]Python Lambda in a loopHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?Getting the last element of a listHow to make a flat list out of list of listsHow do I get the number of elements in a list?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?

Typesetting numbers above, below, left, and right of a symbol

Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?

Create two random teams from a list of players

What is my clock telling me to do?

What is the highest achievable score in Catan

How to prevent a single-element caster from being useless against immune foes?

How can I solve this sudoku?

What is this kind of symbol meant to be?

How can you tell the version of Ubuntu on a system in a .sh (bash) script?

Can a US President, after impeachment and removal, be re-elected or re-appointed?

Why does one get the wrong value when printing counters together?

Unknown indication below upper stave

Why are subdominants unstable?

Correct word for a little toy that always stands up?

May a hotel provide accommodation for fewer people than booked?

Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?

How would a lunar colony attack Earth?

What Marvel character has this 'W' symbol?

How can a circuit not have a neutral?

Should students have access to past exams or an exam bank?

How can a class have multiple methods without breaking the single responsibility principle

Is it possible to tell if a child will turn into a Hag?

Antonym of "Megalomania"

Was Donald Trump at ground zero helping out on 9-11?



Buttonbehaviour with buttons generated dynamically by a pyhton list (kivy) [duplicate]


Python Lambda in a loopHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?Getting the last element of a listHow to make a flat list out of list of listsHow do I get the number of elements in a list?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0
















This question already has an answer here:



  • Python Lambda in a loop

    4 answers



I have spend the last few days making an app that I want to implement on a raspberry pi coupled to a 10 inch touchscreen. I am making this app for a student housing association here in Germany.
So the idea is to have the raspberry with the touchscreen installed on/next to/on top of (not sure yet), the common fridge. The fridge only holds drinks in .5L bottles format which cost 0.80€ to 1.10€ each. We don't pay immediately, we just write down our consummations on a list.
What I am trying to do is an app with the kivy library in which I can just click on my name, get a drink and the app would save my consumptions, what I already paid and save these infos in a CSV file. In order to do that I have to generate a number of buttons dynamically (the names of the poeple on the list) with kivy, which I did. But now it seems that I cannot assign them any function when they get pressed. More precisely, the index of each button doesn't seem to have any effect. I don't really know how to explain it so let's look at some code:



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

class grid(App):

button = [0 for i in range(25)]

def build(self):
main = GridLayout(cols = 5)

for i in range(25):
self.button[i] = Button(text = str(i))
self.button[i].bind(on_release =lambda x: grid.printString(str(i)))
main.add_widget(self.button[i])

return main

def printString(string):
print(string)


app = grid()
app.run()


So this isn't my actual code, it's an example that illustrates the problem. The output I get is 24 for each button I click. What I want is that each button prints it's own index in the button[] list. I have been trying to make this work for hours and I haven't found anything that worked. If anyone knows how to assign these button behaviors correctly, I would be really grateful for your input.










share|improve this question














marked as duplicate by eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 26 at 21:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

    – eyllanesc
    Mar 26 at 21:41











  • Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

    – Sébastien Kovacs
    Mar 26 at 21:46











  • Have you read the answers to the duplicate question? There they explain it clearly

    – eyllanesc
    Mar 26 at 21:48











  • You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

    – John Anderson
    Mar 26 at 21:50











  • Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

    – Sébastien Kovacs
    Mar 26 at 21:56

















0
















This question already has an answer here:



  • Python Lambda in a loop

    4 answers



I have spend the last few days making an app that I want to implement on a raspberry pi coupled to a 10 inch touchscreen. I am making this app for a student housing association here in Germany.
So the idea is to have the raspberry with the touchscreen installed on/next to/on top of (not sure yet), the common fridge. The fridge only holds drinks in .5L bottles format which cost 0.80€ to 1.10€ each. We don't pay immediately, we just write down our consummations on a list.
What I am trying to do is an app with the kivy library in which I can just click on my name, get a drink and the app would save my consumptions, what I already paid and save these infos in a CSV file. In order to do that I have to generate a number of buttons dynamically (the names of the poeple on the list) with kivy, which I did. But now it seems that I cannot assign them any function when they get pressed. More precisely, the index of each button doesn't seem to have any effect. I don't really know how to explain it so let's look at some code:



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

class grid(App):

button = [0 for i in range(25)]

def build(self):
main = GridLayout(cols = 5)

for i in range(25):
self.button[i] = Button(text = str(i))
self.button[i].bind(on_release =lambda x: grid.printString(str(i)))
main.add_widget(self.button[i])

return main

def printString(string):
print(string)


app = grid()
app.run()


So this isn't my actual code, it's an example that illustrates the problem. The output I get is 24 for each button I click. What I want is that each button prints it's own index in the button[] list. I have been trying to make this work for hours and I haven't found anything that worked. If anyone knows how to assign these button behaviors correctly, I would be really grateful for your input.










share|improve this question














marked as duplicate by eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 26 at 21:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

    – eyllanesc
    Mar 26 at 21:41











  • Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

    – Sébastien Kovacs
    Mar 26 at 21:46











  • Have you read the answers to the duplicate question? There they explain it clearly

    – eyllanesc
    Mar 26 at 21:48











  • You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

    – John Anderson
    Mar 26 at 21:50











  • Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

    – Sébastien Kovacs
    Mar 26 at 21:56













0












0








0









This question already has an answer here:



  • Python Lambda in a loop

    4 answers



I have spend the last few days making an app that I want to implement on a raspberry pi coupled to a 10 inch touchscreen. I am making this app for a student housing association here in Germany.
So the idea is to have the raspberry with the touchscreen installed on/next to/on top of (not sure yet), the common fridge. The fridge only holds drinks in .5L bottles format which cost 0.80€ to 1.10€ each. We don't pay immediately, we just write down our consummations on a list.
What I am trying to do is an app with the kivy library in which I can just click on my name, get a drink and the app would save my consumptions, what I already paid and save these infos in a CSV file. In order to do that I have to generate a number of buttons dynamically (the names of the poeple on the list) with kivy, which I did. But now it seems that I cannot assign them any function when they get pressed. More precisely, the index of each button doesn't seem to have any effect. I don't really know how to explain it so let's look at some code:



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

class grid(App):

button = [0 for i in range(25)]

def build(self):
main = GridLayout(cols = 5)

for i in range(25):
self.button[i] = Button(text = str(i))
self.button[i].bind(on_release =lambda x: grid.printString(str(i)))
main.add_widget(self.button[i])

return main

def printString(string):
print(string)


app = grid()
app.run()


So this isn't my actual code, it's an example that illustrates the problem. The output I get is 24 for each button I click. What I want is that each button prints it's own index in the button[] list. I have been trying to make this work for hours and I haven't found anything that worked. If anyone knows how to assign these button behaviors correctly, I would be really grateful for your input.










share|improve this question















This question already has an answer here:



  • Python Lambda in a loop

    4 answers



I have spend the last few days making an app that I want to implement on a raspberry pi coupled to a 10 inch touchscreen. I am making this app for a student housing association here in Germany.
So the idea is to have the raspberry with the touchscreen installed on/next to/on top of (not sure yet), the common fridge. The fridge only holds drinks in .5L bottles format which cost 0.80€ to 1.10€ each. We don't pay immediately, we just write down our consummations on a list.
What I am trying to do is an app with the kivy library in which I can just click on my name, get a drink and the app would save my consumptions, what I already paid and save these infos in a CSV file. In order to do that I have to generate a number of buttons dynamically (the names of the poeple on the list) with kivy, which I did. But now it seems that I cannot assign them any function when they get pressed. More precisely, the index of each button doesn't seem to have any effect. I don't really know how to explain it so let's look at some code:



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

class grid(App):

button = [0 for i in range(25)]

def build(self):
main = GridLayout(cols = 5)

for i in range(25):
self.button[i] = Button(text = str(i))
self.button[i].bind(on_release =lambda x: grid.printString(str(i)))
main.add_widget(self.button[i])

return main

def printString(string):
print(string)


app = grid()
app.run()


So this isn't my actual code, it's an example that illustrates the problem. The output I get is 24 for each button I click. What I want is that each button prints it's own index in the button[] list. I have been trying to make this work for hours and I haven't found anything that worked. If anyone knows how to assign these button behaviors correctly, I would be really grateful for your input.





This question already has an answer here:



  • Python Lambda in a loop

    4 answers







python kivy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 21:36









Sébastien KovacsSébastien Kovacs

1




1





marked as duplicate by eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 26 at 21:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











marked as duplicate by eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 26 at 21:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 26 at 21:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

    – eyllanesc
    Mar 26 at 21:41











  • Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

    – Sébastien Kovacs
    Mar 26 at 21:46











  • Have you read the answers to the duplicate question? There they explain it clearly

    – eyllanesc
    Mar 26 at 21:48











  • You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

    – John Anderson
    Mar 26 at 21:50











  • Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

    – Sébastien Kovacs
    Mar 26 at 21:56

















  • change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

    – eyllanesc
    Mar 26 at 21:41











  • Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

    – Sébastien Kovacs
    Mar 26 at 21:46











  • Have you read the answers to the duplicate question? There they explain it clearly

    – eyllanesc
    Mar 26 at 21:48











  • You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

    – John Anderson
    Mar 26 at 21:50











  • Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

    – Sébastien Kovacs
    Mar 26 at 21:56
















change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

– eyllanesc
Mar 26 at 21:41





change to self.button[i].bind(on_release =lambda x, i=i: grid.printString(str(i)))

– eyllanesc
Mar 26 at 21:41













Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

– Sébastien Kovacs
Mar 26 at 21:46





Thanks a lot, that worked great, if it's not too much, could you tell me why it worked? Or maybe point me to some documentation that explains it?

– Sébastien Kovacs
Mar 26 at 21:46













Have you read the answers to the duplicate question? There they explain it clearly

– eyllanesc
Mar 26 at 21:48





Have you read the answers to the duplicate question? There they explain it clearly

– eyllanesc
Mar 26 at 21:48













You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

– John Anderson
Mar 26 at 21:50





You don't need to use a lambda, just use self.button[i] = Button(text = str(i), on_release=self.button_release), and the signature of the button_release can be def button_release(self, button):. The button arg can then be used to get the button text as button.text.

– John Anderson
Mar 26 at 21:50













Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

– Sébastien Kovacs
Mar 26 at 21:56





Yes I read them, I didn't understand them at first. Sorry that I asked again. But it's starting to make sense. Thanks a lot for your help. Also thanks John Anderson, I will try your approach as well.

– Sébastien Kovacs
Mar 26 at 21:56












1 Answer
1






active

oldest

votes


















0














  • Declare a class CustomButton with inheritance of Button widget

  • Use on_touch_down event and check for collision

  • Assign id when creating CustomButton

Example



main.py



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class CustomButton(Button):

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print("Button.id=", self.id)
return True
return False


class grid(App):

def build(self):
main = GridLayout(cols=5)

for i in range(25):
button = CustomButton(id=str(i), text=str(i))
main.add_widget(button)

return main


app = grid()
app.run()





share|improve this answer



























  • I will try, I got a lot more solutions than I expected, so thanks to you all guys.

    – Sébastien Kovacs
    Mar 26 at 21:59














1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














  • Declare a class CustomButton with inheritance of Button widget

  • Use on_touch_down event and check for collision

  • Assign id when creating CustomButton

Example



main.py



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class CustomButton(Button):

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print("Button.id=", self.id)
return True
return False


class grid(App):

def build(self):
main = GridLayout(cols=5)

for i in range(25):
button = CustomButton(id=str(i), text=str(i))
main.add_widget(button)

return main


app = grid()
app.run()





share|improve this answer



























  • I will try, I got a lot more solutions than I expected, so thanks to you all guys.

    – Sébastien Kovacs
    Mar 26 at 21:59















0














  • Declare a class CustomButton with inheritance of Button widget

  • Use on_touch_down event and check for collision

  • Assign id when creating CustomButton

Example



main.py



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class CustomButton(Button):

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print("Button.id=", self.id)
return True
return False


class grid(App):

def build(self):
main = GridLayout(cols=5)

for i in range(25):
button = CustomButton(id=str(i), text=str(i))
main.add_widget(button)

return main


app = grid()
app.run()





share|improve this answer



























  • I will try, I got a lot more solutions than I expected, so thanks to you all guys.

    – Sébastien Kovacs
    Mar 26 at 21:59













0












0








0







  • Declare a class CustomButton with inheritance of Button widget

  • Use on_touch_down event and check for collision

  • Assign id when creating CustomButton

Example



main.py



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class CustomButton(Button):

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print("Button.id=", self.id)
return True
return False


class grid(App):

def build(self):
main = GridLayout(cols=5)

for i in range(25):
button = CustomButton(id=str(i), text=str(i))
main.add_widget(button)

return main


app = grid()
app.run()





share|improve this answer















  • Declare a class CustomButton with inheritance of Button widget

  • Use on_touch_down event and check for collision

  • Assign id when creating CustomButton

Example



main.py



from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class CustomButton(Button):

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print("Button.id=", self.id)
return True
return False


class grid(App):

def build(self):
main = GridLayout(cols=5)

for i in range(25):
button = CustomButton(id=str(i), text=str(i))
main.add_widget(button)

return main


app = grid()
app.run()






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 22:06

























answered Mar 26 at 21:53









ikolimikolim

10.9k2 gold badges11 silver badges24 bronze badges




10.9k2 gold badges11 silver badges24 bronze badges















  • I will try, I got a lot more solutions than I expected, so thanks to you all guys.

    – Sébastien Kovacs
    Mar 26 at 21:59

















  • I will try, I got a lot more solutions than I expected, so thanks to you all guys.

    – Sébastien Kovacs
    Mar 26 at 21:59
















I will try, I got a lot more solutions than I expected, so thanks to you all guys.

– Sébastien Kovacs
Mar 26 at 21:59





I will try, I got a lot more solutions than I expected, so thanks to you all guys.

– Sébastien Kovacs
Mar 26 at 21:59








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현