Tkinter中的滚动条不可移动。

huangapple go评论67阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年1月4日 01:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定