For some reason, I was able to get this TKinter frame (allValOuterFrame) to expand both vertically and horizontally when its window is resized, but however, it appears that the canvas that the frame holds, as well as the frame and vertical scrollbar inside that canvas, will only expand horizontally. Could someone please explain why?
Here is my code:
# At first I only had "from X import *" but then discovered that
# it is bad practice, so I also included "import X" statements so
# that if I need to use something from tk or ttk explicitly, I can,
# but I had 2/3 of my code done at that point so I had to leave in the
# "import *" things.
try:
from Tkinter import * #python 2
import Tkinter as tk
from ttk import *
import ttk
import tkMessageBox as msg
import tkFileDialog as openfile
except ImportError:
from tkinter import * #python 3
import tkinter as tk
from tkinter.ttk import *
import tkinter.ttk as ttk
from tkinter import messagebox as msg
from tkinter import filedialog as openfile
import csv
# My stuff:
from extractor import Analysis
from extractor import createDictionariesAndLists as getRawData
from nitrogenCorrector import correct as nCorrect
from carbonCorrector import correct as cCorrect
( ... )
def createAllValWindow(self):
allValWindow = Toplevel(self)
allValWindow.grab_set()
if self.element == "N":
allValWindow.title("All Nitrogen Raw Data Values")
elif self.element == "C":
allValWindow.title("All Carbon Raw Data Values")
else:
allValWindow.title("All Raw Data Values")
allValOuterFrame = tk.Frame(allValWindow,background="#00FF00")
allValCanvas = Canvas(allValOuterFrame, borderwidth=0)
allValInnerFrame = Frame(allValCanvas, borderwidth=5)
def allValOnFrameConfigure(event):
allValCanvas.configure(scrollregion=allValCanvas.bbox("all"))
allValOuterFrame.grid_rowconfigure(0,weight=1)
allValOuterFrame.grid_columnconfigure(0,weight=1)
allValInnerFrame.grid_rowconfigure(0,weight=1)
allValInnerFrame.grid_columnconfigure(0,weight=1)
allValVertScrollbar = Scrollbar(allValOuterFrame, orient="vertical",command=allValCanvas.yview)
allValHorizScrollbar = Scrollbar(allValOuterFrame, orient="horizontal",command=allValCanvas.xview)
allValCanvas.configure(yscrollcommand=allValVertScrollbar.set, xscrollcommand=allValHorizScrollbar.set)
allValVertScrollbar.grid(row=1,column=12,sticky=N+S)
allValHorizScrollbar.grid(row=2,column=0,columnspan=12,sticky=E+W)
allValCanvas.grid(row=1,column=0,columnspan=12,sticky=N+S+E+W) allValCanvas.create_window((4,4),window=allValInnerFrame,anchor="nw",tags="allValInnerFrame")
allValInnerFrame.bind("",allValOnFrameConfigure)
allValDoneButton = Button(allValWindow,text="Done",command=allValWindow.destroy)
allValOuterFrame.pack(fill="both",expand=1)
allValDoneButton.pack()
textRows = [self.rawHeader]
textLabels = [[]]
numDiffAminos = len(self.analyses) - 1 # Ignore the "trash" list at the end
for singleAminoAnalyses in self.analyses[0:numDiffAminos]: # Once again, ignoring the last list
if len(singleAminoAnalyses) < 1:
continue
for analysis in singleAminoAnalyses:
textRows.append([str(analysis.analysisID),
str(analysis.row),
str(analysis.identifier1),
str(analysis.identifier2),
str(analysis.comment),
str(analysis.peakNumber),
str(analysis.rt),
str(analysis.component),
str(analysis.areaAll),
str(analysis.ampl),
str(analysis.r),
str(analysis.delta)])
textLabels.append([])
for i in range(len(textRows)):
if i == 0:
listRow = i
else:
listRow = i+1
for j in range(len(textRows[i])):
if i == 0:
textLabels[i].append(Label(allValInnerFrame,text=textRows[i][j],font=("Fixedsys",10,"bold")))
else:
textLabels[i].append(Label(allValInnerFrame,text=textRows[i][j]))
if j == 9:
textLabels[i][j].grid(row=listRow,column=j,sticky=W+E,padx=(4,10))
else:
textLabels[i][j].grid(row=listRow,column=j,sticky=W+E,padx=(4,4))
if i == 0:
separator = tk.Frame(allValInnerFrame, height=2, borderwidth=1, bg="black", relief=SUNKEN)
separator.grid(row=1,column=0,columnspan=12,sticky=W+E)
解决方案
It is because you give row 0 of AllValOuterFrame a weight of 1, but you put the canvas in row 1. If you move the canvas to row 0 or give row 1 a weight of 1, it will resize the way you expect.