Python tkinter, Making two text widget's scrolling synchronize -
i trying make 2 text widget's scrolling synchronize. far i've achieved using scrollbar, when using scrollbar works fine. example, when have focus on 1 of text widgets , use mousewheel scroll, text widget focus scrolled, scrollbar updated other text remains same. same behaviour occurs when using page down or page keys , fas know every form of scrolling doesn't use scrollbar.
this code, think init relevant part bind events, in case decided put 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): = str.find(string, '.') if >= 0: = int(string[i+1:]) else: raise valueerror return @staticmethod def __get_line_index(string): = str.find(string, '.') if >= 0: = int(string[:i]) else: raise valueerror return @staticmethod def __get_hex_value(string): if (len(string) != 1): raise valueerror = "%02x" % ord(string) return def __update_offset(self, line_index): = "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) = 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): chars in string: self.__write_char(chars)
this image of widget i'm trying create, simple hex viewer (both text widgets have same amount of lines):
http://i.stack.imgur.com/yb8ih.png
so question is, should handle page up, page down, mousewheel , every other form of scrolling independently? isn't there more simple way of having both text widgets have same scrolling time?
to answer question if event handling should done independently each scrollbar - decision have make. if want make widget purpose, can handle both not on own. create custom event handler , call setters/getters accordingly.
if have 2 widgets (including 2 scrollbars) in main widget want have events bound, bind them on there using widget.bind(<event>, handler)
or using widget.bind_all(<event>, handler)
bind events raised child widgets these handlers.
as using same handler (self.__scrollbar.set
) in code, use custom event handler bind page up
/ page down
keys scroll specific offset, , 1 custom scroll mousewheel
- event.
if scroll independently , call both scrolling functions @ these handlers or if scroll both in 1 function you, mentioned above.
as scrolling needs called each tk.text
-widget prefer calling both scrollings in 1 handler bound parent widget , below (so using bind_all
), matter of personal preference think.
Comments
Post a Comment