unlikelysyntax
unlikelysyntax

Reputation: 3

Trouble with Tkinter Scrollbar

I am trying to attach a scrollbar to my listbox in one of my toplevels. However, instead of attaching to my listbox it is attaching itself to the toplevel instead. I am at a complete lost here. Any ideas on what I am doing wrong? I can provide the whole program code if anyone needs it.

def onNewRecipe(self):
    self.top = Toplevel()
    self.top.title("New Recipe")

    quantity = StringVar()


    #Center the window
    w = 600
    h = 600

    sw = self.top.winfo_screenwidth()
    sh = self.top.winfo_screenheight()

    x = (sw - w)/2
    y = (sh - h)/2
    self.top.geometry('%dx%d+%d+%d' % (w, h, x, y))

    #Add quantity label
    addQuantity = Label(self.top, text="Add Quantity:")
    addQuantity.place(x=0,y=0)

    quantityAdd = Entry(self.top, textvariable=quantity)
    quantityAdd.place(x=150, y=0)

    #Add ingredient label
    addIngredient = Label(self.top, text="Add Ingredients:")
    addIngredient.place(x=0,y=30)

    ingredientAdd = Entry(self.top)
    ingredientAdd.place(x=150, y=30)

    #Select measurements label
    selectMeasurement = Label(self.top, text="Select Measurements:")
    selectMeasurement.place(x=0, y=60)

    measurement = StringVar()
    measurement.set("ounce")

    measurementSelect = OptionMenu(self.top, measurement, "ounce", "pound", "gallon", "quart", "fl oz", "pint", "cup", "table spoon", "teaspoon")
    measurementSelect.place(x=150, y=60)

    #Add measurements label
    addMeasurement = Label(self.top, text="Amount:")
    addMeasurement.place(x=0, y=100)

    measurementAdd = Entry(self.top)
    measurementAdd.place(x=150, y=100)

    #Add the textwidget
    recipeText = Text(self.top)
    recipeText.place(x=0,y=200)

    #Cooking direction label
    cookingDirection = Label(self.top, text="Cooking Direction")
    cookingDirection.place(x=0,y=175)

    def onNewIngredient():
        qVar = quantity.get()
        print(qVar)


    #Add the Add Button
    addButton = Button(self.top, text="Add", command= onNewIngredient)
    addButton.place(x=0, y=130)

    #Add Ingredients listbox
    ingredScroll = Scrollbar(self.top, orient=VERTICAL)
    ingredientListbox = Listbox(self.top, yscrollcommand=ingredScroll.set)
    ingredScroll.config(command=ingredientListbox.yview)
    ingredScroll.pack(side=RIGHT, fill=Y)
    ingredientListbox.place(x=450, y=0)

Upvotes: 0

Views: 88

Answers (1)

Kevin
Kevin

Reputation: 76184

Browsing this tutorial, it looks like the usual way to do this is to create a Frame that contains exactly two widgets: the list box and the scroll bar. In your case, it would look like:

    #Add Ingredients listbox
    box = Frame(self.top)
    ingredScroll = Scrollbar(box, orient=VERTICAL)
    ingredientListbox = Listbox(box, yscrollcommand=ingredScroll.set)
    ingredScroll.config(command=ingredientListbox.yview)
    ingredScroll.pack(side=RIGHT, fill=Y)
    ingredientListbox.pack()
    box.place(x=450, y=0)

Upvotes: 2

Related Questions