00001 from Tkinter import *
00002 from tkMessageBox import *
00003 import datetime
00004 import __builtin__
00005 import threading
00006
00007 def waitInputConfirm(msg):
00008 root = None
00009 try:
00010 root = Tk()
00011 root.withdraw()
00012 ret = askokcancel("waitInputConfirm", msg)
00013 return ret == True
00014 except Exception:
00015 _, e, _ = sys.exc_info()
00016 if "couldn't connect to display" in str(e):
00017 c = raw_input(msg+' (Enter [Y/y] to proceed) ').lower()
00018 return c == 'y' or c == ''
00019 raise
00020 finally:
00021 if root:
00022 root.destroy()
00023 return True
00024
00025 def waitInputSelect(msg):
00026 root = Tk()
00027 root.withdraw()
00028 ret = askyesno("waitInputSelect", msg)
00029 root.destroy()
00030 return ret
00031
00032 class commandPanel:
00033 def __init__(self, frame, wimf, label, cmd, isFirst):
00034 self.wimf = wimf
00035 self.panel = Frame(frame)
00036 self.panel.pack()
00037 self.cmd = cmd
00038 self.isFirst = isFirst
00039 self.bCmd = Button(self.panel, text=label, command=self.processEvent)
00040 self.bCmd.pack(side=LEFT)
00041 self.args = []
00042 self.nstr = cmd.count("#T")
00043 self.nint = cmd.count("#I")
00044 self.ndbl = cmd.count("#D")
00045 if self.nstr:
00046 for i in range(self.nstr):
00047 tf = Text(self.panel, height=1, width=10)
00048 tf.pack(side=LEFT)
00049 self.args.append(tf)
00050 bc = Button(self.panel, text="select", command=self.selectFile)
00051 bc.pack(side=LEFT)
00052 elif self.nint:
00053 for i in range(self.nint):
00054 tf = Text(self.panel, height=1, width=10)
00055 tf.pack(side=LEFT)
00056 self.args.append(tf)
00057 elif self.ndbl:
00058 for i in range(self.ndbl):
00059 tf = Text(self.panel, height=1, width=10)
00060 tf.pack(side=LEFT)
00061 self.args.append(tf)
00062 self.continuous = BooleanVar()
00063 if cmd.count("#continuous"):
00064
00065 self.cbContinuous = CheckButton(self.panel, text="continuous", command=self.toggleContinuous, variable=self.continuous)
00066 self.cbContinuous.setSelected(True)
00067 else:
00068 self.cbContinuous = None
00069 self.continuous.set(False)
00070 return
00071 def toggleContinuous(self):
00072 if self.continuous.get() == True:
00073 self.continuous.set(False)
00074 else:
00075
00076 None
00077 return
00078 def selectFile(self):
00079 jfc = JFileChooser(".")
00080 pf = posFilter()
00081 jfc.addChoosableFileFilter(pf)
00082 ret = jfc.showOpenDialog(None)
00083 if ret == 0:
00084 path = jfc.getSelectedFile().toString()
00085 a = path.split("/")
00086 file = a[len(a)-1]
00087 base = file.split(".")[0]
00088 self.args[0].setText(base)
00089 def processEvent(self):
00090 cmd = self.cmd
00091 if self.nstr:
00092 for i in range(self.nstr):
00093 str = self.args[i].get("1.0",END)
00094 cmd = cmd.replace("#T", str[0:len(str)-1], 1)
00095 elif self.nint:
00096 for i in range(self.nint):
00097 str = self.args[i].get("1.0",END)
00098 cmd = cmd.replace("#I", str[0:len(str)-1], 1)
00099 elif self.ndbl:
00100 for i in range(self.ndbl):
00101 str = self.args[i].get("1.0",END)
00102 cmd = cmd.replace("#D", str[0:len(str)-1], 1)
00103 self.wimf.addHistory(cmd)
00104
00105 exec(cmd)
00106 if self.isFirst and self.wimf.isSequential():
00107 self.wimf.advancePage()
00108 return
00109
00110 class waitInputMenuFrame(Frame):
00111 def __init__(self, menu, master=None):
00112 Frame.__init__(self, master, width=400, height=600)
00113 self.master.title('waitInputMenu')
00114
00115 self.lmenu = menu[1:]
00116 self.page = 0
00117 self.pageExec = 0
00118
00119 panel1 = LabelFrame(self, relief=GROOVE)
00120 panel1.pack()
00121 la = Button(panel1, text='Local', command = self.showLocalFrame)
00122 la.pack(padx = 5, pady = 5, side = LEFT)
00123 lb = Button(panel1, text='Global', command = self.showGlobalFrame)
00124 lb.pack(padx = 5, pady = 5, side = LEFT)
00125 lc = Button(panel1, text='History', command = self.showHistoryFrame)
00126 lc.pack(padx = 5, pady = 5, side = LEFT)
00127
00128 panel2 = Frame(self)
00129 panel2.pack()
00130
00131 self.HistoryPanel = Frame(panel2)
00132 self.taHistory = Text(self.HistoryPanel, height=5)
00133 self.taHistory.pack()
00134
00135 self.localMenuPanel2 = Frame(panel2)
00136 self.localMenuPanel2.pack()
00137 self.localMenuPanel = Frame(self.localMenuPanel2)
00138 self.localMenuPanel.pack()
00139 p = LabelFrame(self.localMenuPanel2)
00140 p.pack()
00141 self.lPage = Label(p)
00142 self.bPrev = Button(p, text="Prev", command=self.prevPage)
00143 self.bPrev.pack(padx=5, pady=5, side=LEFT)
00144 self.bNext = Button(p, text="Next", command=self.nextPage)
00145 self.bNext.pack(padx=5, pady=5, side=LEFT)
00146 self.bSkip = Button(p, text="Skip", command=self.advancePage)
00147 self.bSkip.pack(padx=5, pady=5, side=LEFT)
00148 self.cbSequential = BooleanVar()
00149 cb = Checkbutton(p, text="sequential", command=self.setupPage, variable=self.cbSequential)
00150 cb.pack(padx=5, pady=5, side=LEFT)
00151 self.cbSequential.set(True)
00152 self.setupPage()
00153
00154 gmenu = menu[0]
00155 self.GlobalMenuPanel = Frame(panel2)
00156 bQuit = Button(self.GlobalMenuPanel, text="Quit", command = exit)
00157 bQuit.pack(padx = 5, pady = 5)
00158 bRestart = Button(self.GlobalMenuPanel, text="Restart", command = self.restart)
00159 bRestart.pack(padx = 5, pady = 5)
00160 for i in range(len(gmenu)/2):
00161 label = gmenu[i*2]
00162 content = gmenu[i*2+1]
00163 if content == "#label":
00164 p = Label(self.GlobalMenuPanel, text=label, bd=0)
00165 else:
00166 cp = commandPanel(self.GlobalMenuPanel, self, label, content, False)
00167 p = cp.panel
00168 p.pack()
00169 def restart(self):
00170 self.pageExec = 0
00171 self.page = 0
00172 self.setupPage()
00173 self.showLocalFrame()
00174 return
00175 def isSequential(self):
00176 return self.cbSequential.get()
00177 def setupPage(self):
00178 for k,v in self.localMenuPanel.children.iteritems():
00179 v.pack_forget()
00180 pmenu = self.lmenu[self.page]
00181 for i in range(len(pmenu)/2):
00182 cp = commandPanel(self.localMenuPanel, self, pmenu[i*2], pmenu[i*2+1], i==0)
00183 if self.page != self.pageExec and self.isSequential():
00184 cp.bCmd.configure(state='disabled')
00185 else:
00186 cp.bCmd.configure(state='active')
00187 if cp.continuous.get() and self.page == self.pageExec:
00188 cp.processEvent()
00189 self.lPage.configure(text=str(self.page+1)+"/"+str(len(self.lmenu)))
00190 if self.page == 0:
00191 self.bPrev.configure(state='disabled')
00192 else:
00193 self.bPrev.configure(state='active')
00194 if self.page == len(self.lmenu)-1:
00195 self.bNext.configure(state='disabled')
00196 self.bSkip.configure(state='disabled')
00197 else:
00198 self.bNext.configure(state='active')
00199 self.bSkip.configure(state='active')
00200 return
00201 def advancePage(self):
00202 if self.pageExec < len(self.lmenu)-1:
00203 self.pageExec += 1
00204 self.page += 1
00205 self.setupPage()
00206 elif self.pageExec == len(self.lmenu)-1:
00207 self.showGlobalFrame()
00208 return
00209 def nextPage(self):
00210 self.page += 1
00211 self.setupPage()
00212 return
00213 def prevPage(self):
00214 self.page -= 1
00215 self.setupPage()
00216 return
00217 def addHistory(self,msg):
00218 dstr = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
00219 str = dstr+":"+msg+"\n"
00220 self.taHistory.insert("1.0", str)
00221 return
00222 def showLocalFrame(self):
00223 self.HistoryPanel.pack_forget()
00224 self.GlobalMenuPanel.pack_forget()
00225 self.localMenuPanel2.pack()
00226 return True
00227 def showGlobalFrame(self):
00228 self.localMenuPanel2.pack_forget()
00229 self.HistoryPanel.pack_forget()
00230 self.GlobalMenuPanel.pack()
00231 return True
00232 def showHistoryFrame(self):
00233 self.localMenuPanel2.pack_forget()
00234 self.GlobalMenuPanel.pack_forget()
00235 self.HistoryPanel.pack()
00236 return True
00237
00238 def waitInputMenuMain(menu):
00239 f = waitInputMenuFrame(menu)
00240 f.pack()
00241 f.mainloop()
00242
00243 def waitInputMenu(menu):
00244 thr = threading.Thread(target=waitInputMenuMain, args=(menu,))
00245 thr.start()
00246 return thr
00247
00248 __builtin__.waitInputConfirm = waitInputConfirm
00249 __builtin__.waitInputSelect = waitInputSelect
00250 __builtin__.waitInputMenu = waitInputMenu