Example #1
0
class Data_Cell(Cell):
    def __init__(self,
                 master,
                 variable,
                 anchor=W,
                 bordercolor=None,
                 borderwidth=1,
                 padx=0,
                 pady=0,
                 background=None,
                 foreground=None,
                 font=None,
                 row_num=0,
                 col_num=0,
                 table=None,
                 width=100,
                 text_to_img=None):
        Cell.__init__(self,
                      master,
                      background=background,
                      highlightbackground=bordercolor,
                      highlightcolor=bordercolor,
                      highlightthickness=borderwidth,
                      bd=0)
        self.background = background
        self.text_to_img = text_to_img
        self.variable = variable
        self.row_num = row_num
        self.table = table

        def change_var(*args):
            try:
                if self.text_to_img[self.variable.get()]:
                    #print(self.variable.get())
                    bg = background
                    fg = foreground
                    if self._message_widget:
                        bg = self._message_widget.cget('background')
                        fg = self._message_widget.cget('foreground')
                    self._message_widget.pack_forget()
                    self._message_widget = Label(
                        self,
                        width=width,
                        background=bg,
                        foreground=fg,
                        image=self.text_to_img[self.variable.get()])
                    self._message_widget.pack(expand=True,
                                              padx=padx,
                                              pady=pady,
                                              anchor=anchor)
                    self._message_widget.bind(
                        '<Button-1>',
                        lambda event, row=self.row_num, table=self.table:
                        cell_selected(event, table, row))
            except KeyError as e:
                pass
            return

        if text_to_img:
            variable.trace('w', change_var)

        self._message_widget = Message(self,
                                       width=width,
                                       textvariable=variable,
                                       font=font,
                                       background=background,
                                       foreground=foreground)
        self._message_widget.pack(expand=True,
                                  padx=padx,
                                  pady=pady,
                                  anchor=anchor)

        self.bind('<Button-1>',
                  lambda event, row=row_num, table=table: cell_selected(
                      event, table, row))
        self._message_widget.bind('<Button-1>',
                                  lambda event, row=row_num, table=table:
                                  cell_selected(event, table, row))