I have found this code about scrollbar is just working fine.
JavaScript
x
16
16
1
from tkinter import *
2
3
master = Tk()
4
5
scrollbar = Scrollbar(master)
6
scrollbar.pack(side=RIGHT, fill=Y)
7
8
listbox = Listbox(master, yscrollcommand=scrollbar.set)
9
for i in range(10000):
10
listbox.insert(END, str(i))
11
listbox.pack(side=LEFT, fill=BOTH)
12
13
scrollbar.config(command=listbox.yview)
14
15
mainloop()
16
I try to use it in my code like this:
JavaScript
1
23
23
1
import tkinter as tk
2
3
class interface(tk.Frame):
4
def __init__(self,den):
5
self.tklist()
6
#in my code, tklist is not called here. I called it here to minimize the code
7
#there are stuff in here also
8
9
def tklist(self):
10
scrollbar = tk.Scrollbar(den)
11
self.lst1 = tk.Listbox(den, selectmode="SINGLE", width="100", yscrollcommand=scrollbar.set)
12
for i in range(1000):
13
self.lst1.insert(END, str(i))
14
self.lst1.pack(side=LEFT, fill=BOTH)
15
scrollbar.config(command=lst1.yview)
16
17
den = tk.Tk()
18
den.title("Search")
19
20
inter = interface(den)
21
22
den.mainloop()
23
But when I ran above code, I got an error on insertion line.
JavaScript
1
2
1
NameError: global name 'END' is not defined
2
By the way, I tried to find documentation and a link from effbot is the closest I got but still couldn’t understand what is wrong.
Advertisement
Answer
END
, LEFT
, and BOTH
all reside in the tkinter
namespace. Thus, they need to be qualified by placing tk.
before them:
JavaScript
1
5
1
for i in range(1000):
2
self.lst1.insert(tk.END, str(i))
3
self.lst1.pack(side=tk.LEFT, fill=tk.BOTH)
4
scrollbar.config(command=lst1.yview)
5
Or, you could import them explicitly if you want:
JavaScript
1
2
1
from tkinter import BOTH, END, LEFT
2