gpt4 book ai didi

Python tkinter,使两个文本小部件的滚动同步

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:42 26 4
gpt4 key购买 nike

我正在尝试使两个文本小部件的滚动同步。到目前为止,我已经实现了使用滚动条,当使用滚动条时它工作正常。但是,例如,当我将焦点放在其中一个文本小部件上并使用鼠标滚轮滚动时,只有具有焦点的文本小部件会滚动,滚动条也会更新,但其他文本保持不变。当使用向下翻页或向上翻页键时,以及据我所知,对于不使用滚动条的每种滚动形式,同样的行为会发生。

这是我的代码,我认为只有 init 是我绑定(bind)事件的相关部分,但以防万一我决定将所有代码放在一起:

## 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)

这是我尝试创建的小部件的图像,一个简单的十六进制查看器(两个文本小部件的行数相同):

http://i.stack.imgur.com/Yb8IH.png

所以我的问题是,我是否应该独立处理所有向上翻页、向下翻页、鼠标滚轮和所有其他形式的滚动?有没有更简单的方法让两个文本小部件始终具有相同的滚动效果?

最佳答案

我知道这有点旧,但这个解决方案非常有效。默认情况下,文本小部件会响应滚动鼠标滚轮,因此无需绑定(bind)任何内容。

import sys
if sys.version[0] < '3':
from Tkinter import *
else:
from tkinter import *


class ScrolledTextPair(Frame):
'''Two Text widgets and a Scrollbar in a Frame'''

def __init__(self, master, **kwargs):
Frame.__init__(self, master) # no need for super

# Different default width
if 'width' not in kwargs:
kwargs['width'] = 30

# Creating the widgets
self.left = Text(self, **kwargs)
self.left.pack(side=LEFT, fill=BOTH, expand=True)
self.right = Text(self, **kwargs)
self.right.pack(side=LEFT, fill=BOTH, expand=True)
self.scrollbar = Scrollbar(self)
self.scrollbar.pack(side=RIGHT, fill=Y)

# Changing the settings to make the scrolling work
self.scrollbar['command'] = self.on_scrollbar
self.left['yscrollcommand'] = self.on_textscroll
self.right['yscrollcommand'] = self.on_textscroll

def on_scrollbar(self, *args):
'''Scrolls both text widgets when the scrollbar is moved'''
self.left.yview(*args)
self.right.yview(*args)

def on_textscroll(self, *args):
'''Moves the scrollbar and scrolls text widgets when the mousewheel
is moved on a text widget'''
self.scrollbar.set(*args)
self.on_scrollbar('moveto', args[0])


# Example
if __name__ == '__main__':
root = Tk()

t = ScrolledTextPair(root, bg='white', fg='black')
t.pack(fill=BOTH, expand=True)
for i in range(50):
t.left.insert(END,"foo %s\n" % i)
t.right.insert(END,"bar %s\n" % i)

root.title("Text scrolling example")
root.mainloop()

关于Python tkinter,使两个文本小部件的滚动同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com