Tkinter Scrollbar问题

时间:2014-07-25 18:31:04

标签: python-3.x tkinter

我正在尝试将滚动条附加到我的一个顶级列表框中。但是,它不是附加到我的列表框,而是将其自身附加到顶层。我完全迷失在这里。关于我做错了什么的任何想法?如果有人需要,我可以提供整个程序代码。

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)

1 个答案:

答案 0 :(得分:2)

浏览this教程,看起来通常的方法是创建一个包含两个小部件的框架:列表框和滚动条。在您的情况下,它看起来像:

    #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)
相关问题