forked from onp/gmcr-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets_f03_01_irreversibleToggles.py
113 lines (79 loc) · 3.58 KB
/
widgets_f03_01_irreversibleToggles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright: (c) Oskar Petersons 2013
"""Widgets for editing the reversibility of options in the conflict.
Loaded by the frame_03_irreversibles module.
"""
from tkinter import *
from tkinter import ttk
from data_01_conflictModel import ConflictModel
class ToggleButton(ttk.Labelframe):
def __init__(self,master,option,*args):
ttk.Frame.__init__(self,master,*args)
self.columnconfigure(0,weight=1)
self.option = option
self.fwdIcon=PhotoImage(file='icons/fwdArrow.gif')
self.backIcon=PhotoImage(file='icons/backArrow.gif')
self.bothIcon=PhotoImage(file='icons/bothArrow.gif')
ttk.Label(self,text=option.name).grid(column=0,row=0,sticky=(N,S,E,W))
ttk.Label(self,text=' N ').grid(column=1,row=0,sticky=(N,S,E,W))
ttk.Label(self,text=' Y ').grid(column=3,row=0,sticky=(N,S,E,W))
self.fwd = ttk.Button(self,image=self.fwdIcon, command=lambda: self.chg('back'))
self.back = ttk.Button(self,image=self.backIcon,command=lambda: self.chg('both'))
self.both = ttk.Button(self,image=self.bothIcon,command=lambda: self.chg('fwd'))
self.both.grid(column=2,row=0,sticky=(N,S,E,W))
self.curr = self.both
self.chg(option.permittedDirection)
def chg(self,new):
self.curr.grid_remove()
if new == 'back':
self.back.grid(column=2,row=0,sticky=(N,S,E,W))
self.curr= self.back
self.option.permittedDirection = 'back'
elif new == 'fwd':
self.fwd.grid(column=2,row=0,sticky=(N,S,E,W))
self.curr = self.fwd
self.option.permittedDirection = 'fwd'
elif new == 'both':
self.both.grid(column=2,row=0,sticky=(N,S,E,W))
self.curr= self.both
self.option.permittedDirection = 'both'
class IrreversibleSetter(Frame):
def __init__(self,master,conflict):
ttk.Frame.__init__(self,master)
self.opts= []
self.conflict = conflict
self.canv = Canvas(self)
self.mainFrame = ttk.Frame(self.canv)
self.scrollY = ttk.Scrollbar(self, orient=VERTICAL,command = self.canv.yview)
self.canvWindow = self.canv.create_window((0,0),window=self.mainFrame,anchor='nw')
self.canv.grid(column=0,row=0,sticky=(N,S,E,W))
self.scrollY.grid(column=1,row=0,sticky=(N,S,E,W))
self.canv.configure(yscrollcommand=self.scrollY.set)
self.rowconfigure(0, weight=1)
self.mainFrame.bind("<Configure>",self.resize)
self.refreshDisplay()
def resize(self,event=None):
self.canv.configure(scrollregion=self.canv.bbox("all"))
self.canv["width"] = self.canv.bbox("all")[2]
def refreshDisplay(self):
self.opts=[]
for chld in self.mainFrame.winfo_children():
chld.destroy()
for dmIdx,dm in enumerate(self.conflict.decisionMakers):
currframe = ttk.LabelFrame(self.mainFrame,text=dm.name)
currframe.grid(column=0,row=dmIdx,sticky=(N,S,E,W))
currframe.columnconfigure(0,weight=1)
for optIdx,opt in enumerate(dm.options):
newtog = ToggleButton(currframe,opt)
newtog.grid(column=0,row=optIdx,sticky=(N,S,E,W))
self.opts.append(newtog)
# ######################
def main():
root = Tk()
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)
g1 = ConflictModel('Prisoners.gmcr')
f1 = IrreversibleSetter(root,g1)
f1.grid(column=0,row=0,sticky=(N,S,E,W))
root.mainloop()
if __name__ == '__main__':
main()