This question has already been answered:

The loop reads the settings.json file with categories. Windows are created, the number of which is equal to the number of dictionaries of the array.

Buttons are created in the inner loop. Moreover, in each window, the buttons are given a specific name from the dictionary.

The problem is that only the last command name is sent to bind - thunar . That is, all buttons cause thunar .

How to fix it?

Program :

 #-*-coding:utf-8-*-# from tkinter import * import json for category in json.load(open('settings.json')): topwindow = Toplevel() stuff = [] for apps in category['apps']: stuff.append(apps['command']) button1 = Button(topwindow,text=apps['name']) button1.bind("<Button-1>",lambda e: print(apps['command'])) button1.pack(fill='both') print("name: {0} command: {1}".format(apps['name'],apps['command'])) topwindow.mainloop() 

settings.json :

  [ { "name": "Офис", "icon-name": "applications-office", "icon-size": "18", "apps": [{ "name": "Libreoffice Writer", "command": "libreoffice --writer" }, { "name": "Libreoffice Calc", "command": "libreoffice --calc" }, { "name":"Libreoffice Impress", "command":"Libreoffice --impress" } ] }, { "name": "Программирование", "icon-name": "applications-development", "icon-size": "18", "apps": [{ "name": "Sublime Text", "command": "subl" }, { "name": "Code::Blocs", "command": "codeblocks" } ] }, { "name": "Видео Аудио", "icon-name": "applications-multimedia", "icon-size": "18", "apps": [{ "name": "VLC медиаплеер", "command": "vlc" }, { "name": "Audacity", "command": "audacity" } ]}, { "name": "Интернет", "icon-name": "applications-internet", "icon-size": "18", "apps": [{ "name": "Интернет браузер", "command": "firefox" }, { "name": "Консольный интернет браузер", "command": "lynx" } ]}, { "name": "Система", "icon-name": "preferences-system", "icon-size": "18", "apps": [{ "name": "XTerm", "command": "xterm" }, { "name": "Настройки \"WP Desktop\"", "command": "wpsettings_gui --root" } ] }, { "name": "Пользователь", "icon-name": "user-home", "icon-size": "18", "apps": [{ "name": "Файловый менеджер", "command": "thunar" } ] } ] 

Reported as a duplicate by jfs python 30 Sep '17 at 8:26

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

In your code, the callback function always referred to the apps variable, which had the value assigned in the last iteration of the loop, which always caused the same value. To solve the problem, you can use the default value of the function argument.

Also, it makes no sense to create a link to the button object, which will be overwritten with each iteration.

 #-*-coding:utf-8-*-# from tkinter import * import json Tk().withdraw() # блокируем отрисовку главного окна for category in json.load(open('settings.json')): topwindow = Toplevel() stuff = [] for apps in category['apps']: stuff.append(apps['command']) cmd = lambda e=apps['command']: print(e) # задаем значение аргумента по умолчанию Button(topwindow, text=apps['name'], command=cmd).pack(fill='both') print("name: {0} command: {1}".format(apps['name'],apps['command'])) topwindow.mainloop()