class ImageSelectWindow(object): def __init__(self): self.image_Wid = 300 self.image_Hei = 300 self.image_interval = 10 self.canvas_Wid = 930 self.canvas_Hei = 600 self.window_wid = self.canvas_Wid + 160 self.window_Hei = self.canvas_Hei # init state control variable self.has_select_root_path = False # sign has select root path(where the sub-directories save) or not self.root_path = '' self.group_list = [] # create main window self.mainWin = Tkinter.Tk() self.mainWin.geometry( str(self.window_wid) + 'x' + str(self.window_Hei)) # init the size of window self.mainWin.title("image select tool") self.mainWin.tk.eval('package require Tix') # bind the key press event(space to pass, q to quit) self.mainWin.bind('<KeyPress>', self._keyPressEvent) # create init image(a black background) self.img_path_list = [] self.photo_img_list = [] # create Canvas control self.max_shown_num = 90 self.cv = Canvas(self.mainWin, bg='white', width=self.canvas_Wid, height=self.canvas_Hei, scrollregion=(0, 0, self.canvas_Wid, (self.image_Hei + self.image_interval) * math.ceil(self.max_shown_num / 3))) self.vbar = Scrollbar(self.cv, orient=Tkinter.VERTICAL) self.vbar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) self.vbar.config(command=self.cv.yview) self.cv.config(yscrollcommand=self.vbar.set) self.cv.pack(side=Tkinter.LEFT, expand=True, fill=Tkinter.BOTH) # bind the mouse click event self.cv.bind('<Button-1>', self._mouseClickEvent) # bind the mouse wheel event self.cv.bind_all('<MouseWheel>', self._mouseWheelEvent) # create total Frame to lay out all components self.frame = Frame(self.mainWin, width=250, height=self.window_Hei) self.frame.pack(side=Tkinter.LEFT) # create text control self.save_path = './image_select_result' self.entry = Entry(self.frame, state='normal') self.entry.pack(side=Tkinter.TOP) self.entry.insert(0, self.save_path) # create 'START' button self.btn_start = Button(self.frame, text='START', command=self._selectPath, activeforeground='blue', activebackground='white', bg='blue', fg='white') self.btn_start.pack(side=Tkinter.TOP, pady=30) # create data annotation label button self.btn_pass = Button(self.frame, text='PASS', command=lambda: self._passCurrentGroup(), activeforeground='black', activebackground='blue', bg='white', fg='black') self.btn_pass.pack(side=Tkinter.TOP, pady=30) #NumericUpDown控件 self.num_count = Control(self.frame, integer=True, max=-1, min=-1, value=-1, step=1, label='current Image:', command=self._showImages) self.num_count.label.config(font='Helvetica -14 bold') self.num_count.pack(side=Tkinter.TOP, pady=30) # create 'QUIT' button self.btn_quit = Button(self.frame, text='QUIT', command=self.mainWin.quit, activeforeground='blue', activebackground='white', bg='red', fg='white') self.btn_quit.pack(side=Tkinter.BOTTOM, pady=100) def _keyPressEvent(self, event): if event.keycode == 32: self._passCurrentGroup() elif event.keycode == 81: self.mainWin.destroy() def _mouseClickEvent(self, event): # get coordinate of x x_coordinate = event.x # compute coordinate of y # get the location of the scrollBar in the scroll range scale = self.vbar.get()[0] start_y = scale * ((self.image_Hei + self.image_interval) * math.ceil(self.max_shown_num / 3)) y_coordinate = start_y + event.y self._selectOneImage(x_coordinate, y_coordinate) def _mouseWheelEvent(self, event): self.cv.yview_scroll(-1 * (event.delta / 20), "units") def _showImages(self, ev=None): if self.has_select_root_path: if int(self.num_count['value']) == -1: # clear the images draw before first for idx in range(len(self.img_path_list)): self.cv.delete(self.img_path_list[idx]) self.img_path_list = [] self.photo_img_list = [] return else: # clear the images draw before first for idx in range(len(self.img_path_list)): self.cv.delete(self.img_path_list[idx]) # get the current group path and images list img_group_path = self.group_list[int(self.num_count['value'])] self.img_path_list = self._getImagePathList(img_group_path) self.img_path_list = self.img_path_list[:min( len(self.img_path_list), self.max_shown_num)] self.photo_img_list = [] for idx in range(len(self.img_path_list)): # load the current image cur_img_path = self.img_path_list[idx] cur_img = Image.open(cur_img_path).resize( (self.image_Wid, self.image_Hei)) self.photo_img_list.append(ImageTk.PhotoImage(cur_img)) # draw cur image to the appropriate location by index x_coordinate = (idx % 3) * (self.image_Wid + self.image_interval) y_coordinate = math.floor( idx / 3) * (self.image_Hei + self.image_interval) self.cv.create_image((x_coordinate, y_coordinate), anchor=Tkinter.NW, image=self.photo_img_list[idx], tags=self.img_path_list[idx]) # give a tag is convenient for delete later def _passCurrentGroup(self): cur_idx = int(self.num_count['value']) if cur_idx != -1: # check has next group or not if cur_idx + 1 < len(self.group_list): self.num_count['value'] = str(cur_idx + 1) else: tkMessageBox.showinfo( title='thanks', message='all the image select mission has finished~') def _selectOneImage(self, x_coordinate, y_coordinate): cur_idx = int(self.num_count['value']) if cur_idx == -1: return col = math.floor(x_coordinate / (self.image_Wid + self.image_interval)) row = math.floor(y_coordinate / (self.image_Hei + self.image_interval)) idx = int(row * 3 + col) if idx < len(self.img_path_list): img_click_path = self.img_path_list[idx] cur_img = Image.open(img_click_path) img_name = img_click_path.split('\\')[-1] cur_save_path = os.path.join(self.save_path, img_name) cur_img.save(cur_save_path) def _selectPath(self): self.root_path = tkFileDialog.askdirectory() self.group_list = self._getGroupPathList() if len(self.group_list) > 0: self.has_select_root_path = True self.num_count.config(max=len(self.group_list) - 1) self.num_count['value'] = str(0) else: self.has_select_root_path = False self.num_count['value'] = str(-1) self.num_count.config(max=-1) tkMessageBox.showwarning( 'warning', 'No available sub-directories detected! please re-select root path and start' ) def _getGroupPathList(self): group_list = [] for root, dirs, files in os.walk(self.root_path): for dir_name in dirs: cur_group_path = os.path.join(root, dir_name) group_list.append(cur_group_path) return group_list def _getImagePathList(self, img_group_path): img_path_list = [] for root, dirs, files in os.walk(img_group_path): for file_name in files: cur_img_path = os.path.join(img_group_path, file_name) img_path_list.append(cur_img_path) return img_path_list
class Chatbox(object): def __init__(self, master, my_nick=None, command=None, topic=None, entry_controls=None, maximum_lines=None, timestamp_template=None, scrollbar_background=None, scrollbar_troughcolor=None, history_background=None, history_font=None, history_padx=None, history_pady=None, history_width=None, entry_font=None, entry_background=None, entry_foreground=None, label_template=u"{nick}", label_font=None, logging_file=None, tags=None): self.interior = Frame(master, class_="Chatbox") self._command = command self._is_empty = True self._maximum_lines = maximum_lines self._timestamp_template = timestamp_template self._command = command self._label_template = label_template self._logging_file = logging_file if logging_file is None: self._log = None else: try: self._log = open(logging_file, "r") except: self._log = None top_frame = Frame(self.interior, class_="Top") top_frame.pack(expand=True, fill=BOTH) self._textarea = Text(top_frame, state=DISABLED) self._vsb = Scrollbar(top_frame, takefocus=0, command=self._textarea.yview) self._vsb.pack(side=RIGHT, fill=Y) self._textarea.pack(side=RIGHT, expand=YES, fill=BOTH) self._textarea["yscrollcommand"] = self._vsb.set entry_frame = Frame(self.interior, class_="Chatbox_Entry") entry_frame.pack(fill=X, anchor=N) if entry_controls is not None: controls_frame = Frame(entry_frame, class_="Controls") controls_frame.pack(fill=X) entry_controls(controls_frame, chatbox=self) bottom_of_entry_frame = Frame(entry_frame) self._entry_label = Label(bottom_of_entry_frame) self._entry = Entry(bottom_of_entry_frame) else: self._entry_label = Label(entry_frame) self._entry = Entry(entry_frame) self._entry.pack(side=LEFT, expand=YES, fill=X) self._entry.bind("<Return>", self._on_message_sent) self._entry.focus() if history_background: self._textarea.configure(background=history_background) if history_font: self._textarea.configure(font=history_font) if history_padx: self._textarea.configure(padx=history_padx) if history_width: self._textarea.configure(width=history_width) if history_pady: self._textarea.configure(pady=history_pady) if scrollbar_background: self._vsb.configure(background=scrollbar_background) if scrollbar_troughcolor: self._vsb.configure(troughcolor=scrollbar_troughcolor) if entry_font: self._entry.configure(font=entry_font) if entry_background: self._entry.configure(background=entry_background) if entry_foreground: self._entry.configure(foreground=entry_foreground) if label_font: self._entry_label.configure(font=label_font) if tags: for tag, tag_config in tags.items(): self._textarea.tag_config(tag, **tag_config) self.set_nick(my_nick) @property def topic(self): return @topic.setter def topic(self, topic): return def focus_entry(self): self._entry.focus() def bind_entry(self, event, handler): self._entry.bind(event, handler) def bind_textarea(self, event, handler): self._textarea.bind(event, handler) def bind_tag(self, tagName, sequence, func, add=None): self._textarea.tag_bind(tagName, sequence, func, add=add) def focus(self): self._entry.focus() def user_message(self, nick, content): if self._timestamp_template is None: self._write((u"%s:" % nick, "nick"), " ", (content, "user_message")) else: timestamp = datetime.datetime.now().strftime( self._timestamp_template) self._write((timestamp, "timestamp"), " ", (u"%s:" % nick, "nick"), " ", (content, "user_message")) def notification_message(self, content, tag=None): if tag is None: tag = "notification" self._write((content, tag)) notification = notification_message def notification_of_private_message(self, content, from_, to): if self._timestamp_template is None: self.notification_message( u"{from_} -> {to}: {content}".format(from_=from_, to=to, content=content), "notification_of_private_message") else: timestamp = datetime.datetime.now().strftime( self._timestamp_template) self.notification_message( u"{timestamp} {from_} -> {to}: {content}".format( timestamp=timestamp, from_=from_, to=to, content=content), "notification_of_private_message") def new_message(self, message): if isinstance(message, User_Message): self.user_message(message.content, message.nick) elif isinstance(message, Notification_Message): self.notification(message.content, message.tag) elif isinstance(message, Notification_Of_Private_Message): self.notification_of_private_message(message.from_, message.to, message.content) else: raise Exception("Bad message") def tag(self, tag_name, **kwargs): self._textarea.tag_config(tag_name, **kwargs) def clear(self): self._is_empty = True self._textarea.delete('1.0', END) @property def logging_file(self): return self._logging_file def send(self, content): if self._my_nick is None: raise Exception("Nick not set") self.user_message(self._my_nick, content) def _filter_text(self, text): return "".join(ch for ch in text if ch <= u"\uFFFF") def _write(self, *args): if len(args) == 0: return relative_position_of_scrollbar = self._vsb.get()[1] self._textarea.config(state=NORMAL) if self._is_empty: self._is_empty = False else: self._textarea.insert(END, "\n") if self._log is not None: self._log.write("\n") for arg in args: if isinstance(arg, tuple): text, tag = arg # Parsing not allowed characters text = self._filter_text(text) self._textarea.insert(END, text, tag) else: text = arg text = self._filter_text(text) self._textarea.insert(END, text) if self._log is not None: self._log.write(text) if self._maximum_lines is not None: start_line = int(self._textarea.index('end-1c').split('.') [0]) - self._maximum_lines if lines_to_delete >= 1: self._textarea.delete('%s.0' % start_line, END) self._textarea.config(state=DISABLED) if relative_position_of_scrollbar == 1: self._textarea.yview_moveto(1) def _on_message_sent(self, event): message = self._entry.get() self._entry.delete(0, END) self.send(message) if self._command: self._command(message) def set_nick(self, my_nick): self._my_nick = my_nick if my_nick: text = self._label_template.format(nick=my_nick) self._entry_label["text"] = text self._entry_label.pack(side=LEFT, padx=(5, 5), before=self._entry) else: self._entry_label.pack_forget()
class Chatbox(object): def __init__(self, master, my_nick=None, command=None, topic=None, entry_controls=None, maximum_lines=None, timestamp_template=None, scrollbar_background=None, scrollbar_troughcolor=None, history_background=None, history_font=None, history_padx=None, history_pady=None, history_width=None, entry_font=None, entry_background=None, entry_foreground=None, label_template=u"{nick}", label_font=None, logging_file='log.txt', tags=None): self._master = master self.interior = Frame(master, class_="Chatbox") self._command = command self._is_empty = True self._maximum_lines = maximum_lines self._timestamp_template = timestamp_template self._command = command self._label_template = label_template self._logging_file = logging_file if logging_file is None: self._log = None else: try: self._log = open(logging_file, "a") except: self._log = None #### INSTRUCTION: self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold") self.content_font = tkfont.Font(family='Helvetica', size=13) self.instruction_font = tkfont.Font(family='Courier', size=13) instruction_label_frame = Frame(self.interior, class_="Instruction_label") instruction_label_frame.pack(fill=X) self._label_instruction = Label(instruction_label_frame, text="INSTRUCTIONS", font=self.title_font) self._label_instruction.pack(side=LEFT) instruction_frame = Frame(self.interior, class_="Instruction") instruction_frame.pack(fill=X) self._text_instruction = Text(instruction_frame, height=13, borderwidth=2, relief="groove", font=self.instruction_font) self._text_instruction.pack(fill=X, side=LEFT, expand=True) quote = """1. Read the question. 2. Fill your answer Please answer in a COMPLETE SENTENCE, not just keywords. Example: Good: Bad: Question: What kind of food do you like? Question: What kind of food do you like? Reply: I like spicy food. Reply: spicy food. If you are reluctant to answer the question, click "Next Dialogue" button to go to the next question. 3. press Enter to submit your answer. 4. Rate the reply question in the Evaluation section 5. Click "Next Dialogue" button at the bottom to continue. """ self._text_instruction.insert(END, quote) counter_frame = Frame(self.interior) counter_frame.pack(fill=X) global counter_question counter_question = StringVar() counter_question.set(str(1)) Label(counter_frame, text="").pack(side='top', anchor='w') self._label_counter_1 = Label(counter_frame, text="DIALOGUE", font=self.title_font).pack(side=LEFT) self._label_counter_2 = Label(counter_frame, textvariable=counter_question, font=self.title_font).pack(side=LEFT) self._label_counter_3 = Label(counter_frame, text="OF 10 ", font=self.title_font).pack(side=LEFT) #### MESSAGE DISPLAY AREA: top_frame = Frame(self.interior, class_="Top") top_frame.pack( expand=True, fill=BOTH ) # True: the widget is expanded to fill any extra space, BOTH: fill X&Y self._textarea = Text(top_frame, height=1, borderwidth=2, relief="groove", state=DISABLED, font=self.content_font) self._vsb = Scrollbar(top_frame, takefocus=0, command=self._textarea.yview) self._vsb.pack(side=RIGHT, fill=Y) #scroll bar di kanan, sepanjang sumbu Y self._textarea.pack(side=RIGHT, expand=YES, fill=BOTH) self._textarea.insert(END, '\n') self._textarea[ "yscrollcommand"] = self._vsb.set # text area tempat display chat #### MESSAGE ENTRY: entry_frame = Frame(self.interior, class_="Chatbox_Entry") entry_frame.pack( fill=X, anchor=N ) # fill=X: make all widgets as wide as parent widget, anchor=N: place the text at the top #self.var_entry = StringVar() if entry_controls is not None: controls_frame = Frame(entry_frame, class_="Controls") controls_frame.pack(fill=X) entry_controls(controls_frame, chatbox=self) bottom_of_entry_frame = Frame(entry_frame) self._entry_label = Label(bottom_of_entry_frame) self._entry = Entry( bottom_of_entry_frame) #,textvariable=self.var_entry else: self._entry_label = Label(entry_frame) self._entry = Entry( entry_frame) #, width = 70, textvariable=self.var_entry self._entry.pack(side=LEFT, fill=X, expand=YES) self._entry.bind( "<Return>", self._on_message_sent ) # when user press enter in the chatbox, call on_message_sent self._entry.focus() #self._buttonmessage = Button(entry_frame, text="Submit", width = 20, command=self._on_message_sent) #self._buttonmessage.bind("<Button-1>", self._on_message_sent) # bind the action of the left button of your mouse to the button assuming your primary click button is the left one. #self._buttonmessage.pack(side=LEFT) #### label_evaluation = Frame(self.interior) label_evaluation.pack(fill=X, anchor=N) Label(label_evaluation, text="").pack(side='top', anchor='w') Label(label_evaluation, text="EVALUATION", font=self.title_font).pack(side='top', anchor='w') Label( label_evaluation, text= "Please indicate how strongly you agree or disagree with all the following statements." ).pack(side='top', anchor='w') #Label(label_evaluation, text="").pack(side='top', anchor='w') #### QUESTIONNAIRES: ''' Questionnaire frame''' question_frame = Frame(self.interior) #question_frame.grid(column=0, row=0, sticky=(N, W, E, S)) #question_frame.columnconfigure(0, weight=1) #question_frame.rowconfigure(0, weight=1) #self.configure(background="white") question_frame.pack() self.var_q1 = IntVar() self.var_q2 = IntVar() self.var_q3 = IntVar() self.var_q4 = IntVar() self.var_q1.set(0) # none selected self.var_q2.set(0) # none selected self.var_q3.set(0) # none selected self.var_q4.set(0) # none selected #Label(question_frame, text="").grid(column=0, row=0, sticky="W") #Label(question_frame, text="EVALUATION", font=self.title_font).grid(column=0, row=1, sticky="W") Label(question_frame, text="Strongly disagree").grid(column=1, row=3, padx=4) Label(question_frame, text="Disagree").grid(column=2, row=3, padx=4) Label(question_frame, text="Neither agree nor disagree").grid(column=3, row=3, padx=4) Label(question_frame, text="Agree").grid(column=4, row=3, padx=4) Label(question_frame, text="Strongly agree").grid(column=5, row=3, padx=4) Label(question_frame, text="The grammar of the reply question is correct").grid( column=0, row=4, sticky="W") Radiobutton(question_frame, variable=self.var_q1, value=1).grid(column=1, row=4) Radiobutton(question_frame, variable=self.var_q1, value=2).grid(column=2, row=4) Radiobutton(question_frame, variable=self.var_q1, value=3).grid(column=3, row=4) Radiobutton(question_frame, variable=self.var_q1, value=4).grid(column=4, row=4) Radiobutton(question_frame, variable=self.var_q1, value=5).grid(column=5, row=4) Label( question_frame, text= "The reply question is appropriate to be asked \nin a conversation", justify=LEFT).grid(column=0, row=5, sticky="W") Radiobutton(question_frame, variable=self.var_q2, value=1).grid(column=1, row=5) Radiobutton(question_frame, variable=self.var_q2, value=2).grid(column=2, row=5) Radiobutton(question_frame, variable=self.var_q2, value=3).grid(column=3, row=5) Radiobutton(question_frame, variable=self.var_q2, value=4).grid(column=4, row=5) Radiobutton(question_frame, variable=self.var_q2, value=5).grid(column=5, row=5) Label(question_frame, text="The reply question is related to my answer").grid( column=0, row=6, sticky="W") Radiobutton(question_frame, variable=self.var_q3, value=1).grid(column=1, row=6) Radiobutton(question_frame, variable=self.var_q3, value=2).grid(column=2, row=6) Radiobutton(question_frame, variable=self.var_q3, value=3).grid(column=3, row=6) Radiobutton(question_frame, variable=self.var_q3, value=4).grid(column=4, row=6) Radiobutton(question_frame, variable=self.var_q3, value=5).grid(column=5, row=6) Label(question_frame, text="The dialogue as a whole feels natural").grid(column=0, row=7, sticky="W") Radiobutton(question_frame, variable=self.var_q4, value=1).grid(column=1, row=7) Radiobutton(question_frame, variable=self.var_q4, value=2).grid(column=2, row=7) Radiobutton(question_frame, variable=self.var_q4, value=3).grid(column=3, row=7) Radiobutton(question_frame, variable=self.var_q4, value=4).grid(column=4, row=7) Radiobutton(question_frame, variable=self.var_q4, value=5).grid(column=5, row=7) #E1 = Entry(question_frame) #### NEXT QUESTIONS BUTTON: button_next = Frame(self.interior, class_="Evaluation_3") button_next.pack(fill=X, anchor=N) Label(button_next, text="").pack(side='top', anchor='w') Label( button_next, text= "Please give your comment here.\nFor example, give the reason why do you think the reply question is not appropriate, \nincorrect grammar, not related to your answer, or why it does not feel natural.", justify=LEFT).pack(side='top', anchor='w') self.var_comment = StringVar() self.E1 = Entry(button_next, textvariable=self.var_comment) self.E1.pack(side='top', anchor='w', expand=YES, fill=X) Label(button_next, text="").pack() #button self._button_next = Button(button_next, text="Next Dialogue >>", command=self._on_click_next, width=50) self._button_next.pack() #### if history_background: self._textarea.configure(background=history_background) if history_font: self._textarea.configure(font=history_font) if history_padx: self._textarea.configure(padx=history_padx) if history_width: self._textarea.configure(width=history_width) if history_pady: self._textarea.configure(pady=history_pady) if scrollbar_background: self._vsb.configure(background=scrollbar_background) if scrollbar_troughcolor: self._vsb.configure(troughcolor=scrollbar_troughcolor) if entry_font: self._entry.configure(font=entry_font) if entry_background: self._entry.configure(background=entry_background) if entry_foreground: self._entry.configure(foreground=entry_foreground) if label_font: self._entry_label.configure(font=label_font) if tags: for tag, tag_config in tags.items(): self._textarea.tag_config(tag, **tag_config) self.set_nick(my_nick) @property def topic(self): return @topic.setter def topic(self, topic): return def focus_entry(self): self._entry.focus() def bind_entry(self, event, handler): self._entry.bind(event, handler) def bind_textarea(self, event, handler): self._textarea.bind(event, handler) def bind_tag(self, tagName, sequence, func, add=None): self._textarea.tag_bind(tagName, sequence, func, add=add) def focus(self): self._entry.focus() def user_message(self, nick, content): if self._timestamp_template is None: self._write((u"%s:" % nick, "nick"), " ", (content, "user_message")) else: timestamp = datetime.datetime.now().strftime( self._timestamp_template) self._write((timestamp, "timestamp"), " ", (u"%s:" % nick, "nick"), " ", (content, "user_message")) def notification_message(self, content, tag=None): if tag is None: tag = "notification" self._write((content, tag)) notification = notification_message def notification_of_private_message(self, content, from_, to): if self._timestamp_template is None: self.notification_message( u"{from_} -> {to}: {content}".format(from_=from_, to=to, content=content), "notification_of_private_message") else: timestamp = datetime.datetime.now().strftime( self._timestamp_template) self.notification_message( u"{timestamp} {from_} -> {to}: {content}".format( timestamp=timestamp, from_=from_, to=to, content=content), "notification_of_private_message") def new_message(self, message): if isinstance(message, User_Message): self.user_message(message.content, message.nick) elif isinstance(message, Notification_Message): self.notification(message.content, message.tag) elif isinstance(message, Notification_Of_Private_Message): self.notification_of_private_message(message.from_, message.to, message.content) else: raise Exception("Bad message") def tag(self, tag_name, **kwargs): self._textarea.tag_config(tag_name, **kwargs) def clear(self): self._is_empty = True self._textarea.delete('1.0', END) @property def logging_file(self): return self._logging_file def send(self, content): if self._my_nick is None: raise Exception("Nick not set") self.user_message(self._my_nick, content) def _filter_text(self, text): return "".join(ch for ch in text if ch <= u"\uFFFF") def _write(self, *args): if len(args) == 0: return relative_position_of_scrollbar = self._vsb.get()[1] self._textarea.config(state=NORMAL) if self._is_empty: self._is_empty = False else: self._textarea.insert(END, "\n") if self._log is not None: self._log.write("\n") for arg in args: if isinstance(arg, tuple): text, tag = arg # Parsing not allowed characters text = self._filter_text(text) self._textarea.insert(END, text, tag) else: text = arg text = self._filter_text(text) self._textarea.insert(END, text) if self._log is not None: self._log.write(text) if self._maximum_lines is not None: start_line = int(self._textarea.index('end-1c').split('.') [0]) - self._maximum_lines if lines_to_delete >= 1: self._textarea.delete('%s.0' % start_line, END) self._textarea.config(state=DISABLED) if relative_position_of_scrollbar == 1: self._textarea.yview_moveto(1) def _on_message_sent(self, event): if not self._entry.get(): # or not self.var_entry.get() showwarning('Empty answer', 'Please fill your anwer') else: # update flag press enter global flag_press_enter global counter_enter_global if flag_press_enter == False: flag_press_enter = True #counter_enter += 1 if flag_press_enter == True: if counter_enter_global < 2: counter_enter_global += 1 # get the input from user #print("var_entry:",self.var_entry.get()) message = self._entry.get() # clear entry self._entry.delete(0, END) self.send(message) if self._command: self._command( message) # display user input to the chat window if counter_enter_global > 1: self._textarea.config(state=NORMAL) self._textarea.insert( END, "\n[Thank you for your response. Continue to evaluate the Reply Question]" ) self._textarea.config(state=DISABLED) else: ''' running the follow-up question generation ''' with open(input_path, 'w') as f: f.write("%s\n" % message) pp.preprocess_senna_input(input_path, senna_input_path) rs.runSENNA(senna_input_file) # getting semantic representation sentenceList, dlines = fqg.create_SR(senna_input_path) # generate the questions listOfAllGeneratedQA = fqg.generate_questions( sentenceList, dlines) print(listOfAllGeneratedQA) #reply_list = random.choice(listOfAllGeneratedQA[0]) reply = rs.ranking(listOfAllGeneratedQA) if self._log is not None: self._log.write("\nTemplate: %s" % listOfAllGeneratedQA) #reply = reply_list[0] print(reply) self.user_message("Reply question", reply) else: showinfo( 'Thank you', 'Thank you for your response. Please continue to evaluate the Reply Question' ) self._entry.focus() def set_nick(self, my_nick): self._my_nick = my_nick if my_nick: text = self._label_template.format(nick=my_nick) self._entry_label["text"] = text self._entry_label.pack(side=LEFT, padx=(5, 5), before=self._entry) else: self._entry_label.pack_forget() def _on_click_next(self): global flag_press_enter global counter_enter_global maximum_counter = 10 if flag_press_enter == True and self.var_q1.get( ) != 0 and self.var_q2.get() != 0 and self.var_q3.get( ) != 0 and self.var_q4.get() != 0: global counter_question_global if self._log is not None: self._log.write("\nDialogue number: %s" % str(counter_question_global)) # increase question counter counter_question_global += 1 # write questionnaire's answers to log file if self._log is not None: self._log.write("\nQ1: %s" % self.var_q1.get()) self._log.write("\nQ2: %s" % self.var_q2.get()) self._log.write("\nQ3: %s" % self.var_q3.get()) self._log.write("\nQ3: %s" % self.var_q4.get()) self._log.write("\nComment: %s" % self.var_comment.get()) #print ('debug: ',self.var_q1.get()) if counter_question_global > maximum_counter: showinfo( 'Finish', 'Thank you for your participation! \nWe will now save your responses. \nPress OK to close the application.' ) if OK: self._master.destroy() global flag_close flag_close = True elif self.var_q1.get() == 0 and flag_press_enter == True: showinfo('Rate the evaluation', 'Please rate all evaluations.') elif self.var_q2.get() == 0 and flag_press_enter == True: showinfo('Rate the evaluation', 'Please rate all evaluations.') elif self.var_q3.get() == 0 and flag_press_enter == True: showinfo('Rate the evaluation', 'Please rate all evaluations.') elif self.var_q4.get() == 0 and flag_press_enter == True: showinfo('Rate the evaluation', 'Please rate all evaluations.') else: # clear chat area self._textarea.config(state=NORMAL) self._textarea.delete('1.0', END) self._textarea.config(state=DISABLED) # fill with new starter's question with open(starter_questions) as f: dlines = f.read().splitlines( ) #reading a file without newlines starter = (random.choice(dlines)) self.user_message("Question", starter) # update question counter counter_question.set(str(counter_question_global)) # reset flag_press_enter = False counter_enter_global = 0 self.var_q1.set(0) # questionnaire 1 self.var_q2.set(0) # questionnaire 2 self.var_q3.set(0) # questionnaire 3 self.var_q4.set(0) # questionnaire 3 self.E1.delete(0, END) # clear comment self._entry.delete(0, END) # clear entry message