python/waitInput.py
Go to the documentation of this file.
1 from Tkinter import *
2 from tkMessageBox import *
3 import datetime
4 import __builtin__
5 import threading
6 
7 def waitInputConfirm(msg):
8  root = None
9  try:
10  root = Tk()
11  root.withdraw()
12  ret = askokcancel("waitInputConfirm", msg)
13  return ret == True
14  except Exception:
15  _, e, _ = sys.exc_info()
16  if "couldn't connect to display" in str(e):
17  c = raw_input(msg+' (Enter [Y/y] to proceed) ').lower()
18  return c == 'y' or c == ''
19  raise
20  finally:
21  if root:
22  root.destroy()
23  return True
24 
25 def waitInputSelect(msg):
26  root = Tk()
27  root.withdraw()
28  ret = askyesno("waitInputSelect", msg)
29  root.destroy()
30  return ret
31 
33  def __init__(self, frame, wimf, label, cmd, isFirst):
34  self.wimf = wimf
35  self.panel = Frame(frame)
36  self.panel.pack()
37  self.cmd = cmd
38  self.isFirst = isFirst
39  self.bCmd = Button(self.panel, text=label, command=self.processEvent)
40  self.bCmd.pack(side=LEFT)
41  self.args = []
42  self.nstr = cmd.count("#T")
43  self.nint = cmd.count("#I")
44  self.ndbl = cmd.count("#D")
45  if self.nstr:
46  for i in range(self.nstr):
47  tf = Text(self.panel, height=1, width=10)
48  tf.pack(side=LEFT)
49  self.args.append(tf)
50  bc = Button(self.panel, text="select", command=self.selectFile)
51  bc.pack(side=LEFT)
52  elif self.nint:
53  for i in range(self.nint):
54  tf = Text(self.panel, height=1, width=10)
55  tf.pack(side=LEFT)
56  self.args.append(tf)
57  elif self.ndbl:
58  for i in range(self.ndbl):
59  tf = Text(self.panel, height=1, width=10)
60  tf.pack(side=LEFT)
61  self.args.append(tf)
62  self.continuous = BooleanVar()
63  if cmd.count("#continuous"):
64  #self.continuous.set(True)
65  self.cbContinuous = CheckButton(self.panel, text="continuous", command=self.toggleContinuous, variable=self.continuous)
66  self.cbContinuous.setSelected(True)
67  else:
68  self.cbContinuous = None
69  self.continuous.set(False)
70  return
71  def toggleContinuous(self):
72  if self.continuous.get() == True:
73  self.continuous.set(False)
74  else:
75  #self.continuous.set(True)
76  None
77  return
78  def selectFile(self):
79  jfc = JFileChooser(".")
80  pf = posFilter()
81  jfc.addChoosableFileFilter(pf)
82  ret = jfc.showOpenDialog(None)
83  if ret == 0:
84  path = jfc.getSelectedFile().toString()
85  a = path.split("/")
86  file = a[len(a)-1]
87  base = file.split(".")[0]
88  self.args[0].setText(base)
89  def processEvent(self):
90  cmd = self.cmd
91  if self.nstr:
92  for i in range(self.nstr):
93  str = self.args[i].get("1.0",END)
94  cmd = cmd.replace("#T", str[0:len(str)-1], 1)
95  elif self.nint:
96  for i in range(self.nint):
97  str = self.args[i].get("1.0",END)
98  cmd = cmd.replace("#I", str[0:len(str)-1], 1)
99  elif self.ndbl:
100  for i in range(self.ndbl):
101  str = self.args[i].get("1.0",END)
102  cmd = cmd.replace("#D", str[0:len(str)-1], 1)
103  self.wimf.addHistory(cmd)
104  #print "cmd=",cmd
105  exec(cmd)
106  if self.isFirst and self.wimf.isSequential():
107  self.wimf.advancePage()
108  return
109 
110 class waitInputMenuFrame(Frame):
111  def __init__(self, menu, master=None):
112  Frame.__init__(self, master, width=400, height=600)
113  self.master.title('waitInputMenu')
114 
115  self.lmenu = menu[1:]
116  self.page = 0
117  self.pageExec = 0
118  # panel for tab buttons
119  panel1 = LabelFrame(self, relief=GROOVE)
120  panel1.pack()
121  la = Button(panel1, text='Local', command = self.showLocalFrame)
122  la.pack(padx = 5, pady = 5, side = LEFT)
123  lb = Button(panel1, text='Global', command = self.showGlobalFrame)
124  lb.pack(padx = 5, pady = 5, side = LEFT)
125  lc = Button(panel1, text='History', command = self.showHistoryFrame)
126  lc.pack(padx = 5, pady = 5, side = LEFT)
127  #
128  panel2 = Frame(self)
129  panel2.pack()
130  # history panel
131  self.HistoryPanel = Frame(panel2)
132  self.taHistory = Text(self.HistoryPanel, height=5)
133  self.taHistory.pack()
134  # local menu panel
135  self.localMenuPanel2 = Frame(panel2)
136  self.localMenuPanel2.pack()
137  self.localMenuPanel = Frame(self.localMenuPanel2)
138  self.localMenuPanel.pack()
139  p = LabelFrame(self.localMenuPanel2)
140  p.pack()
141  self.lPage = Label(p)
142  self.bPrev = Button(p, text="Prev", command=self.prevPage)
143  self.bPrev.pack(padx=5, pady=5, side=LEFT)
144  self.bNext = Button(p, text="Next", command=self.nextPage)
145  self.bNext.pack(padx=5, pady=5, side=LEFT)
146  self.bSkip = Button(p, text="Skip", command=self.advancePage)
147  self.bSkip.pack(padx=5, pady=5, side=LEFT)
148  self.cbSequential = BooleanVar()
149  cb = Checkbutton(p, text="sequential", command=self.setupPage, variable=self.cbSequential)
150  cb.pack(padx=5, pady=5, side=LEFT)
151  self.cbSequential.set(True)
152  self.setupPage()
153  # global menu panel
154  gmenu = menu[0]
155  self.GlobalMenuPanel = Frame(panel2)
156  bQuit = Button(self.GlobalMenuPanel, text="Quit", command = exit)
157  bQuit.pack(padx = 5, pady = 5)
158  bRestart = Button(self.GlobalMenuPanel, text="Restart", command = self.restart)
159  bRestart.pack(padx = 5, pady = 5)
160  for i in range(len(gmenu)/2):
161  label = gmenu[i*2]
162  content = gmenu[i*2+1]
163  if content == "#label":
164  p = Label(self.GlobalMenuPanel, text=label, bd=0)
165  else:
166  cp = commandPanel(self.GlobalMenuPanel, self, label, content, False)
167  p = cp.panel
168  p.pack()
169  def restart(self):
170  self.pageExec = 0
171  self.page = 0
172  self.setupPage()
173  self.showLocalFrame()
174  return
175  def isSequential(self):
176  return self.cbSequential.get()
177  def setupPage(self):
178  for k,v in self.localMenuPanel.children.iteritems():
179  v.pack_forget()
180  pmenu = self.lmenu[self.page]
181  for i in range(len(pmenu)/2):
182  cp = commandPanel(self.localMenuPanel, self, pmenu[i*2], pmenu[i*2+1], i==0)
183  if self.page != self.pageExec and self.isSequential():
184  cp.bCmd.configure(state='disabled')
185  else:
186  cp.bCmd.configure(state='active')
187  if cp.continuous.get() and self.page == self.pageExec:
188  cp.processEvent()
189  self.lPage.configure(text=str(self.page+1)+"/"+str(len(self.lmenu)))
190  if self.page == 0:
191  self.bPrev.configure(state='disabled')
192  else:
193  self.bPrev.configure(state='active')
194  if self.page == len(self.lmenu)-1:
195  self.bNext.configure(state='disabled')
196  self.bSkip.configure(state='disabled')
197  else:
198  self.bNext.configure(state='active')
199  self.bSkip.configure(state='active')
200  return
201  def advancePage(self):
202  if self.pageExec < len(self.lmenu)-1:
203  self.pageExec += 1
204  self.page += 1
205  self.setupPage()
206  elif self.pageExec == len(self.lmenu)-1:
207  self.showGlobalFrame()
208  return
209  def nextPage(self):
210  self.page += 1
211  self.setupPage()
212  return
213  def prevPage(self):
214  self.page -= 1
215  self.setupPage()
216  return
217  def addHistory(self,msg):
218  dstr = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
219  str = dstr+":"+msg+"\n"
220  self.taHistory.insert("1.0", str)
221  return
222  def showLocalFrame(self):
223  self.HistoryPanel.pack_forget()
224  self.GlobalMenuPanel.pack_forget()
225  self.localMenuPanel2.pack()
226  return True
227  def showGlobalFrame(self):
228  self.localMenuPanel2.pack_forget()
229  self.HistoryPanel.pack_forget()
230  self.GlobalMenuPanel.pack()
231  return True
232  def showHistoryFrame(self):
233  self.localMenuPanel2.pack_forget()
234  self.GlobalMenuPanel.pack_forget()
235  self.HistoryPanel.pack()
236  return True
237 
239  f = waitInputMenuFrame(menu)
240  f.pack()
241  f.mainloop()
242 
243 def waitInputMenu(menu):
244  thr = threading.Thread(target=waitInputMenuMain, args=(menu,))
245  thr.start()
246  return thr
247 
248 __builtin__.waitInputConfirm = waitInputConfirm
249 __builtin__.waitInputSelect = waitInputSelect
250 __builtin__.waitInputMenu = waitInputMenu
def waitInputSelect(msg)
def waitInputMenuMain(menu)
def waitInputConfirm(msg)
def __init__(self, menu, master=None)
def __init__(self, frame, wimf, label, cmd, isFirst)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51