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

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. self.destroy()
  74. def createTopFrame(self):
  75. '''--------------------------------------------------------------------------------------------------
  76. Erstellt die Buttons und Textfelder fuer die Taskleiste und positionier diese im Frame
  77. '''
  78. self.topFrame = Frame(self, bg=self.layout["backgroundBar"], height=50, width=500)
  79. 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"])
  80. 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"])
  81. #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"])
  82. self.toplabel = Label(self.topFrame, text="visuelles BCI", font=self.layout["font"], bg=self.layout["backgroundBar"], fg=self.layout["fontColor"], )
  83. self.topFrame.grid(column=0, row=0)
  84. self.topFrame.grid_propagate(0)
  85. self.topFrame.pack_propagate(0)
  86. self.changeBtn.grid(column=0, row=0, padx=10,pady=5)
  87. self.saveBtn.grid(column=1, row=0, padx=10, pady=5)
  88. #self.setBtn.grid(column=2, row=0, padx=10, pady=5)
  89. self.toplabel.grid(column=2, row=0, padx=100, pady=2)
  90. def createMainFrame(self):
  91. '''--------------------------------------------------------------------------------------------------
  92. Erstellt und postioniert einen Container als Platzhalter fuer die verschiedenen Seiten. Anscliessend
  93. erstellt es die verschiedenen Seiten und speicher diese in eine Liste
  94. '''
  95. self.mainFrame = Frame(self, bg=self.layout["background"], height=250, width=500)
  96. self.mainFrame.grid(column=0, row=1)
  97. self.mainFrame.pack_propagate(0)
  98. self.container = Frame(self.mainFrame, bg=self.layout["background"], height=250, width=500)
  99. self.container.pack(side="top", fill="both", expand = True)
  100. self.container.pack_propagate(0)
  101. self.container.grid_propagate(0)
  102. self.frames = {}
  103. for F in (StartPage, WorkingPageTaktil, WorkingPageVisuell):
  104. page_name = F.__name__
  105. frame = F(parent=self.container, controller=self.controller, layout=self.layout)
  106. self.frames[page_name] = frame
  107. frame.grid(row=0, column=0, sticky="nsew")
  108. self.changeFrame("StartPage")
  109. def createBottomFrame(self):
  110. '''--------------------------------------------------------------------------------------------------
  111. Erstellt und positioniert ein Label fuer die ganzen Informationen
  112. '''
  113. self.bottomFrame = Frame(self, bg=self.layout["backgroundBar"], height=100, width=500)
  114. 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"])
  115. self.bottomFrame.grid(column=0, row=2)
  116. self.bottomFrame.pack_propagate(0)
  117. self.bottomFrame.grid_propagate(0)
  118. self.bottomlabel.grid(padx=5,pady=5)
  119. def setInfoText(self, text):
  120. self.bottomlabel['text'] = text
  121. def setTitleText(self,text):
  122. self.title(text)
  123. self.toplabel["text"] = text
  124. def setChangeBtnText(self,text):
  125. self.changeBtn["text"] = text
  126. def openfiledialog(self):
  127. path = self.PATH_DEFAULT
  128. #file = fd.askopenfilename(initialdir=path)
  129. file = fd.askdirectory(initialdir=path)
  130. return file
  131. def savefiledialog(self):
  132. self.dialog = Tk()
  133. l = Label(self.dialog, text="Bitte geben Sie einen Speichernamen ein:")
  134. eingabefeld = Entry(self.dialog)
  135. okBtn = Button(self.dialog, text="OK", command=lambda:self.okBtn(eingabefeld.get()))
  136. l.pack()
  137. eingabefeld.pack()
  138. okBtn.pack()
  139. self.dialog.protocol("WM_DELETE_WINDOW", self.closeDialog)
  140. self.dialog.mainloop()
  141. def okBtn(self, text):
  142. path = self.PATH_DEFAULT + "/" + text
  143. print(path)
  144. self.controller.copyspelling(path)
  145. self.dialog.destroy()
  146. def closeDialog(self):
  147. self.controller.copyspelling("-1")
  148. self.dialog.destroy()
  149. def getWindowSize(self):
  150. return [self.winfo_x(), self.winfo_y(), self.winfo_height(), self.winfo_width()]
  151. class StartPage(Frame):
  152. '''--------------------------------------------------------------------------------------------------
  153. Bildet die Seite in der die Aktionen ausgesucht werden koennen. Beihaltet 2 Buttons einen zum starten
  154. des freien Buchstabierens und einen yum starten des gefuerten Buchstabierens
  155. '''
  156. def __init__(self, parent, controller, layout):
  157. self.layout = layout
  158. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  159. self.grid_propagate(0)
  160. self.pack_propagate(0)
  161. self.controller = controller
  162. self.container = Frame(self, bg=self.layout["background"], height=250, width=500)
  163. self.btnFrame = Frame(self.container, bg=self.layout["background"], height=250, width=250)
  164. self.container.columnconfigure(0, weight=1) # Set weight to row and
  165. self.container.rowconfigure(0, weight=1) # column where the widget is
  166. self.container.grid_propagate(0)
  167. self.container.pack_propagate(0)
  168. self.container.pack()
  169. self.btnFrame.grid()
  170. 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"])
  171. #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"])
  172. testBtn.grid(row=0,column=0, padx=25,pady=25)
  173. 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"])
  174. freeSpellingBtn.grid(row=0, column=1, padx=25,pady=25)
  175. def adjustSize(self, height, width):
  176. self.container.configure(height=height, width= width)
  177. self.btnFrame.configure(height=(height*4)/5, width= width/2)
  178. class WorkingPageTaktil(Frame):
  179. '''--------------------------------------------------------------------------------------------------
  180. Bildet die Seite nach Auswahl einer Aktion. Beihaltet einen mittig plazierten Buttons zum stoppen
  181. des ablaufenden Vorgang
  182. '''
  183. def __init__(self, parent, controller, layout):
  184. self.layout = layout
  185. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  186. self.controller = controller
  187. self.columnconfigure(0, weight=1) # Set weight to row and
  188. self.rowconfigure(0, weight=1) # column where the widget is
  189. 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"])
  190. stopBtn.grid()
  191. def adjustSize(self, height, width):
  192. pass
  193. class WorkingPageVisuell(Frame):
  194. '''--------------------------------------------------------------------------------------------------
  195. Bildet die Seite nach Auswahl einer Aktion. Beihaltet einen mittig unten plazierten Buttons zum
  196. stoppen des ablaufenden Vorgang
  197. '''
  198. def __init__(self, parent, controller, layout):
  199. self.layout = layout
  200. Frame.__init__(self, parent, height=250, width=500, bg=self.layout["background"])
  201. self.controller = controller
  202. self.grid_propagate(0)
  203. self.pack_propagate(0)
  204. self.windowFrame = Frame(self, height=200, width=500, bg=self.layout["background"])
  205. self.windowFrame.grid(column=0, row=0)
  206. self.buttonFrame = Frame(self, height=200, width=500, bg=self.layout["background"])
  207. self.buttonFrame.grid(column=0, row=1)
  208. self.buttonFrame.columnconfigure(0, weight=1) # Set weight to row and
  209. self.buttonFrame.rowconfigure(0, weight=1) # column where the widget is
  210. 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"])
  211. stopBtn.grid()
  212. def adjustSize(self, height, width):
  213. self.windowFrame.configure(height=(height*4)/5, width= width)
  214. self.buttonFrame.configure(height=(height)/10, width= width)