0

我希望滚动条同时滚动tk.Text。我尝试了很多东西,比如 : 的函数tk.Scrollbar(command=self.multiple_scroll)当前配置:

self.reader = tk.Toplevel(self)
self.reader.title("XXX")
self.reader.resizable(False, True)
self.reader.geometry("650x500")
self.reader.minsize(650,150)

self.panel = tk.Text(self.reader, wrap="word", width=12, font=('Courier',14))
self.viewer = tk.Text(self.reader, wrap="word", font=('Courier',14))
self.scrollbar = tk.Scrollbar(self.reader, command=(self.panel.yview, self.viewer.yview))

self.panel.configure(yscrollcommand=self.scrollbar.set)
self.viewer.configure(yscrollcommand=self.scrollbar.set)

self.scrollbar.pack(side="right", fill="y")
self.panel.pack(side="left", fill="both", expand=True)
self.viewer.pack(side="bottom", fill="both", expand=True)

当我使用滚轮滚动时,光标下的文本会移动,但不会移动另一个。我需要将两者tk.Text分开。

这是一张图片

4

1 回答 1

1

从这里复制:Python tkinter,使两个文本小部件的滚动同步,当然从这里更重要:多个命令到 tkinter中的按钮,上面写着:

To do "two things with one button", the correct solution is to create a function that does these "two things", and then associate that function with the button.

我最终得到:

将 tkinter 导入为 tk


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.reader= parent
        
        # self.reader = tk.Toplevel(self)
        self.reader.title("XXX")
        self.reader.resizable(False, True)
        self.reader.geometry("650x50")
        self.reader.minsize(650,150)
        
        self.panel = tk.Text(self.reader, wrap="word", width=12, font=('Courier',14))
        self.viewer = tk.Text(self.reader, wrap="word", font=('Courier',14))
        # self.scrollbar = tk.Scrollbar(self.reader, command=(self.panel.yview, self.viewer.yview))
        
        self.scrollbar = tk.Scrollbar(self.reader, command=(self.on_scrollbar))
        
        #self.panel.configure(yscrollcommand=self.scrollbar.set)
        #self.viewer.configure(yscrollcommand=self.scrollbar.set)
        
        self.scrollbar.pack(side="right", fill="y")
        self.panel.pack(side="left", fill="both", expand=True)
        self.viewer.pack(side="bottom", fill="both", expand=True)
        
        
        
        # Changing the settings to make the scrolling work
        # self.scrollbar['command'] = self.on_scrollbar
        self.panel['yscrollcommand'] = self.on_textscroll
        self.viewer['yscrollcommand'] = self.on_textscroll
        
        
    def on_scrollbar(self, *args):
        """
        Scrolls both text widgets when the scrollbar is moved
        """
        self.panel.yview(*args)
        self.viewer.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])



if __name__ == "__main__":
    master = tk.Tk()
   
    MainApplication(master)  
    master.mainloop()  

让我们知道它是否按预期工作,我注意到的是,一旦两个文本中的一个到达末尾,另一个文本会继续使两个文本在小于最长文本的窗口中不对齐。不知道如何处理,需要找到一种添加空行的方法,以便两个文本具有相同的长度,让我知道是否有更好的方法

于 2021-09-16T17:15:03.630 回答