简体   繁体   中英

Scrollable Frame Python Tkinter

I am working na project that has a scroll able frame. It lets me add widgets to the frame but I can not get the frame to scroll and show the rest of the widgets. I have compared my code to other scroll able frames online and I could not notice the difference. Any one see the solution.

Code:

from Tkinter import *
import ttk
import os

class GUI(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.pack(fill=BOTH, expand=YES)

    def gameView(self):
        self.mainFrame = Frame(self)
        self.mainFrame.pack(side=TOP)

        self.scroller = ttk.Scrollbar(self.mainFrame, orient=VERTICAL)
        self.scroller.pack(side=RIGHT, fill=Y)

        self.canvas = Canvas(self.mainFrame, bd=0)
        self.canvas.pack(fill=BOTH, side=LEFT)

        self.viewArea = Frame(self.canvas, bg="Pink")
        self.viewArea.pack(side=TOP, fill=BOTH)

        self.canvas.config(yscrollcommand=self.scroller.set)
        self.scroller.config(command=self.canvas.yview)
        self.canvas.create_window((0,0), window=self.viewArea, anchor=NW, width=783, height=650)

        self.viewArea.bind("<Configure>", self.scrollCom)

        self.itemHolder = Frame(self.viewArea, bg="Pink")
        self.itemHolder.pack(side=TOP)

        self.gameGather()

    def scrollCom(self, event):
        self.canvas.config(scrollregion=self.canvas.bbox("all"), width=783, height=650)

    def gameGather(self):
        for i in range(0, 50):
            label = Label(self.viewArea, text="Pie")
            label.pack(side=TOP)

root = Tk()
root.title("School Vortex 2.0")
root.geometry("800x650")
root.resizable(0,0)

gui = GUI(root)
gui.gameView()

root.mainloop()

When you put the window on the canvas you are explicitly giving it a height and a width. Because of that, the actual width and height of the frame is completely ignored. Because the frame is almost exactly the height of the canvas, there's nothing to scroll.

If you remove the width and height options from the call to create_window your frame will be scrollable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM