Weil ich gerade mal Lust hatte zu zeigen, wie man sowas eigentlich macht habe ich mal dieses kleine Testprogramm geschrieben.
Tkinter ist zwar nicht das gelbe vom Ei, aber etwas das es gut macht und das man verwenden sollte sind die Var-Klassen. Damit kann man sauber Daten von GUI trennen, und sich reziprok von Aenderungen benachrichtigen lassen. Und bei Checkboxen insbesondere ist das auch das vorgeschlagene Vorgehen.
Code: Alles auswählen
import math
import tkinter as tk
import json
import tempfile
from functools import partial
from itertools import cycle
TEST_DATA = [
[
1, 1, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1,
0, 1, 0, 1
],
[
1, 0, 1, 1,
0, 1, 0, 1,
1, 0, 1, 1,
0, 1, 0, 1,
0, 0, 1, 1,
0, 1, 1, 0
],
]
class CircleView(object):
def __init__(self, parent, model, width=400, height=400):
frame = tk.Frame(parent)
frame.config(width=width, height=height)
frame.pack()
for i, var in enumerate(model):
step = math.pi * 2 / len(model) * i
x = math.cos(step) * (width / 2) * .8 + width / 2
y = math.sin(step) * (width / 2) * .8 + width / 2
cb = tk.Checkbutton(frame, var=var)
cb.place(x=x, y=y)
class CircleModel(object):
def __init__(self):
self._vars = [tk.IntVar() for _ in range(24)]
def __iter__(self):
return iter(self._vars)
def __len__(self):
return len(self._vars)
def load(self, path):
with open(path) as inf:
data = json.load(inf)
for var, value in zip(self._vars, data):
var.set(value)
def save(self, path):
with open(path, "w") as outf:
json.dump([var.get() for var in self._vars], outf)
def write_test_data():
res = []
for data in TEST_DATA:
f = tempfile.mktemp()
with open(f, "w") as outf:
json.dump(data, outf)
res.append(f)
return res
def swap_test_data(filenames, model, root):
model.load(next(filenames))
f = partial(swap_test_data, filenames, model, root)
root.after(2000, lambda: f())
def main():
test_data_files = write_test_data()
root = tk.Tk()
model = CircleModel()
v = CircleView(root, model)
f = partial(swap_test_data, cycle(test_data_files), model, root)
root.after(2000, lambda: f())
b = tk.Button(root, text="Save", command=lambda: model.save("/tmp/test.json"))
b.pack()
tk.mainloop()
if __name__ == '__main__':
main()