我发现这段关于滚动条的代码运行良好。
from tkinter import *
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(master, yscrollcommand=scrollbar.set)
for i in range(10000):
listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()
我尝试在我的代码中这样使用它:
import tkinter as tk
class interface(tk.Frame):
def __init__(self,den):
self.tklist()
#in my code, tklist is not called here. I called it here to minimize the code
#there are stuff in here also
def tklist(self):
scrollbar = tk.Scrollbar(den)
self.lst1 = tk.Listbox(den, selectmode="SINGLE", width="100", yscrollcommand=scrollbar.set)
for i in range(1000):
self.lst1.insert(END, str(i))
self.lst1.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=lst1.yview)
den = tk.Tk()
den.title("Search")
inter = interface(den)
den.mainloop()
但是当我运行上面的代码时,在插入行出现了错误。
NameError: global name 'END' is not defined
顺便说一下,我试图查找文档,从effbot的链接是我能找到的最接近的,但仍然无法理解问题出在哪里。
"end"
,"left"
或"both"
。我从来没有完全理解为什么 tkinter 会为这些字符串创建常量。 - Bryan Oakley