Python 2.7 では、「エントリ」ウィジェットの状態をチェックボタンのおかげで通常/無効にしたいと考えています。
この質問の助けを借りて、チェックボタンでウィジェットを無効にしますか? 、1つのチェックボタンと1つのエントリでそれを行うことができます
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import Tkinter as tk
root = tk.Tk()
class Principal(tk.Tk):
def __init__(self, *args, **kwargs):
self.foo = tk.StringVar()
self.nac = tk.IntVar()
self.ck1 = tk.Checkbutton(root, text='test',
variable=self.nac, command=self.naccheck)
self.ck1.pack()
self.ent1 = tk.Entry(root, width=20, background='white',
textvariable=self.foo, state='disabled')
self.ent1.pack()
def naccheck(self):
print "check"
if self.nac.get() == 0:
self.ent1.configure(state='disabled')
else:
self.ent1.configure(state='normal')
app = Principal()
root.mainloop()
2つ以上のペア(チェックボタン/エントリ)を持ちたいときに問題が発生します。私の最終的なインターフェースでは、このペアが 20 個以上ある可能性があるため、同じ「naccheck」メソッドを 20 個以上持つことは避けたいと思います。
私はこれを試しました:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import Tkinter as tk
root = tk.Tk()
class Principal(tk.Tk):
def __init__(self, *args, **kwargs):
self.foo = tk.StringVar()
self.nac = {}
self.ent = {}
self.ent["test"] = tk.Entry(root, width=20, background='white', textvariable=self.foo, state='disabled')
self.ent["test"].pack()
self.ent["image"] = tk.Entry(root, width=20, background='white', textvariable=self.foo, state='disabled')
self.ent["image"].pack()
self.nac["test"] = tk.IntVar()
self.ck1 = tk.Checkbutton(root, text='test', variable=self.nac["test"], command=self.naccheck("test"))
self.ck1.pack()
self.nac["image"] = tk.IntVar()
self.ck1 = tk.Checkbutton(root, text='image', variable=self.nac["image"], command=self.naccheck("image"))
self.ck1.pack()
def naccheck(self,item):
print "check "+item
print self.nac[item].get()
if self.nac[item].get() == 0:
self.ent[item].configure(state='disabled')
else:
self.ent[item].configure(state='normal')
app = Principal()
root.mainloop()
残念ながら、このコードを起動すると、チェックボタンごとに「naccheck」メソッドがすぐに呼び出され、いずれかをクリックしても呼び出されません...
私が間違ったことをしましたか?