英文:
Scrollbar in Tkinter not moveable
问题
我正在构建一个程序的初级阶段,可以从TreeView中获取项目并将它们移动到ListBox中。我已经为TreeView和ListBox添加了滚动条,以便它们变得更长时,我可以滚动查看内容。目前,我已经在TreeView中填充了比窗口能容纳的更多项目。我可以使用鼠标滚轮滚动TreeView,但当我点击并拖动滚动条上的框时,滚动条不会移动。我正在使用Python 3.7。我需要一些帮助弄清楚发生了什么。
import tkinter as tk
from tkinter import ttk
import l5x
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("TreeView Example")
tree_frame = ttk.Frame(root)
list_frame = ttk.Frame(root)
tree = ttk.Treeview(tree_frame)
tree_scroll = ttk.Scrollbar(tree_frame, orient="vertical", command=tree.yview())
tree.configure(yscrollcommand=tree_scroll.set)
listbox = tk.Listbox(tree_frame)
list_scroll = ttk.Scrollbar(tree_frame, orient="vertical", command=listbox.yview())
listbox.configure(yscrollcommand=list_scroll.set)
tree_frame.columnconfigure(0, weight=1)
tree_frame.columnconfigure(1, weight=1)
tree_frame.columnconfigure(2, weight=1)
tree_frame.columnconfigure(3, weight=1)
for i in range(0, 20):
tree.insert('', 'end', i, text="blah")
tree_frame.grid(row=0, column=0, sticky="NSEW")
tree.grid(row=0, column=0, sticky="NSEW")
tree_scroll.grid(row=0, column=1, sticky="NS")
listbox.grid(row=0, column=2, sticky="NSEW")
list_scroll.grid(row=0, column=3, sticky="NS")
root.mainloop()
英文:
I'm in the beginning stages of building a program where I can take items from a TreeView and move them into a listbox. I have added scrollbars to both the Treeview and listbox so that as they get longer, I can scroll to see the contents. Currently, I have populated the Treeview with more items than can fit in the window. I have the ability to scroll the Treeview with the mouse wheel, but when I click and drag the box on the scrollbar, the scrollbar will not move. I'm using Python 3.7. I could use some help figuring out what's going on.
import tkinter as tk
from tkinter import ttk
import l5x
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("TreeView Example")
tree_frame = ttk.Frame(root)
list_frame = ttk.Frame(root)
tree = ttk.Treeview(tree_frame)
tree_scroll = ttk.Scrollbar(tree_frame,orient="vertical",command=tree.yview())
tree.configure(yscrollcommand=tree_scroll.set)
listbox = tk.Listbox(tree_frame)
list_scroll = ttk.Scrollbar(tree_frame,orient="vertical",command=listbox.yview())
listbox.configure(yscrollcommand=list_scroll.set)
tree_frame.columnconfigure(0, weight=1)
tree_frame.columnconfigure(1, weight=1)
tree_frame.columnconfigure(2, weight=1)
tree_frame.columnconfigure(3, weight=1)
for i in range(0,20):
tree.insert('','end',i,text="blah")
tree_frame.grid(row=0,column=0,sticky="NSEW")
tree.grid(row=0,column=0,sticky="NSEW")
tree_scroll.grid(row=0,column=1,sticky="NS")
listbox.grid(row=0,column=2,sticky="NSEW")
list_scroll.grid(row=0,column=3,sticky="NS")
root.mainloop()
答案1
得分: 0
command
属性要求您传递一个可调用函数的引用。相反,您正在调用 yview()
方法并将结果传递给 command
属性。
您的滚动条定义应该如下所示:
tree_scroll = ttk.Scrollbar(tree_frame, orient="vertical", command=tree.yview)
list_scroll = ttk.Scrollbar(tree_frame, orient="vertical", command=listbox.yview)
英文:
The command
attribute requires you pass a reference to a callable function. Instead, you're calling the yview()
method and giving the results to the command
attribute.
Your scrollbar definition needs to look like this:
tree_scroll = ttk.Scrollbar(tree_frame,orient="vertical",command=tree.yview)
list_scroll = ttk.Scrollbar(tree_frame,orient="vertical",command=listbox.yview)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论