Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (50.2k points)

I have executed the below code which is regarding the scrollbar and it is just running fine.

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)



I have tried to apply the same concept in my code like this:

import tkinter as tk

class interface(tk.Frame):

    def __init__(self,den):


        #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)


den = tk.Tk()


inter = interface(den)


But when I executed my code, I am getting an error on the insertion line.

NameError: global name 'END' is not defined

1 Answer

0 votes
by (108k points)

Kindly be informed that the END, LEFT, and BOTH all should be imported from the tkinter namespace in Python

for i in range(1000):

    self.lst1.insert(tk.END, str(i))

self.lst1.pack(side=tk.LEFT, fill=tk.BOTH)


Or, the easy way is to import them explicitly if you want:

from tkinter import BOTH, END, LEFT

Related questions

Browse Categories