我有一个完成或继续模拟的列表,用户可以通过检查按钮进行选择并相互比较。该列表最近超出了屏幕的大小,所以我想实现一个滚动条。我将窗口实现为Toplevel,然后在stackOverflow上的示例中在Toplevel中放置一个框架,其中包含一个框架,其中包含基于网格的复选框和标签数组。滚动条显示在右侧,但您无法滚动。这是我的代码:
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"))
感谢您的帮助。
我进行了大量编辑,以使窗口可调整大小并将滚动条放在正确的位置。谢谢你的帮助。
答案 0 :(得分:1)
如果您希望滚动小部件,则无法使用grid
将小部件放置在画布中。您必须使用画布的create_window
方法。请尝试删除此声明:
self.insideFrame.grid()