textvariableに同じ値を指定しているからだと思います。
普通textvariableにはtkiner.StringVarのインスタンスを指定し、後からsetすることでラベルの表示テキストを変更するなんて場合に使うと思います。
変数の使い方については以下を参照しました。
https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html
Label
You can set its textvariable option to a StringVar. Then any call to the variable's .set() method will change the text displayed on the label. This is not necessary if the label's text is static; use the text attribute for labels that don't change while the application is running.
1はStringVarのインスタンスではないのですが、動きを見る限り、こんなふうに動いているように思えました(仕様書からこの動作を見つけられなかったので単なる想像です)
(A) 1がtextvariableに初めて指定されたとき
(A1) 初期テキストはtextオプションで決まる
(A2) 1に対応する内部的なStringVar(のようなもの?)が生成される
その値は(A1)の内容になる
(B) 他のラベルのtextvariableに1が指定されたとき
(B1) textオプションは無視される
(B2) 初期テキストは(A2)で生成されたStringVarの内容となる
追記:質問者さんがやりたいことが今一つピンときてはいないのですが、ウィジェットの標準的なプロパティとは別にアプリケーションで固有の情報をウィジェットに結び付けておきたいと解釈しました。どうするのがベストか自信ないですが、以下の例では「userOwnValue」という名前でアプリケーション固有情報をウィジェットの属性として直接設定してしまう例です。tkinter.Labelの派生クラスを定義してその中に__value
のようなtkinterと絶対に衝突しない名前を定義すると安全とは思いますがそこまで厳密にすべきかどうか自分にはわかりませんでした。
python
1import tkinter as tk
2
3
4root = tk.Tk()
5size = 40
6
7labelB = None
8
9
10def on_click_common(event):
11 print('text of clicked label =', event.widget['text'])
12 print('userOwnValue of the label =', event.widget.userOwnValue)
13
14
15def on_click_a(event):
16 global labelB
17 if labelB is None:
18 # まだlabelBを生成してない場合のみ生成する
19 labelB = tk.Label(root, text="×", font=size)
20 labelB.userOwnValue = 2
21 labelB.place(x=size)
22 labelB.bind("<1>", on_click_b)
23 on_click_common(event)
24
25
26def on_click_b(event):
27 on_click_common(event)
28
29
30labelA = tk.Label(root, text="○", font=size)
31labelA.userOwnValue = 1
32labelA.place(x=0)
33labelA.bind("<1>", on_click_a)
34
35
36tk.mainloop()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/12 01:40
2018/05/12 01:48
2018/05/12 06:06
2018/05/12 06:23 編集
2018/05/12 06:50
2018/05/12 07:39
2018/05/12 11:31