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