【问题标题】:Scrolling through multiple listboxes using one scrollbar without using a class使用一个滚动条滚动多个列表框而不使用类
【发布时间】:2017-07-31 15:41:47
【问题描述】:

我想在 Tkinter 中使用单个(垂直)滚动条同时(同步)滚动多个列表框。

问题在于 Stack Overflow 上的所有解决方案似乎都使​​用类将单个滚动条附加到多个列表框。如果可能的话,我想在不使用课程的情况下做同样的事情,因为我没有任何课程经验。

这是我的代码的简化版本:

from tkinter import *
import tkinter as tk


root = Tk()

##This code will only scroll through 1 listbox.
listbox1 = Listbox(root)
listbox1.grid(row=1, column=2)
listbox2 = Listbox(root)
listbox2.grid(row=1, column=3)
scrollbary = Scrollbar(root, command=listbox1.yview, orient=VERTICAL)
scrollbary.grid(row=1, column=1, sticky="ns")

for i in range(100):
    listbox1.insert("end","item %s" % i)
    listbox2.insert("end","item %s" % i)

【问题讨论】:

    标签: python tkinter listbox scrollbar python-3.6


    【解决方案1】:

    我修改了http://effbot.org/tkinterbook/listbox.htm 中的代码,以便它在类之外工作。

    import tkinter as tk
    
    root = tk.Tk()
    
    def yview(*args):
        """ scroll both listboxes together """
        listbox1.yview(*args)
        listbox2.yview(*args)
    
    listbox1 = tk.Listbox(root)
    listbox1.grid(row=1, column=2)
    listbox2 = tk.Listbox(root)
    listbox2.grid(row=1, column=3)
    scrollbary = tk.Scrollbar(root, command=yview)
    listbox1.config(yscrollcommand=scrollbary.set)
    listbox2.config(yscrollcommand=scrollbary.set)
    scrollbary.grid(row=1, column=1, sticky="ns")
    
    for i in range(100):
        listbox1.insert("end","item %s" % i)
        listbox2.insert("end","item %s" % i)
    
    root.mainloop()
    

    如果您还想用鼠标滚轮一起滚动它们,请参阅此问题的答案:Scrolling multiple Tkinter listboxes together。答案是通过类给出的,但绑定和函数也可以不使用类来完成。

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2011-05-03
      • 1970-01-01
      • 2018-04-20
      • 2016-01-10
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多