Created
May 7, 2017 15:11
-
-
Save tiwo/72541c4ec173c64ac7c4b498e42ebc16 to your computer and use it in GitHub Desktop.
trying to reproduce E.Zag's issue with λ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import tkinter as tk" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"See [E.Zag's question on stackoverflow](http://stackoverflow.com/questions/43831296/how-to-assign-unique-intvar-s-to-a-large-set-of-checkbuttons-in-tkinter/43832036#comment74702106_43832036); I'm trying to reproduce their issue.\n", | |
"\n", | |
"Here's their code, unaltered except for the default argument in the λ to bind to var (`command=lambda v=var: ...`):" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class application():\n", | |
"\n", | |
" def __init__(self):\n", | |
" self.win = tk.Tk()\n", | |
" self.createWidgets()\n", | |
"\n", | |
" def createWidgets(self):\n", | |
" a=[]\n", | |
" b=[]\n", | |
" c=[1 for i in range(7)]\n", | |
" k=1\n", | |
" for index, item in enumerate(c):\n", | |
" var=tk.IntVar()\n", | |
" a.append(var)\n", | |
" check=tk.Checkbutton(self.win, text='Button %d' %(k), command=lambda v=var: self.a(v.get()), variable=var)\n", | |
" b.append(check)\n", | |
" b[index].grid(row=k)\n", | |
" k=k+1\n", | |
"\n", | |
" def a(self, var):\n", | |
" print(var)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"1\n", | |
"1\n", | |
"1\n", | |
"1\n", | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"if __name__ == \"__main__\":\n", | |
" app = application()\n", | |
" app.win.mainloop()\n", | |
"\n", | |
"# another window appears.\n", | |
"# Clicking the buttons in sequence, then close that window:" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"All these ones are making me suspicious. I want some indication of what happens:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class verbose_application():\n", | |
"\n", | |
" def __init__(self):\n", | |
" self.win = tk.Tk()\n", | |
" self.createWidgets()\n", | |
"\n", | |
" def createWidgets(self):\n", | |
" a=[]\n", | |
" b=[]\n", | |
" c=[1 for i in range(7)]\n", | |
" k=1\n", | |
" for index, item in enumerate(c):\n", | |
" var=tk.IntVar()\n", | |
" a.append(var)\n", | |
" check=tk.Checkbutton(self.win, text='Button %d' %(k), command=lambda v=var: self.a(v.get()), variable=var,\n", | |
" onvalue=k, offvalue=-k) # <-- this is new\n", | |
" b.append(check)\n", | |
" b[index].grid(row=k)\n", | |
" k=k+1\n", | |
"\n", | |
" def a(self, var):\n", | |
" print(var)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"-1\n", | |
"-2\n", | |
"-3\n", | |
"-4\n", | |
"-5\n", | |
"-6\n", | |
"-7\n" | |
] | |
} | |
], | |
"source": [ | |
"if __name__ == \"__main__\":\n", | |
" app = verbose_application()\n", | |
" app.win.mainloop()\n", | |
"\n", | |
"# another window appears.\n", | |
"# Clicking the buttons in sequence, and then clicking again to unselect:" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment