首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python可滚动框架有滚动条,但不滚动

Python可滚动框架有滚动条,但不滚动
EN

Stack Overflow用户
提问于 2015-11-06 19:41:24
回答 1查看 207关注 0票数 0

我有一个列表的已完成或持续的模拟,用户可以选择通过一个检查按钮和相互比较。最近这个列表超出了屏幕的大小,所以我想实现一个滚动条。我将窗口实现为Toplevel,并遵循stackOverflow上的一个示例,在Toplevel中放置一个框架,其中有一个画布,一个包含基于网格的复选框和标签数组的框架。滚动条显示在右边,但不能滚动。这是我的代码:

代码语言:javascript
运行
复制
 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"))

谢谢你的帮助。

我做了一些编辑,以使窗口可调整大小,并将滚动条放在正确的位置。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 20:53:39

如果希望小部件滚动,则不能使用grid将小部件放置在画布中。您必须使用画布的create_window方法。尝试删除以下语句:

代码语言:javascript
运行
复制
self.insideFrame.grid()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33574441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档