简体   繁体   English

可滚动框架Python Tkinter

[英]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. 如果从对create_window的调用中删除了widthheight选项,则框架将是可滚动的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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