我发现这个关于滚动条的代码工作正常。
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
顺便说一句,我试图找到文档,a link from effbot是我得到的最接近但仍然无法理解的错误。
答案 0 :(得分:9)
END
,LEFT
和BOTH
都位于tkinter
命名空间中。因此,他们需要通过在他们面前放置tk.
来获得资格:
for i in range(1000):
self.lst1.insert(tk.END, str(i))
self.lst1.pack(side=tk.LEFT, fill=tk.BOTH)
scrollbar.config(command=lst1.yview)
或者,您可以根据需要明确导入它们:
from tkinter import BOTH, END, LEFT
答案 1 :(得分:0)
使用“ end”代替END
from tkinter import *
self.lst1.insert("end", str(i))