You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIViewTKinter.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. '''--------------------------------------------------------------------------------------------------
  2. Die View bildet die Oberflaeche ab.
  3. Sie ist in 3 Sektoren Top, Main und ButtonFrame unterteilt. Der TopFrame beihnahltet die Taskleiste,
  4. der Button Frame gibt Informationen raus. Im Main Frame sind die Buttons yu starten und stoppen der
  5. Buchstabierungen. HIerfuer sind extra Seiten erstellt, die beliebig, je nach Situation ausgetauscht
  6. werden koennen
  7. '''
  8. try:
  9. from Tkinter import * #Fuer Python 2.7
  10. import tkFileDialog as fd
  11. except ImportError: #Fuer Python >3
  12. from tkinter import *
  13. from tkinter import filedialog as fd
  14. from UIController import *
  15. class View(Tk):
  16. def __init__(self, c, path_default, *args, **kwargs):
  17. '''--------------------------------------------------------------------------------------------------
  18. In der Init wird das Lazout gesetyt und die Funktionen yur erstellung der Frames aufgerufen,
  19. zudem wird dem schliesen des Fensters sowie den anpassen der Groesse andere Funktionen hinterlegt
  20. '''
  21. Tk.__init__(self, *args, **kwargs)
  22. self.controller = c
  23. self.PATH_DEFAULT = path_default
  24. self.title("Visuelles Buchstabieren")
  25. self.h = 1000
  26. self.w = 2000
  27. self.geometry('{}x{}'.format(self.w,self.h))
  28. self.faktor = [(0.073),(0.727), (0.2)]
  29. #self.resizable(height=False, width= False)
  30. self.layout = {
  31. "background": "#00001E",
  32. "backgroundBtn": "#1C86EE",
  33. "fontColor": "#FFFFFF",
  34. "backgroundBar": "#00002E",
  35. "font": ('Calibri', 20, 'bold'),
  36. "fontSmall": ('Calibri', 14, 'bold'),
  37. "fontInfo": ('Calibri', 25)
  38. }
  39. self.createTopFrame()
  40. self.createMainFrame()
  41. self.createBottomFrame()
  42. self.bind("<Configure>", self.adjustSize)
  43. self.protocol("WM_DELETE_WINDOW", self.onClosing)
  44. def changeFrame(self, pageName):
  45. '''--------------------------------------------------------------------------------------------------
  46. Setzt dem Frame passend zum pageName in den Vordergrund
  47. '''
  48. frame = self.frames[pageName]
  49. frame.tkraise()
  50. def adjustSize(self, event):
  51. '''--------------------------------------------------------------------------------------------------
  52. passt nach Faktoren die einyelnen Seitenteile der jeweiligen Frame Groesse an
  53. '''
  54. if(str(event.widget) =="."):
  55. self.h = event.height
  56. self.w = event.width
  57. self.topFrame.configure(height=(self.h*self.faktor[0]), width=self.w)
  58. self.mainFrame.configure(height=(self.h*self.faktor[1]), width=self.w)
  59. self.bottomFrame.configure(height=(self.h*self.faktor[2]), width=self.w)
  60. self.container.configure(height=(self.h*self.faktor[1]), width=self.w)
  61. for f in self.frames:
  62. self.frames[f].configure(height=(self.h*self.faktor[1]), width=self.w)
  63. self.frames[f].adjustSize(height=(self.h*self.faktor[1]), width=self.w)
  64. def onClosing(self):
  65. '''--------------------------------------------------------------------------------------------------
  66. Stoppt alle laufenden Threads und schliesst OpenVibe und den Aquisitionserver bevor sich das Fenster
  67. schliesst
  68. '''
  69. print("closing")
  70. self.controller.commandStop()
  71. self.controller.stopAcquisitionServer()
  72. print("destroy")
  73. try:
  74. self.dialog.destroy()
  75. except:
  76. pass
  77. self.destroy()
  78. def createTopFrame(self):
  79. '''--------------------------------------------------------------------------------------------------
  80. Erstellt die Buttons und Textfelder fuer die Taskleiste und positionier diese im Frame
  81. '''
  82. self.topFrame = Frame(self, bg=self.layout["backgroundBar"], height=50, width=500)
  83. self.changeBtn = Button(self.topFrame, text="Wechsel zu taktilen BCI", command=lambda: self.controller.actionPerformed("wechsel"), height=3, width = 30, font=self.layout["fontSmall"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  84. self.saveBtn = Button(self.topFrame, text="Auswaehlen der Datei", command=lambda: self.controller.actionPerformed("speicherort"), height=3, width = 30, font=self.layout["fontSmall"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  85. #self.setBtn = Button(self.topFrame, text="Auswaehlen der Datei", command=lambda: self.controller.actionPerformed("setFile"), height=2, width = 25, font=self.layout["fontSmall"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  86. self.toplabel = Label(self.topFrame, text="visuelles BCI", font=self.layout["font"], bg=self.layout["backgroundBar"], fg=self.layout["fontColor"], )
  87. self.topFrame.grid(column=0, row=0)
  88. self.topFrame.grid_propagate(0)
  89. self.topFrame.pack_propagate(0)
  90. self.changeBtn.grid(column=0, row=0, padx=10,pady=5)
  91. self.saveBtn.grid(column=1, row=0, padx=10, pady=5)
  92. #self.setBtn.grid(column=2, row=0, padx=10, pady=5)
  93. self.toplabel.grid(column=2, row=0, padx=100, pady=2)
  94. def createMainFrame(self):
  95. '''--------------------------------------------------------------------------------------------------
  96. Erstellt und postioniert einen Container als Platzhalter fuer die verschiedenen Seiten. Anscliessend
  97. erstellt es die verschiedenen Seiten und speicher diese in eine Liste
  98. '''
  99. self.mainFrame = Frame(self, bg=self.layout["background"], height=250, width=500)
  100. self.mainFrame.grid(column=0, row=1)
  101. self.mainFrame.pack_propagate(0)
  102. self.container = Frame(self.mainFrame, bg=self.layout["background"], height=250, width=500)
  103. self.container.pack(side="top", fill="both", expand = True)
  104. self.container.pack_propagate(0)
  105. self.container.grid_propagate(0)
  106. self.frames = {}
  107. for F in (StartPage, WorkingPageTaktil, WorkingPageVisuell):
  108. page_name = F.__name__
  109. frame = F(parent=self.container, controller=self.controller, layout=self.layout)
  110. self.frames[page_name] = frame
  111. frame.grid(row=0, column=0, sticky="nsew")
  112. self.changeFrame("StartPage")
  113. def createBottomFrame(self):
  114. '''--------------------------------------------------------------------------------------------------
  115. Erstellt und positioniert ein Label fuer die ganzen Informationen
  116. '''
  117. self.bottomFrame = Frame(self, bg=self.layout["backgroundBar"], height=100, width=500)
  118. self.bottomlabel = Label(self.bottomFrame, text="Hier stehen Infos\nGanz viele", justify='left', font=self.layout["fontInfo"], bg=self.layout["backgroundBar"], fg=self.layout["fontColor"])
  119. self.bottomFrame.grid(column=0, row=2)
  120. self.bottomFrame.pack_propagate(0)
  121. self.bottomFrame.grid_propagate(0)
  122. self.bottomlabel.grid(padx=5,pady=5)
  123. def setInfoText(self, text):
  124. self.bottomlabel['text'] = text
  125. def setTitleText(self,text):
  126. self.title(text)
  127. self.toplabel["text"] = text
  128. def setChangeBtnText(self,text):
  129. self.changeBtn["text"] = text
  130. def setDefaultPath(self, path):
  131. self.PATH_DEFAULT = path
  132. def openfiledialog(self):
  133. path = self.PATH_DEFAULT
  134. #file = fd.askopenfilename(initialdir=path)
  135. file = fd.askdirectory(initialdir=path)
  136. return file
  137. def savefiledialog(self):
  138. self.dialog = Tk()
  139. self.dialog.title("Visuelles Buchstabieren")
  140. h = 100
  141. w = 400
  142. self.dialog.geometry('{}x{}'.format(w,h))
  143. self.dialog.geometry("+{}+{}".format(100,200))
  144. l = Label(self.dialog, text="Bitte geben Sie einen Speichernamen ein:")
  145. eingabefeld = Entry(self.dialog)
  146. okBtn = Button(self.dialog, text="OK", command=lambda:self.okBtn(eingabefeld.get()))
  147. l.pack()
  148. eingabefeld.pack()
  149. okBtn.pack()
  150. self.dialog.protocol("WM_DELETE_WINDOW", self.closeDialog)
  151. self.dialog.mainloop()
  152. def okBtn(self, text):
  153. path = self.PATH_DEFAULT + "/" + text
  154. print(path)
  155. self.controller.copyspelling(path)
  156. self.dialog.destroy()
  157. def closeDialog(self):
  158. self.controller.copyspelling("-1")
  159. self.dialog.destroy()
  160. def getWindowSize(self):
  161. return [self.winfo_x(), self.winfo_y(), self.winfo_height(), self.winfo_width()]
  162. class StartPage(Frame):
  163. '''--------------------------------------------------------------------------------------------------
  164. Bildet die Seite in der die Aktionen ausgesucht werden koennen. Beihaltet 2 Buttons einen zum starten
  165. des freien Buchstabierens und einen yum starten des gefuerten Buchstabierens
  166. '''
  167. def __init__(self, parent, controller, layout):
  168. self.layout = layout
  169. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  170. self.grid_propagate(0)
  171. self.pack_propagate(0)
  172. self.controller = controller
  173. self.container = Frame(self, bg=self.layout["background"], height=250, width=500)
  174. self.btnFrame = Frame(self.container, bg=self.layout["background"], height=250, width=250)
  175. self.container.columnconfigure(0, weight=1) # Set weight to row and
  176. self.container.rowconfigure(0, weight=1) # column where the widget is
  177. self.container.grid_propagate(0)
  178. self.container.pack_propagate(0)
  179. self.container.pack()
  180. self.btnFrame.grid()
  181. testBtn = Button(self.btnFrame, text="gefuehrtes Buchstabieren", command=lambda: self.controller.actionPerformed("copySpelling"), height=6, width = 20, font=self.layout["font"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  182. #testBtn = Button(self.btnFrame, text="copySpelling", command=lambda: self.controller.actionPerformed("test"), height=6, width = 20, font=self.layout["font"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  183. testBtn.grid(row=0,column=0, padx=25,pady=25)
  184. freeSpellingBtn = Button(self.btnFrame, text="freies Buchstabieren", command=lambda: self.controller.actionPerformed("freeSpelling"), height=6, width = 20, font=self.layout["font"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  185. freeSpellingBtn.grid(row=0, column=1, padx=25,pady=25)
  186. def adjustSize(self, height, width):
  187. self.container.configure(height=height, width= width)
  188. self.btnFrame.configure(height=(height*4)/5, width= width/2)
  189. class WorkingPageTaktil(Frame):
  190. '''--------------------------------------------------------------------------------------------------
  191. Bildet die Seite nach Auswahl einer Aktion. Beihaltet einen mittig plazierten Buttons zum stoppen
  192. des ablaufenden Vorgang
  193. '''
  194. def __init__(self, parent, controller, layout):
  195. self.layout = layout
  196. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  197. self.controller = controller
  198. self.columnconfigure(0, weight=1) # Set weight to row and
  199. self.rowconfigure(0, weight=1) # column where the widget is
  200. stopBtn = Button(self, text="stop", command=lambda: self.controller.actionPerformed("stop"), height=4, width = 15, font=self.layout["font"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  201. stopBtn.grid()
  202. def adjustSize(self, height, width):
  203. pass
  204. class WorkingPageVisuell(Frame):
  205. '''--------------------------------------------------------------------------------------------------
  206. Bildet die Seite nach Auswahl einer Aktion. Beihaltet einen mittig unten plazierten Buttons zum
  207. stoppen des ablaufenden Vorgang
  208. '''
  209. def __init__(self, parent, controller, layout):
  210. self.layout = layout
  211. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  212. self.controller = controller
  213. self.grid_propagate(0)
  214. self.pack_propagate(0)
  215. self.windowFrame = Frame(self, height=200, width=500, bg=self.layout["background"])
  216. self.windowFrame.grid(column=0, row=0)
  217. self.buttonFrame = Frame(self, height=200, width=500, bg=self.layout["background"])
  218. self.buttonFrame.grid(column=0, row=1)
  219. self.buttonFrame.columnconfigure(0, weight=1) # Set weight to row and
  220. self.buttonFrame.rowconfigure(0, weight=1) # column where the widget is
  221. stopBtn = Button(self.buttonFrame, text="stop", command=lambda: self.controller.actionPerformed("stop"), height=1, width = 15, font=self.layout["font"], bg=self.layout["backgroundBtn"], fg=self.layout["fontColor"])
  222. stopBtn.grid()
  223. def adjustSize(self, height, width):
  224. self.windowFrame.configure(height=(height*4)/5, width= width)
  225. self.buttonFrame.configure(height=(height)/10, width= width)