简体   繁体   English

Python Tkinter可滚动框架具有滚动条,但不滚动

[英]Python Tkinter Scrollable frame has a scrollbar but doesn't scroll

I have a list of completed or continuing simulations that the user can select via a checkbutton and compare with one another. 我有一个完整或连续的仿真列表,用户可以通过一个选择按钮进行选择,并相互比较。 The list recently grew beyond the size of the screen and so I would like to implement a scrollbar. 该列表最近超出了屏幕的大小,因此我想实现一个滚动条。 I implemented the window as a Toplevel and following an example on stackOverflow put a frame in the Toplevel with a canvas in that with a frame in that which contains the grid-based array of checkboxes and labels. 我将窗口实现为顶层,并按照stackOverflow上的示例在顶层中放置了一个带有画布的框架,该框架中包含一个框架,该框架包含基于网格的复选框和标签。 The scroll bar shows on the right but you can't scroll. 滚动条显示在右侧,但您无法滚动。 Here is my code: 这是我的代码:

 def overview_callback(self):
    self.overviewWindow = Toplevel(self.master)
    self.overviewWindow.geometry("1500x500")
    self.overviewFrame = Frame(self.overviewWindow)
    self.overviewFrame.pack(side=TOP, fill="both", expand=True)
    self.overviewCanvas = Canvas(self.overviewFrame)
    self.insideFrame = Frame(self.overviewCanvas)
    self.hsb = Scrollbar(self.overviewFrame, orient="horizontal", command=self.overviewCanvas.xview)
    self.hsb.pack(side=BOTTOM, fill="x")
    self.overviewCanvas.configure(xscrollcommand=self.hsb.set)

    self.vsb = Scrollbar(self.overviewFrame, orient="vertical", command=self.overviewCanvas.yview)
    self.overviewCanvas.configure(yscrollcommand=self.vsb.set)
    self.vsb.pack(side=RIGHT, fill="y")
    self.overviewCanvas.pack(side=TOP, fill="both", expand=True)
    self.overviewCanvas.create_window((0,0), window=self.insideFrame, anchor="nw", tags="self.insideFrame")
    self.overviewCanvas.pack(side=TOP, fill="both", expand=True)
    self.insideFrame.bind("<Configure>", self.onFrameConfigure)

    self.overviewWindow.title("Simulations Overview")
    self.overviewList = []
    self.overviewVariables = []
    count = 0
    for geometry in self.gNames:
        goalNames = os.listdir('Geometries/'+geometry+'/Goals')
        if len(goalNames) > 0:
            for l, goal in enumerate(goalNames):
                count += 1
                self.overviewList.append([geometry, goal])
                self.overviewVariables.append(IntVar())
                Checkbutton(self.insideFrame, variable=self.overviewVariables[-1]).grid(row=count, column=0, sticky=W)
                Label(self.insideFrame, text=geometry+","+goal+":").grid(row=count, column=1, sticky=W)
                simStatus, simType, latestResult = tuneUtils.getLatestResult(goal, 'Geometries/'+geometry+'/Goals/'+goal+"/Output.txt")
                if simStatus == "Not Started":
                    Label(self.insideFrame, text=simStatus).grid(row=count, column=2, sticky=W)
                else:
                    # Extract Within/Not Within and GlobalMetric number to display
                    upToNotWithin = re.findall("^.*"+"not within Tolerance: ["+"[\w\s]*\]",latestResult)[0]
                    score = re.findall("GlobalMetric = "+"[\s\-\d\.]*", latestResult)[0]
                    Label(self.insideFrame, text=simStatus+" "+simType+" "+upToNotWithin+" "+score).grid(row=count, column=2, sticky=W)
    button_frame = Frame(self.overviewWindow)
    button_frame.pack(side=BOTTOM)
    exit_button = Button(button_frame, text='Exit', command=self.overviewExit_callback)
    compare_button = Button(button_frame, text='Compare', command=self.overviewCompare_callback)
    for b in (exit_button, compare_button):
        b.pack(side=LEFT, expand=YES, padx=10, pady=10, fill=BOTH)

def onFrameConfigure(self, event):
    self.overviewCanvas.configure(scrollregion=self.overviewCanvas.bbox("all"))

Thank you for your help. 谢谢您的帮助。

I have made a number of edits in order to make the windows resizable and to put the scrollbars in the correct spots. 为了使窗口可调整大小并将滚动条放置在正确的位置,我进行了许多编辑。 Thanks for the help. 谢谢您的帮助。

You cannot use grid to place a widget inside a canvas if you expect that widget to scroll. 如果希望小部件滚动,则不能使用grid将其放置在画布中。 You must use the create_window method of the canvas. 您必须使用画布的create_window方法。 Try removing this statement: 尝试删除以下语句:

self.insideFrame.grid()

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

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