I am trying to make two text widget's scrolling synchronize. So far I've achieved that using an Scrollbar, when using the scrollbar it works fine. But for example, when I have the focus on one of the text widgets and I use the mousewheel to scroll, only the text widget with the focus is scrolled, the scrollbar is also updated but the other text remains the same. The same behaviour occurs when using page down or page up keys and as fas as I know for every form of scrolling that doesn't use the scrollbar.
This is my code, I think only init is the relevant part where I bind the events, but just in case I decided to put all my code:
## HexText class # # class HexText (tkk.Frame): __POS_TEXT = "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F" __OFFSET_TEXT = "0x00000000" __LINE_LENGTH = len(__POS_TEXT) def __init__(self, master): super(HexText, self).__init__(master) self.__create_widgets() self.__organize_widgets() def __scrolls(self, *args): self.__data.yview(*args) self.__offset.yview(*args) def __create_widgets(self): self.__scrollbar = tkk.Scrollbar(self) self.__scrollbar["orient"] = tk.VERTICAL self.__scrollbar["command"] = self.__scrolls self.__data = tk.Text(self) self.__data["height"] = 8 self.__data["width"] = HexText.__LINE_LENGTH self.__data["state"] = tk.DISABLED self.__data["relief"] = tk.GROOVE self.__data["yscrollcommand"] = self.__scrollbar.set self.__offset = tk.Text(self) self.__offset["height"] = 8 self.__offset["width"] = len(HexText.__OFFSET_TEXT) self.__offset["state"] = tk.DISABLED self.__offset["relief"] = tk.FLAT self.__offset["bg"] = self.winfo_toplevel()["bg"] self.__offset["yscrollcommand"] = self.__scrollbar.set self.__pos = tk.Text(self) self.__pos.insert(tk.CURRENT, HexText.__POS_TEXT) self.__pos["height"] = 1 self.__pos["width"] = HexText.__LINE_LENGTH self.__pos["state"] = tk.DISABLED self.__pos["relief"] = tk.FLAT self.__pos["bg"] = self.winfo_toplevel()["bg"] def __organize_widgets(self): self.__pos.grid(row = 0, column = 1, sticky = tk.N + tk.E + tk.W + tk.S) self.__offset.grid(row = 1, column = 0, sticky = tk.N + tk.E + tk.W + tk.S) self.__data.grid(row = 1, column = 1, sticky = tk.N + tk.E + tk.W + tk.S) self.__scrollbar.grid(row = 1, column = 2, sticky = tk.N + tk.E + tk.W + tk.S) @staticmethod def __get_char_index(string): i = str.find(string, '.') if i >= 0: i = int(string[i+1:]) else: raise ValueError return i @staticmethod def __get_line_index(string): i = str.find(string, '.') if i >= 0: i = int(string[:i]) else: raise ValueError return i @staticmethod def __get_hex_value(string): if (len(string) != 1): raise ValueError i = "%02X" % ord(string) return i def __update_offset(self, line_index): i = "0x%08X\n" % ((line_index) * 0x10) self.__offset["state"] = tk.NORMAL self.__offset.insert(tk.CURRENT, i) self.__offset["state"] = tk.DISABLED def __append(self, string): self.__data["state"] = tk.NORMAL self.__data.insert(tk.CURRENT, string) self.__data["state"] = tk.DISABLED def __write_char(self, string): str_index = self.__data.index(tk.CURRENT) i = HexText.__get_char_index(str_index) if (len(string) != 1): raise ValueError if (i == 0): self.__update_offset(HexText.__get_line_index(str_index) - 1) if (i == HexText.__LINE_LENGTH - 2): self.__append(HexText.__get_hex_value(string) + '\n') else: self.__append(HexText.__get_hex_value(string) + ' ') def write_str(self, string): for chars in string: self.__write_char(chars) This is an image of the widget I'm trying to create, a simple hex viewer (both text widgets have the same amount of lines):
https://i.sstatic.net/Yb8IH.png
So my question is, should I handle all the page up, page down, mousewheel and every other form of scrolling independently? Isn't there a more simple way of having both text widgets have the same scrolling all the time?