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

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